From 56997536b930f6f310ab33f695217020ca7769c8 Mon Sep 17 00:00:00 2001 From: Matt Lehner Date: Tue, 14 Feb 2012 14:52:51 -0500 Subject: [PATCH] added setTimezone to allow globally overriding the timezone when formating dates --- lib/Twig/Extension/Core.php | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f5d2acd..ae25758 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -16,6 +16,7 @@ class Twig_Extension_Core extends Twig_Extension { protected $dateFormats = array('F j, Y H:i', '%d days'); protected $numberFormat = array(0, '.', ','); + protected $timezone = null; /** * Sets the default format to be used by the date filter. @@ -45,6 +46,30 @@ class Twig_Extension_Core extends Twig_Extension } /** + * Sets the default timezone to be used by the date filter. + * + * @param DateTimeZone|string $timezone The default timezone string or a DateTimeZone object + */ + public function setTimezone($timezone) + { + if ($timezone instanceof DateTimeZone) { + $this->timezone = $timezone; + } else { + $this->timezone = new DateTimeZone($timezone); + } + } + + /** + * Gets the default timezone to be used by the date filter. + * + * @return DateTimeZone The default timezone currently in use + */ + public function getTimezone() + { + return $this->timezone; + } + + /** * Sets the default format to be used by the number_format filter. * * @param integer $decimal The number of decimal places to use. @@ -342,6 +367,10 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $ $date->setTimezone($timezone); } + else if (($timezone = $env->getExtension('core')->getTimezone()) instanceof DateTimeZone) + { + $date->setTimezone($timezone); + } return $date->format($format); } -- 1.7.2.5