From: Fabien Potencier Date: Fri, 30 Dec 2011 08:15:53 +0000 (+0100) Subject: added a way to set the default global date interval format X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=736dfa84d08b2ed873e3cd9c4ef273b125f4e416;p=web%2Fkonrad%2Ftwig.git added a way to set the default global date interval format --- diff --git a/CHANGELOG b/CHANGELOG index 8c742cf..278792a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0-RC2 + * added a way to set the default global date interval format * 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 a03156b..5083412 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -14,27 +14,34 @@ if (!defined('ENT_SUBSTITUTE')) { */ class Twig_Extension_Core extends Twig_Extension { - protected $dateFormat = 'F j, Y H:i'; + protected $dateFormats = array('F j, Y H:i', '%d days'); protected $numberFormat = array(0, '.', ','); /** * Sets the default format to be used by the date filter. * - * @param string $format The default date format string + * @param string $format The default date format string + * @param string $dateIntervalFormat The default date interval format string */ - public function setDateFormat($format) + public function setDateFormat($format = null, $dateIntervalFormat = null) { - $this->dateFormat = $format; + if (null !== $format) { + $this->dateFormats[0] = $format; + } + + if (null !== $dateIntervalFormat) { + $this->dateFormats[1] = $dateIntervalFormat; + } } /** * Gets the default format to be used by the date filter. * - * @return string The default date format string + * @return array The default date format string and the default date interval format string */ public function getDateFormat() { - return $this->dateFormat; + return $this->dateFormats; } /** @@ -300,17 +307,18 @@ function twig_random($values) * {{ post.published_at|date("m/d/Y") }} * * - * @param Twig_Environment $env A Twig_Environment instance - * @param DateTime|string $date A date - * @param string $format A format - * @param DateTimeZone|string $timezone A timezone + * @param Twig_Environment $env A Twig_Environment instance + * @param DateTime|DateInterval|string $date A date + * @param string $format A format + * @param DateTimeZone|string $timezone A timezone * * @return string The formatter date */ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null) { if (null === $format) { - $format = $env->getExtension('core')->getDateFormat(); + $formats = $env->getExtension('core')->getDateFormat(); + $format = $date instanceof DateInterval ? $formats[1] : $formats[0]; } if ($date instanceof DateInterval || $date instanceof DateTime) { diff --git a/test/Twig/Tests/Fixtures/filters/date.test b/test/Twig/Tests/Fixtures/filters/date.test index deb52b2..6f59f20 100644 --- a/test/Twig/Tests/Fixtures/filters/date.test +++ b/test/Twig/Tests/Fixtures/filters/date.test @@ -11,6 +11,8 @@ {{ date4|date('d/m/Y') }} {{ date5|date }} {{ date5|date('d/m/Y') }} +{{ date6|date }} +{{ date6|date('%d days %h hours') }} --DATA-- date_default_timezone_set('UTC'); return array( @@ -19,6 +21,7 @@ return array( 'date3' => '2010-10-04 13:45', 'date4' => 1286199900, 'date5' => -86410, + 'date6' => new DateInterval('P2D'), ) --EXPECT-- October 4, 2010 13:45 @@ -31,3 +34,5 @@ October 4, 2010 13:45 04/10/2010 December 30, 1969 23:59 30/12/1969 +2 days +2 days 0 hours diff --git a/test/Twig/Tests/Fixtures/filters/date_default_format.test b/test/Twig/Tests/Fixtures/filters/date_default_format.test index 5e1d8de..87c7442 100644 --- a/test/Twig/Tests/Fixtures/filters/date_default_format.test +++ b/test/Twig/Tests/Fixtures/filters/date_default_format.test @@ -1,14 +1,19 @@ --TEST-- "date" filter --TEMPLATE-- -{{ date|date }} -{{ date|date('d/m/Y') }} +{{ date1|date }} +{{ date1|date('d/m/Y') }} +{{ date2|date }} +{{ date2|date('%d days') }} --DATA-- date_default_timezone_set('UTC'); -$twig->getExtension('core')->setDateFormat('Y-m-d'); +$twig->getExtension('core')->setDateFormat('Y-m-d', '%d days %h hours'); return array( - 'date' => mktime(13, 45, 0, 10, 4, 2010), + 'date1' => mktime(13, 45, 0, 10, 4, 2010), + 'date2' => new DateInterval('P2D'), ) --EXPECT-- 2010-10-04 04/10/2010 +2 days 0 hours +2 days