From a3310e75a1d97e969279d23607b2339b1f1a8731 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 15 Mar 2012 17:24:51 +0100 Subject: [PATCH] fixed the date filter when a DateTime instance is passed with a specific timezone (closes #660) --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 4 ++++ test/Twig/Tests/Fixtures/filters/date.test | 4 ++++ 3 files changed, 9 insertions(+), 0 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 92313b7..195f335 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.7.0 (2012-XX-XX) + * fixed the date filter when a DateTime instance is passed with a specific timezone * added a trim filter * 1.6.1 (2012-02-29) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 81bbb1f..bdf25ff 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -380,6 +380,10 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ } if ($date instanceof DateInterval || $date instanceof DateTime) { + if (null !== $timezone) { + $date->setTimezone($timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone)); + } + return $date->format($format); } diff --git a/test/Twig/Tests/Fixtures/filters/date.test b/test/Twig/Tests/Fixtures/filters/date.test index deb52b2..edfe596 100644 --- a/test/Twig/Tests/Fixtures/filters/date.test +++ b/test/Twig/Tests/Fixtures/filters/date.test @@ -3,8 +3,10 @@ --TEMPLATE-- {{ date1|date }} {{ date1|date('d/m/Y') }} +{{ date1|date('d/m/Y H:i:s', 'Europe/Paris') }} {{ date2|date }} {{ date2|date('d/m/Y') }} +{{ date2|date('d/m/Y H:i:s', 'Europe/Paris') }} {{ date3|date }} {{ date3|date('d/m/Y') }} {{ date4|date }} @@ -23,8 +25,10 @@ return array( --EXPECT-- October 4, 2010 13:45 04/10/2010 +04/10/2010 15:45:00 October 4, 2010 13:45 04/10/2010 +04/10/2010 15:45:00 October 4, 2010 13:45 04/10/2010 October 4, 2010 13:45 -- 1.7.2.5