Skip to content

Commit

Permalink
bug #4014 Fix exception when timezone is false (Seb33300)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Apr 3, 2024
2 parents 870da7f + 7e8f5eb commit 64ee590
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion extra/intl-extra/IntlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'm
$date = CoreExtension::dateConverter($env, $date, $timezone);

$formatterTimezone = $timezone;
if (null === $formatterTimezone) {
if (null === $formatterTimezone || false === $formatterTimezone) {
$formatterTimezone = $date->getTimezone();
} elseif (\is_string($formatterTimezone)) {
$formatterTimezone = new \DateTimeZone($timezone);
Expand Down
14 changes: 14 additions & 0 deletions extra/intl-extra/Tests/IntlExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public function testFormatterWithoutProtoFallsBackToCoreExtensionTimezone()
);
}

public function testFormatterWithoutProtoSkipTimezoneConverter()
{
$ext = new IntlExtension();
$env = new Environment(new ArrayLoader());
// EET is always +2 without changes for daylight saving time
// so it has a fixed difference to UTC
$env->getExtension(CoreExtension::class)->setTimezone('EET');

$this->assertStringStartsWith(
'Feb 20, 2020, 1:37:00',
$ext->formatDateTime($env, new \DateTime('2020-02-20T13:37:00+00:00', new \DateTimeZone('UTC')), 'medium', 'medium', '', false)
);
}

public function testFormatterProto()
{
$dateFormatterProto = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, new \DateTimeZone('Europe/Paris'));
Expand Down

0 comments on commit 64ee590

Please sign in to comment.