From ca61facb1a0109f5afb85a4e081211239c7e103c Mon Sep 17 00:00:00 2001 From: hidenorigoto Date: Tue, 15 Oct 2013 18:17:03 +0900 Subject: [PATCH] Added support DateTimeInterface (PHP 5.5) if available --- lib/Twig/Extension/Core.php | 10 +++-- .../Tests/Fixtures/filters/date_immutable.test | 35 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/date_immutable.test diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index e03bb3c..0634b2a 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -485,13 +485,15 @@ function twig_date_converter(Twig_Environment $env, $date = null, $timezone = nu $defaultTimezone = $timezone; } - if ($date instanceof DateTime) { - $date = clone $date; + if ($date instanceof DateTime || $date instanceof DateTimeInterface) { + $returningDate = new DateTime($date->format('c')); if (false !== $timezone) { - $date->setTimezone($defaultTimezone); + $returningDate->setTimezone($defaultTimezone); + } else { + $returningDate->setTimezone($date->getTimezone()); } - return $date; + return $returningDate; } $asString = (string) $date; diff --git a/test/Twig/Tests/Fixtures/filters/date_immutable.test b/test/Twig/Tests/Fixtures/filters/date_immutable.test new file mode 100644 index 0000000..b89ceb3 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/date_immutable.test @@ -0,0 +1,35 @@ +--TEST-- +"date" filter +--CONDITION-- +version_compare(phpversion(), '5.5.0', '>=') +--TEMPLATE-- +{{ date1|date }} +{{ date1|date('d/m/Y') }} +{{ date1|date('d/m/Y H:i:s', 'Asia/Hong_Kong') }} +{{ date1|date('d/m/Y H:i:s', timezone1) }} +{{ date1|date('d/m/Y H:i:s') }} + +{{ date2|date('d/m/Y H:i:s P', 'Europe/Paris') }} +{{ date2|date('d/m/Y H:i:s P', 'Asia/Hong_Kong') }} +{{ date2|date('d/m/Y H:i:s P', false) }} +{{ date2|date('e', 'Europe/Paris') }} +{{ date2|date('e', false) }} +--DATA-- +date_default_timezone_set('Europe/Paris'); +return array( + 'date1' => new DateTimeImmutable('2010-10-04 13:45'), + 'date2' => new DateTimeImmutable('2010-10-04 13:45', new DateTimeZone('America/New_York')), + 'timezone1' => new DateTimeZone('America/New_York'), +) +--EXPECT-- +October 4, 2010 13:45 +04/10/2010 +04/10/2010 19:45:00 +04/10/2010 07:45:00 +04/10/2010 13:45:00 + +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 -- 1.7.2.5