From: Jeremy Mikola Date: Thu, 20 Sep 2012 11:37:48 +0000 (-0300) Subject: Avoid setting timezones on DateIntervals X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a6a1ef0052026a0e14ae018be60c1faa53c4a865;p=web%2Fkonrad%2Ftwig.git Avoid setting timezones on DateIntervals --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index c789350..8b12fbb 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -388,7 +388,11 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ $format = $date instanceof DateInterval ? $formats[1] : $formats[0]; } - if ($date instanceof DateInterval || $date instanceof DateTime) { + if ($date instanceof DateInterval) { + return $date->format($format); + } + + if ($date instanceof DateTime) { if (null !== $timezone) { $date = clone $date; $date->setTimezone($timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone)); diff --git a/test/Twig/Tests/Fixtures/filters/date.test b/test/Twig/Tests/Fixtures/filters/date.test index ae6d0da..07d0c9e 100644 --- a/test/Twig/Tests/Fixtures/filters/date.test +++ b/test/Twig/Tests/Fixtures/filters/date.test @@ -11,6 +11,7 @@ {{ date2|date }} {{ date2|date('d/m/Y') }} {{ date2|date('d/m/Y H:i:s', 'Europe/Paris') }} +{{ date2|date('d/m/Y H:i:s', timezone1) }} {{ date2|date('d/m/Y H:i:s') }} {{ date3|date }} {{ date3|date('d/m/Y') }} @@ -31,6 +32,7 @@ return array( 'date4' => 1286199900, 'date5' => -86410, 'date6' => new DateTime('2010-10-04 13:45', new DateTimeZone('America/New_York')), + 'timezone1' => new DateTimeZone('America/New_York'), ) --EXPECT-- October 4, 2010 13:45 @@ -43,6 +45,7 @@ UTC October 4, 2010 13:45 04/10/2010 04/10/2010 15:45:00 +04/10/2010 09:45:00 04/10/2010 13:45:00 October 4, 2010 13:45 04/10/2010 diff --git a/test/Twig/Tests/Fixtures/filters/date_interval.test b/test/Twig/Tests/Fixtures/filters/date_interval.test index 2d5adc1..0c8c6f1 100644 --- a/test/Twig/Tests/Fixtures/filters/date_interval.test +++ b/test/Twig/Tests/Fixtures/filters/date_interval.test @@ -3,14 +3,17 @@ --CONDITION-- version_compare(phpversion(), '5.3.0', '>=') --TEMPLATE-- -{{ date6|date }} -{{ date6|date('%d days %h hours') }} +{{ date1|date }} +{{ date1|date('%d days %h hours') }} +{{ date1|date('%d days %h hours', timezone1) }} --DATA-- date_default_timezone_set('UTC'); return array( - 'date5' => -86410, - 'date6' => new DateInterval('P2D'), + 'date1' => new DateInterval('P2D'), + // This should have no effect on DateInterval formatting + 'timezone1' => new DateTimeZone('America/New_York'), ) --EXPECT-- 2 days 2 days 0 hours +2 days 0 hours