From 519b6805b043c6c82338b4e534d7c2b2c7a638b9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Dec 2011 08:58:12 +0100 Subject: [PATCH] refactored date filter code --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 777ab65..8c742cf 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0-RC2 + * fixed the date filter for DateInterval instances (setTimezone() does not exist for them) * refactored Twig_Template::display() to ease its extension * added a number_format filter diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f521523..a03156b 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -313,24 +313,26 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ $format = $env->getExtension('core')->getDateFormat(); } - if (!$date instanceof DateInterval) { - 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())); - } else { - $date = new DateTime($date); - } - } + if ($date instanceof DateInterval || $date instanceof DateTime) { + return $date->format($format); + } - if (null !== $timezone) { - if (!$timezone instanceof DateTimeZone) { - $timezone = new DateTimeZone($timezone); - } + // convert to a 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())); + } else { + $date = new DateTime($date); + } - $date->setTimezone($timezone); + // set Timezone + if (null !== $timezone) { + if (!$timezone instanceof DateTimeZone) { + $timezone = new DateTimeZone($timezone); } + + $date->setTimezone($timezone); } return $date->format($format); -- 1.7.2.5