From f99504ae02a57f109b7c28039e2109b419298a9c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 30 Oct 2012 10:04:25 +0100 Subject: [PATCH] changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone --- CHANGELOG | 1 + doc/filters/date.rst | 38 +++++++++++++++++++-------- lib/Twig/Extension/Core.php | 32 ++++++++--------------- test/Twig/Tests/Fixtures/filters/date.test | 6 +++- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bbb7fdc..5004adb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.11.0 (2012-XX-XX) + * changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone * fixed default timezone usage for the date function * optimized the way Twig exceptions are managed (to make them faster) * added Twig_ExistsLoaderInterface (implementing this interface in your loader make the chain loader much faster) diff --git a/doc/filters/date.rst b/doc/filters/date.rst index 7271aea..39ad732 100644 --- a/doc/filters/date.rst +++ b/doc/filters/date.rst @@ -10,6 +10,9 @@ .. versionadded:: 1.6.1 The default timezone support has been added in Twig 1.6.1. +.. versionadded:: 1.11.0 + The introduction of the false value for the timezone was introduced in Twig 1.11.0 + The ``date`` filter formats a date to a given format: .. code-block:: jinja @@ -24,17 +27,20 @@ instance, to display the current date, filter the word "now": {{ "now"|date("m/d/Y") }} -To escape words and characters in the date format use ``\\`` in front of each character: +To escape words and characters in the date format use ``\\`` in front of each +character: .. code-block:: jinja {{ post.published_at|date("F jS \\a\\t g:ia") }} -You can also specify a timezone: +If the value passed to the ``date`` filter is ``null``, it will return the +current date by default. If an empty string is desired instead of the current +date, use a ternary operator: .. code-block:: jinja - {{ post.published_at|date("m/d/Y", "Europe/Paris") }} + {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }} If no format is provided, Twig will use the default one: ``F j, Y H:i``. This default can be easily changed by calling the ``setDateFormat()`` method on the @@ -46,20 +52,30 @@ dates and the second one is the default format for date intervals: $twig = new Twig_Environment($loader); $twig->getExtension('core')->setDateFormat('d/m/Y', '%d days'); -The default timezone can also be set globally by calling ``setTimezone()``: +Timezone +-------- -.. code-block:: php +By default, the date is displayed by applying the default timezone (the one +specified in php.ini or declared in Twig -- see below), but you can override +it by explicitly specifying a timezone: - $twig = new Twig_Environment($loader); - $twig->getExtension('core')->setTimezone('Europe/Paris'); +.. code-block:: jinja -If the value passed to the ``date`` filter is ``null``, it will return the -current date by default. If an empty string is desired instead of the current -date, use a ternary operator: + {{ post.published_at|date("m/d/Y", "Europe/Paris") }} + +If the date is already a DateTime object, and if you want to keep its current +timezone, pass ``false`` as the timezone value: .. code-block:: jinja - {{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }} + {{ post.published_at|date("m/d/Y", false) }} + +The default timezone can also be set globally by calling ``setTimezone()``: + +.. code-block:: php + + $twig = new Twig_Environment($loader); + $twig->getExtension('core')->setTimezone('Europe/Paris'); .. _`strtotime`: http://www.php.net/strtotime .. _`DateTime`: http://www.php.net/DateTime diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index b969d2f..1f4ed21 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -397,15 +397,6 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ return $date->format($format); } - if ($date instanceof DateTime) { - if (null !== $timezone) { - $date = clone $date; - $date->setTimezone($timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone)); - } - - return $date->format($format); - } - return twig_date_converter($env, $date, $timezone)->format($format); } @@ -424,12 +415,7 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ */ function twig_date_modify_filter(Twig_Environment $env, $date, $modifier) { - if ($date instanceof DateTime) { - $date = clone $date; - } else { - $date = twig_date_converter($env, $date); - } - + $date = twig_date_converter($env, $date, false); $date->modify($modifier); return $date; @@ -453,15 +439,19 @@ function twig_date_modify_filter(Twig_Environment $env, $date, $modifier) function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null) { // determine the timezone - if (null === $timezone) { - $timezone = $env->getExtension('core')->getTimezone(); + if (!$timezone) { + $defaultTimezone = $env->getExtension('core')->getTimezone(); } elseif (!$timezone instanceof DateTimeZone) { - $timezone = new DateTimeZone($timezone); + $defaultTimezone = new DateTimeZone($timezone); + } else { + $defaultTimezone = $timezone; } if ($date instanceof DateTime) { $date = clone $date; - $date->setTimezone($timezone); + if (false !== $timezone) { + $date->setTimezone($defaultTimezone); + } return $date; } @@ -469,12 +459,12 @@ function twig_date_converter(Twig_Environment $env, $date = null, $timezone = nu $asString = (string) $date; if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { $date = new DateTime('@'.$date); - $date->setTimezone($timezone); + $date->setTimezone($defaultTimezone); return $date; } - return new DateTime($date, $timezone); + return new DateTime($date, $defaultTimezone); } /** diff --git a/test/Twig/Tests/Fixtures/filters/date.test b/test/Twig/Tests/Fixtures/filters/date.test index 07d0c9e..f40f4cb 100644 --- a/test/Twig/Tests/Fixtures/filters/date.test +++ b/test/Twig/Tests/Fixtures/filters/date.test @@ -21,8 +21,9 @@ {{ date5|date('d/m/Y') }} {{ date6|date('d/m/Y H:i:s P', 'Europe/Paris') }} {{ date6|date('d/m/Y H:i:s P', 'Asia/Hong_Kong') }} +{{ date6|date('d/m/Y H:i:s P', false) }} {{ date6|date('e', 'Europe/Paris') }} -{{ date6|date('e') }} +{{ date6|date('e', false) }} --DATA-- date_default_timezone_set('UTC'); return array( @@ -55,5 +56,6 @@ December 30, 1969 23:59 30/12/1969 04/10/2010 19:45:00 +02:00 05/10/2010 01:45:00 +08:00 +04/10/2010 13:45:00 -04:00 Europe/Paris -America/New_York \ No newline at end of file +America/New_York -- 1.7.2.5