From e3830c70aa9ae2b0eb89449b548a4fd00842a493 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 30 Oct 2012 09:53:04 +0100 Subject: [PATCH] fixed default timezone usage for the date function (refactor of the previous merge) --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 44 +++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 614b362..bbb7fdc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.11.0 (2012-XX-XX) + * 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/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 189bc0d..372e40d 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -62,6 +62,10 @@ class Twig_Extension_Core extends Twig_Extension */ public function getTimezone() { + if (null === $this->timezone) { + $this->timezone = new DateTimeZone(date_default_timezone_get()); + } + return $this->timezone; } @@ -444,33 +448,29 @@ function twig_date_modify_filter(Twig_Environment $env, $date, $modifier) */ function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null) { - if (!$date instanceof DateTime) { - $asString = (string) $date; - $defaultTimeZone = $env->getExtension('core')->getTimezone(); - $defaultTimeZone = $defaultTimeZone !== null ? $defaultTimeZone : new DateTimeZone(date_default_timezone_get()); - if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { - $date = new DateTime('@'.$date, $defaultTimeZone); - } else { - $date = new DateTime($date, $defaultTimeZone); - } - } else { + // determine the timezone + if (null === $timezone) { + $timezone = $env->getExtension('core')->getTimezone(); + } elseif (!$timezone instanceof DateTimeZone) { + $timezone = new DateTimeZone($timezone); + } + + if ($date instanceof DateTime) { $date = clone $date; + $date->setTimezone($timezone); + + return $date; } - // set Timezone - if (null !== $timezone) { - if ($timezone instanceof DateTimeZone) { - $date->setTimezone($timezone); - } else { - $date->setTimezone(new DateTimeZone($timezone)); - } - } elseif ($defaultTimeZone instanceof DateTimeZone) { - $date->setTimezone($defaultTimeZone); - } else { - $date->setTimezone(new DateTimeZone($defaultTimeZone)); + $asString = (string) $date; + if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) { + $date = new DateTime('@'.$date); + $date->setTimezone($timezone); + + return $date; } - return $date; + return new DateTime($date, $timezone); } /** -- 1.7.2.5