Skip to content

Commit 64ee590

Browse files
committed
bug #4014 Fix exception when timezone is false (Seb33300)
This PR was squashed before being merged into the 3.x branch. Discussion ---------- Fix exception when timezone is false When passing `false` in the timezone param, twig returns an exception: > An exception has been thrown during the rendering of a template ("Twig\Extra\Intl\IntlExtension::createDateFormatter(): Argument 5 ($timezone) must be of type ?DateTimeZone, bool given Passing `false` is supported and allows to skip the timezone conversion. Regression introduced by #3903 Commits ------- 7e8f5eb Fix exception when timezone is false
2 parents 870da7f + 7e8f5eb commit 64ee590

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

extra/intl-extra/IntlExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'm
372372
$date = CoreExtension::dateConverter($env, $date, $timezone);
373373

374374
$formatterTimezone = $timezone;
375-
if (null === $formatterTimezone) {
375+
if (null === $formatterTimezone || false === $formatterTimezone) {
376376
$formatterTimezone = $date->getTimezone();
377377
} elseif (\is_string($formatterTimezone)) {
378378
$formatterTimezone = new \DateTimeZone($timezone);

extra/intl-extra/Tests/IntlExtensionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public function testFormatterWithoutProtoFallsBackToCoreExtensionTimezone()
4545
);
4646
}
4747

48+
public function testFormatterWithoutProtoSkipTimezoneConverter()
49+
{
50+
$ext = new IntlExtension();
51+
$env = new Environment(new ArrayLoader());
52+
// EET is always +2 without changes for daylight saving time
53+
// so it has a fixed difference to UTC
54+
$env->getExtension(CoreExtension::class)->setTimezone('EET');
55+
56+
$this->assertStringStartsWith(
57+
'Feb 20, 2020, 1:37:00',
58+
$ext->formatDateTime($env, new \DateTime('2020-02-20T13:37:00+00:00', new \DateTimeZone('UTC')), 'medium', 'medium', '', false)
59+
);
60+
}
61+
4862
public function testFormatterProto()
4963
{
5064
$dateFormatterProto = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, new \DateTimeZone('Europe/Paris'));

0 commit comments

Comments
 (0)