Skip to content

Commit

Permalink
Fix timezone fallback to CoreExtension in IntlExtension
Browse files Browse the repository at this point in the history
This is probably a regression from twigphp#3844
  • Loading branch information
keulinho committed Nov 3, 2023
1 parent 9f42a76 commit 144c4da
Show file tree
Hide file tree
Showing 2 changed files with 16 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 @@ -371,7 +371,7 @@ public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'm
$date = twig_date_converter($env, $date, $timezone);

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

use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Extension\CoreExtension;
use Twig\Extra\Intl\IntlExtension;
use Twig\Loader\ArrayLoader;

Expand All @@ -30,6 +31,20 @@ public function testFormatterWithoutProto()
);
}

public function testFormatterWithoutProtoFallsBackToCoreExtensionTimezone()
{
$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->assertSame(
'Feb 20, 2020, 3:37:00 PM',
$ext->formatDateTime($env, new \DateTime('2020-02-20T13:37:00+00:00', new \DateTimeZone('UTC')))
);
}

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

0 comments on commit 144c4da

Please sign in to comment.