Skip to content

Commit

Permalink
merged branch blaugueux/master (PR twigphp#736)
Browse files Browse the repository at this point in the history
Commits
-------

c53a02d Fixed DateTimeZone support in date filter.

Discussion
----------

Fixed DateTimeZone support in date filter.

Now $date converted to a DateTime has a DateTimeZone in every case.

---------------------------------------------------------------------------

by fabpot at 2012-05-28T19:20:47Z

Can you add some unit tests to avoid regression to happen later? Thanks.

---------------------------------------------------------------------------

by blaugueux at 2012-05-29T07:52:58Z

@fabpot Here they are.

---------------------------------------------------------------------------

by fabpot at 2012-05-29T10:29:28Z

Great! Can you add a note in the CHANGELOG file and then squash your commit before I merge? Thanks.

---------------------------------------------------------------------------

by blaugueux at 2012-05-29T12:52:22Z

Changelog added. First squash of my life, i cannot say if it's good :(

---------------------------------------------------------------------------

by stof at 2012-05-29T12:55:26Z

it is not. you merged your remote branch instead of forcing the push

---------------------------------------------------------------------------

by blaugueux at 2012-05-29T17:12:05Z

Ok @stof, thanks for your help. I think it's good now.

@fabpot Is it ok for you ?
  • Loading branch information
fabpot committed May 29, 2012
2 parents 78a935b + c53a02d commit 26db550
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -3,6 +3,7 @@
* added the abs filter
* fixed a regression when using a number in template attributes
* fixed compiler when mbstring.func_overload is set to 2
* fixed DateTimeZone support in date filter

* 1.8.1 (2012-05-17)

Expand Down
27 changes: 14 additions & 13 deletions lib/Twig/Extension/Core.php
Expand Up @@ -416,28 +416,29 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
*/
function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null)
{
if ($date instanceof DateTime) {
return $date;
}

$asString = (string) $date;
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()));
if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
$date = new DateTime('@'.$date);
} else {
$date = new DateTime($date);
}
} else {
$date = new DateTime($date);
$date = clone $date;
}

// set Timezone
if (null !== $timezone) {
if (!$timezone instanceof DateTimeZone) {
$timezone = new DateTimeZone($timezone);
if ($timezone instanceof DateTimeZone) {
$date->setTimezone($timezone);
} else {
$date->setTimezone(new DateTimeZone($timezone));
}

$date->setTimezone($timezone);
} elseif (($timezone = $env->getExtension('core')->getTimezone()) instanceof DateTimeZone) {
$date->setTimezone($timezone);
} else {
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
}

return $date;
Expand Down
15 changes: 15 additions & 0 deletions test/Twig/Tests/Fixtures/filters/date.test
Expand Up @@ -4,6 +4,9 @@
{{ date1|date }}
{{ date1|date('d/m/Y') }}
{{ date1|date('d/m/Y H:i:s', 'Europe/Paris') }}
{{ date1|date('d/m/Y H:i:s P', 'Europe/Paris') }}
{{ date1|date('d/m/Y H:i:s P', 'America/Chicago') }}
{{ date1|date('e') }}
{{ date1|date('d/m/Y H:i:s') }}
{{ date2|date }}
{{ date2|date('d/m/Y') }}
Expand All @@ -15,6 +18,10 @@
{{ date4|date('d/m/Y') }}
{{ date5|date }}
{{ date5|date('d/m/Y') }}
{{ date6|date('d/m/Y H:i:s P', 'Europe/Paris') }}
{{ date6|date('d/m/Y H:i:s P', 'Asia/Hong_Kong') }}
{{ date6|date('e', 'Europe/Paris') }}
{{ date6|date('e') }}
--DATA--
date_default_timezone_set('UTC');
return array(
Expand All @@ -23,11 +30,15 @@ return array(
'date3' => '2010-10-04 13:45',
'date4' => 1286199900,
'date5' => -86410,
'date6' => new DateTime('2010-10-04 13:45', new DateTimeZone('America/New_York')),
)
--EXPECT--
October 4, 2010 13:45
04/10/2010
04/10/2010 15:45:00
04/10/2010 15:45:00 +02:00
04/10/2010 08:45:00 -05:00
UTC
04/10/2010 13:45:00
October 4, 2010 13:45
04/10/2010
Expand All @@ -39,3 +50,7 @@ October 4, 2010 13:45
04/10/2010
December 30, 1969 23:59
30/12/1969
04/10/2010 19:45:00 +02:00
05/10/2010 01:45:00 +08:00
Europe/Paris
America/New_York

0 comments on commit 26db550

Please sign in to comment.