aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-03-06 21:13:48 +0100
committerGitHub <noreply@github.com>2017-03-06 21:13:48 +0100
commit9971f7c82cc47446b457464cec5b4fefcae470e1 (patch)
tree87b01fc1dd9d0ca1f0c874aad847029eab74d0d8 /application
parent236239be752a7bb24547237b5751ac4fcbc0e549 (diff)
parent36c8fb1ef869c29e783f0dd5ebef2fb5566e2611 (diff)
downloadShaarli-9971f7c82cc47446b457464cec5b4fefcae470e1.tar.gz
Shaarli-9971f7c82cc47446b457464cec5b4fefcae470e1.tar.zst
Shaarli-9971f7c82cc47446b457464cec5b4fefcae470e1.zip
Merge pull request #750 from ArthurHoaro/feature/intl-dates
Improve datetime display
Diffstat (limited to 'application')
-rw-r--r--application/Utils.php85
1 files changed, 75 insertions, 10 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 35d65224..a936b09f 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -216,20 +216,55 @@ function is_session_id_valid($sessionId)
216function autoLocale($headerLocale) 216function autoLocale($headerLocale)
217{ 217{
218 // Default if browser does not send HTTP_ACCEPT_LANGUAGE 218 // Default if browser does not send HTTP_ACCEPT_LANGUAGE
219 $attempts = array('en_US'); 219 $attempts = array('en_US', 'en_US.utf8', 'en_US.UTF-8');
220 if (isset($headerLocale)) { 220 if (isset($headerLocale)) {
221 // (It's a bit crude, but it works very well. Preferred language is always presented first.) 221 // (It's a bit crude, but it works very well. Preferred language is always presented first.)
222 if (preg_match('/([a-z]{2})-?([a-z]{2})?/i', $headerLocale, $matches)) { 222 if (preg_match('/([a-z]{2,3})[-_]?([a-z]{2})?/i', $headerLocale, $matches)) {
223 $loc = $matches[1] . (!empty($matches[2]) ? '_' . strtoupper($matches[2]) : ''); 223 $first = [strtolower($matches[1]), strtoupper($matches[1])];
224 $attempts = array( 224 $separators = ['_', '-'];
225 $loc.'.UTF-8', $loc, str_replace('_', '-', $loc).'.UTF-8', str_replace('_', '-', $loc), 225 $encodings = ['utf8', 'UTF-8'];
226 $loc . '_' . strtoupper($loc).'.UTF-8', $loc . '_' . strtoupper($loc), 226 if (!empty($matches[2])) {
227 $loc . '_' . $loc.'.UTF-8', $loc . '_' . $loc, $loc . '-' . strtoupper($loc).'.UTF-8', 227 $second = [strtoupper($matches[2]), strtolower($matches[2])];
228 $loc . '-' . strtoupper($loc), $loc . '-' . $loc.'.UTF-8', $loc . '-' . $loc 228 $attempts = cartesian_product_generator([$first, $separators, $second, ['.'], $encodings]);
229 ); 229 } else {
230 $attempts = cartesian_product_generator([$first, $separators, $first, ['.'], $encodings]);
231 }
232 }
233 }
234 setlocale(LC_ALL, implode('implode', iterator_to_array($attempts)));
235}
236
237/**
238 * Build a Generator object representing the cartesian product from given $items.
239 *
240 * Example:
241 * [['a'], ['b', 'c']]
242 * will generate:
243 * [
244 * ['a', 'b'],
245 * ['a', 'c'],
246 * ]
247 *
248 * @param array $items array of array of string
249 *
250 * @return Generator representing the cartesian product of given array.
251 *
252 * @see https://en.wikipedia.org/wiki/Cartesian_product
253 */
254function cartesian_product_generator($items)
255{
256 if (empty($items)) {
257 yield [];
258 }
259 $subArray = array_pop($items);
260 if (empty($subArray)) {
261 return;
262 }
263 foreach (cartesian_product_generator($items) as $item) {
264 foreach ($subArray as $value) {
265 yield $item + [count($item) => $value];
230 } 266 }
231 } 267 }
232 setlocale(LC_ALL, $attempts);
233} 268}
234 269
235/** 270/**
@@ -270,3 +305,33 @@ function normalize_spaces($string)
270{ 305{
271 return preg_replace('/\s{2,}/', ' ', trim($string)); 306 return preg_replace('/\s{2,}/', ' ', trim($string));
272} 307}
308
309/**
310 * Format the date according to the locale.
311 *
312 * Requires php-intl to display international datetimes,
313 * otherwise default format '%c' will be returned.
314 *
315 * @param DateTime $date to format.
316 * @param bool $intl Use international format if true.
317 *
318 * @return bool|string Formatted date, or false if the input is invalid.
319 */
320function format_date($date, $intl = true)
321{
322 if (! $date instanceof DateTime) {
323 return false;
324 }
325
326 if (! $intl || ! class_exists('IntlDateFormatter')) {
327 return strftime('%c', $date->getTimestamp());
328 }
329
330 $formatter = new IntlDateFormatter(
331 setlocale(LC_TIME, 0),
332 IntlDateFormatter::LONG,
333 IntlDateFormatter::LONG
334 );
335
336 return $formatter->format($date);
337}