From 2143858338b31631158b33462e63dbc884d3abe2 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 6 Jul 2020 10:47:22 +0200 Subject: [PATCH 1/2] Put `localedatetime` with previous format and deprecated notice. User `localdate` with new format --- src/Twig/LocaleExtension.php | 37 +++++++++++++++++++++-- templates/_macro/_macro.html.twig | 2 +- templates/_partials/fields/date.html.twig | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Twig/LocaleExtension.php b/src/Twig/LocaleExtension.php index c5b691721..ea9c1d044 100644 --- a/src/Twig/LocaleExtension.php +++ b/src/Twig/LocaleExtension.php @@ -47,7 +47,7 @@ public function getFilters(): array return [ new TwigFilter('localedatetime', [$this, 'localedatetime'], $safe), - new TwigFilter('localdate', [$this, 'localedatetime'], $safe), + new TwigFilter('localdate', [$this, 'localedate'], $safe), ]; } @@ -121,9 +121,42 @@ public function flag($localeCode): string } /** + * @deprecated + * * @param string|\DateTime $dateTime */ - public function localedatetime($dateTime, ?string $format = null, ?string $locale = null): string + public function localedatetime($dateTime, string $format = '%B %e, %Y %H:%M', ?string $locale = '0'): string + { + if (! $dateTime instanceof \DateTime) { + $dateTime = new \DateTime((string) $dateTime); + } + + // Check for Windows to find and replace the %e modifier correctly + // @see: http://php.net/strftime + $os = mb_strtoupper(mb_substr(PHP_OS, 0, 3)); + $format = $os !== 'WIN' ? $format : preg_replace('#(?getTimestamp(); + + // According to http://php.net/manual/en/function.setlocale.php manual + // if the second parameter is "0", the locale setting is not affected, + // only the current setting is returned. + $result = setlocale(LC_ALL, $locale); + + if ($result === false) { + // This shouldn't occur, but.. Dude! + // You ain't even got locale or English on your platform?? + // Various things we could do. We could fail miserably, but a more + // graceful approach is to use the datetime to display a default + // format + // $this->systemLogger->error('No valid locale detected. Fallback on DateTime active.', ['event' => 'system']); + + return $dateTime->format('Y-m-d H:i:s'); + } + + return strftime($format, $timestamp); + } + + public function localdate($dateTime, ?string $format = null, ?string $locale = null): string { if ($dateTime instanceof \Datetime) { $dateTime = Carbon::createFromTimestamp($dateTime->getTimestamp(), $dateTime->getTimezone()); diff --git a/templates/_macro/_macro.html.twig b/templates/_macro/_macro.html.twig index b5cda8402..09eb343db 100644 --- a/templates/_macro/_macro.html.twig +++ b/templates/_macro/_macro.html.twig @@ -77,7 +77,7 @@ {% macro relative_datetime(datetime) %}{% apply spaceless %} - + {{- datetime|date('c') -}} {% endapply %}{% endmacro %} diff --git a/templates/_partials/fields/date.html.twig b/templates/_partials/fields/date.html.twig index 6755cc398..c363f95c8 100644 --- a/templates/_partials/fields/date.html.twig +++ b/templates/_partials/fields/date.html.twig @@ -36,7 +36,7 @@ :errormessage='{{ errormessage|json_encode }}' > {% else %} - {{ value|localedatetime(format|default(null), locale|default(null)) }} + {{ value|localdate(format|default(null), locale|default(null)) }} ({{ macro.relative_datetime(value) }}) {% endif %} From 1fb172bf71dd8a42af4c26ad6d7f71f845da2cf1 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 6 Jul 2020 11:27:34 +0200 Subject: [PATCH 2/2] Fix filter names --- src/Twig/LocaleExtension.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Twig/LocaleExtension.php b/src/Twig/LocaleExtension.php index ea9c1d044..311201436 100644 --- a/src/Twig/LocaleExtension.php +++ b/src/Twig/LocaleExtension.php @@ -47,7 +47,8 @@ public function getFilters(): array return [ new TwigFilter('localedatetime', [$this, 'localedatetime'], $safe), - new TwigFilter('localdate', [$this, 'localedate'], $safe), + new TwigFilter('localedate', [$this, 'localedatetime'], $safe), + new TwigFilter('localdate', [$this, 'localdate'], $safe), ]; }