Skip to content

Commit fab1a37

Browse files
committed
bug #4546 Fix timezone conversion on strings (PrinsFrank)
This PR was squashed before being merged into the 3.x branch. Discussion ---------- Fix timezone conversion on strings When setting a default application timezone, and a display timezone: ```php date_default_timezone_set('UTC'); $twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris'); ``` Date objects get converted, but strings don't. See #4545. For most other paths in `convertDate`, the dateTime object is initialized, and afterwards the timezone gets set. For strings, the timezone is passed as a constructor argument before having the timezone set again, losing its converting behaviour. This makes the convertDate method work identical for DateTime objects as strings. This different behaviour was already spotted in #3568. Fixes #4545 #3568 #2819 Commits ------- f69531f Fix timezone conversion on strings
2 parents 7f12e87 + f69531f commit fab1a37

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/Extension/CoreExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public function convertDate($date = null, $timezone = null)
576576
if (ctype_digit($asString) || ('' !== $asString && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
577577
$date = new \DateTime('@'.$date);
578578
} else {
579-
$date = new \DateTime($date, $this->getTimezone());
579+
$date = new \DateTime($date);
580580
}
581581

582582
if (false !== $timezone) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
--TEST--
2+
"date" filter with time zone conversion
3+
--TEMPLATE--
4+
{{ date1|date }}
5+
{{ date1|date('d/m/Y') }}
6+
{{ date1|date('d/m/Y H:i:s', 'Asia/Hong_Kong') }}
7+
{{ date1|date('d/m/Y H:i:s P', 'Asia/Hong_Kong') }}
8+
{{ date1|date('d/m/Y H:i:s P', 'America/Chicago') }}
9+
{{ date1|date('e') }}
10+
{{ date1|date('d/m/Y H:i:s') }}
11+
12+
{{ date2|date }}
13+
{{ date2|date('d/m/Y') }}
14+
{{ date2|date('d/m/Y H:i:s', 'Asia/Hong_Kong') }}
15+
{{ date2|date('d/m/Y H:i:s', timezone1) }}
16+
{{ date2|date('d/m/Y H:i:s') }}
17+
18+
{{ date3|date }}
19+
{{ date3|date('d/m/Y') }}
20+
21+
{{ date4|date }}
22+
{{ date4|date('d/m/Y') }}
23+
24+
{{ date5|date }}
25+
{{ date5|date('d/m/Y') }}
26+
27+
{{ date6|date('d/m/Y H:i:s P', 'Europe/Paris') }}
28+
{{ date6|date('d/m/Y H:i:s P', 'Asia/Hong_Kong') }}
29+
{{ date6|date('d/m/Y H:i:s P', false) }}
30+
{{ date6|date('e', 'Europe/Paris') }}
31+
{{ date6|date('e', false) }}
32+
33+
{{ date7|date }}
34+
{{ date7|date(timezone='Europe/Paris') }}
35+
{{ date7|date(timezone='Asia/Hong_Kong') }}
36+
{{ date7|date(timezone=false) }}
37+
{{ date7|date(timezone='Indian/Mauritius') }}
38+
39+
{{ '2010-01-28 15:00:00'|date(timezone="Europe/Paris") }}
40+
{{ '2010-01-28 15:00:00'|date(timezone="Asia/Hong_Kong") }}
41+
--DATA--
42+
date_default_timezone_set('Europe/Paris');
43+
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('UTC');
44+
return [
45+
'date1' => mktime(13, 45, 0, 10, 4, 2010),
46+
'date2' => new \DateTime('2010-10-04 13:45'),
47+
'date3' => '2010-10-04 13:45',
48+
'date4' => 1286199900, // \DateTime::createFromFormat('Y-m-d H:i', '2010-10-04 13:45', new \DateTimeZone('UTC'))->getTimestamp() -- A unixtimestamp is always GMT
49+
'date5' => -189291360, // \DateTime::createFromFormat('Y-m-d H:i', '1964-01-02 03:04', new \DateTimeZone('UTC'))->getTimestamp(),
50+
'date6' => new \DateTime('2010-10-04 13:45', new \DateTimeZone('America/New_York')),
51+
'date7' => '2010-01-28T15:00:00+04:00',
52+
'timezone1' => new \DateTimeZone('America/New_York'),
53+
]
54+
--EXPECT--
55+
October 4, 2010 11:45
56+
04/10/2010
57+
04/10/2010 19:45:00
58+
04/10/2010 19:45:00 +08:00
59+
04/10/2010 06:45:00 -05:00
60+
UTC
61+
04/10/2010 11:45:00
62+
63+
October 4, 2010 11:45
64+
04/10/2010
65+
04/10/2010 19:45:00
66+
04/10/2010 07:45:00
67+
04/10/2010 11:45:00
68+
69+
October 4, 2010 11:45
70+
04/10/2010
71+
72+
October 4, 2010 13:45
73+
04/10/2010
74+
75+
January 2, 1964 03:04
76+
02/01/1964
77+
78+
04/10/2010 19:45:00 +02:00
79+
05/10/2010 01:45:00 +08:00
80+
04/10/2010 13:45:00 -04:00
81+
Europe/Paris
82+
America/New_York
83+
84+
January 28, 2010 11:00
85+
January 28, 2010 12:00
86+
January 28, 2010 19:00
87+
January 28, 2010 15:00
88+
January 28, 2010 15:00
89+
90+
January 28, 2010 15:00
91+
January 28, 2010 22:00

0 commit comments

Comments
 (0)