Skip to content

Commit

Permalink
|localDate respects the time zone set in PHP [Closes #380]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 4, 2024
1 parent 2c91fe9 commit 6f5a699
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Latte/Essential/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ public function localDate(
$xlt = ['' => \IntlDateFormatter::NONE, 'full' => \IntlDateFormatter::FULL, 'long' => \IntlDateFormatter::LONG, 'medium' => \IntlDateFormatter::MEDIUM, 'short' => \IntlDateFormatter::SHORT,
'relative-full' => \IntlDateFormatter::RELATIVE_FULL, 'relative-long' => \IntlDateFormatter::RELATIVE_LONG, 'relative-medium' => \IntlDateFormatter::RELATIVE_MEDIUM, 'relative-short' => \IntlDateFormatter::RELATIVE_SHORT];
$date ??= $time === null ? 'long' : null;
$options = [$xlt[$date], $xlt[$time]];
$formatter = new \IntlDateFormatter($this->locale, $xlt[$date], $xlt[$time]);
} else {
$options = (new \IntlDatePatternGenerator($this->locale))->getBestPattern($format);
$formatter = new \IntlDateFormatter($this->locale, pattern: (new \IntlDatePatternGenerator($this->locale))->getBestPattern($format));
}

$res = \IntlDateFormatter::formatObject($value, $options, $this->locale);
$res = $formatter->format($value);
$res = preg_replace('~(\d\.) ~', "\$1\u{a0}", $res);
return $res;
}
Expand Down
13 changes: 12 additions & 1 deletion tests/filters/localDate.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ test('edge cases', function () {
});


test('timestamp & zone', function () {
test('PHP timezone', function () {
$filters = new Filters;
$filters->locale = 'cs_CZ';

date_default_timezone_set('Europe/Prague');
$now = new DateTime('2024-10-01 21:37:45 UTC');
Assert::same('23:37:45 SELČ', $filters->localDate($now, 'Hmmssz'));
Assert::same('23:37', $filters->localDate($now, time: 'short'));
});


test('timestamp & timezone', function () {
$filters = new Filters;
$filters->locale = 'cs_CZ';

Expand Down

0 comments on commit 6f5a699

Please sign in to comment.