added a way to set the default global date interval format

This commit is contained in:
Fabien Potencier
2011-12-30 09:15:53 +01:00
parent 519b6805b0
commit 736dfa84d0
4 changed files with 34 additions and 15 deletions
+1
View File
@@ -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
+19 -11
View File
@@ -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") }}
* </pre>
*
* @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) {
@@ -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
@@ -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