Prevents the DateTime object to be updated when changing the DateTimeZone.
Unit tests added.
Updated changelog.
* 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)
*/
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;
{{ 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') }}
{{ 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(
'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
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
\ No newline at end of file