From: Benjamin Laugueux Date: Fri, 25 May 2012 19:29:08 +0000 (+0200) Subject: Fixed DateTimeZone support in date filter. X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c53a02de1a1781c356a2620e722a3f0e20ed32fa;p=web%2Fkonrad%2Ftwig.git Fixed DateTimeZone support in date filter. Prevents the DateTime object to be updated when changing the DateTimeZone. Unit tests added. Updated changelog. --- diff --git a/CHANGELOG b/CHANGELOG index eb53dc1..70262eb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ * fixed a regression when using a number in template attributes * fixed compiler when mbstring.func_overload is set to 2 + * fixed DateTimeZone support in date filter * 1.8.1 (2012-05-17) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index e72dc5e..8ddba1a 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -415,28 +415,29 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ */ function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null) { - if ($date instanceof DateTime) { - return $date; - } - - $asString = (string) $date; + if (!$date instanceof DateTime) { + $asString = (string) $date; - if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { - $date = new DateTime('@'.$date); - $date->setTimezone(new DateTimeZone(date_default_timezone_get())); + if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { + $date = new DateTime('@'.$date); + } else { + $date = new DateTime($date); + } } else { - $date = new DateTime($date); + $date = clone $date; } // set Timezone if (null !== $timezone) { - if (!$timezone instanceof DateTimeZone) { - $timezone = new DateTimeZone($timezone); + if ($timezone instanceof DateTimeZone) { + $date->setTimezone($timezone); + } else { + $date->setTimezone(new DateTimeZone($timezone)); } - - $date->setTimezone($timezone); } elseif (($timezone = $env->getExtension('core')->getTimezone()) instanceof DateTimeZone) { $date->setTimezone($timezone); + } else { + $date->setTimezone(new DateTimeZone(date_default_timezone_get())); } return $date; diff --git a/test/Twig/Tests/Fixtures/filters/date.test b/test/Twig/Tests/Fixtures/filters/date.test index 4425746..ae6d0da 100644 --- a/test/Twig/Tests/Fixtures/filters/date.test +++ b/test/Twig/Tests/Fixtures/filters/date.test @@ -4,6 +4,9 @@ {{ date1|date }} {{ date1|date('d/m/Y') }} {{ date1|date('d/m/Y H:i:s', 'Europe/Paris') }} +{{ date1|date('d/m/Y H:i:s P', 'Europe/Paris') }} +{{ date1|date('d/m/Y H:i:s P', 'America/Chicago') }} +{{ date1|date('e') }} {{ date1|date('d/m/Y H:i:s') }} {{ date2|date }} {{ date2|date('d/m/Y') }} @@ -15,6 +18,10 @@ {{ date4|date('d/m/Y') }} {{ date5|date }} {{ 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('e', 'Europe/Paris') }} +{{ date6|date('e') }} --DATA-- date_default_timezone_set('UTC'); return array( @@ -23,11 +30,15 @@ return array( 'date3' => '2010-10-04 13:45', 'date4' => 1286199900, 'date5' => -86410, + 'date6' => new DateTime('2010-10-04 13:45', new DateTimeZone('America/New_York')), ) --EXPECT-- October 4, 2010 13:45 04/10/2010 04/10/2010 15:45:00 +04/10/2010 15:45:00 +02:00 +04/10/2010 08:45:00 -05:00 +UTC 04/10/2010 13:45:00 October 4, 2010 13:45 04/10/2010 @@ -39,3 +50,7 @@ October 4, 2010 13:45 04/10/2010 December 30, 1969 23:59 30/12/1969 +04/10/2010 19:45:00 +02:00 +05/10/2010 01:45:00 +08:00 +Europe/Paris +America/New_York \ No newline at end of file