From ce196a1da5d9bbe8860712f93416d99419643739 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 14 Feb 2021 15:11:25 +0100 Subject: [PATCH] Add optional "timezone" parameter to date helper --- ChangeLog.md | 3 ++- README.md | 1 + .../php/web/frontend/helpers/Dates.class.php | 9 ++++---- .../web/frontend/unittest/DatesTest.class.php | 23 +++++++++++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index f007967..69b4cc2 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -4,7 +4,8 @@ Handlebars for XP web frontends change log ## ?.?.? / ????-??-?? * Implemented basic timezone handling by passing default timezone to - the `web.frontend.helpers.Dates` helpers extension. + the `web.frontend.helpers.Dates` helpers extension, overwriteable by + passing e.g. `timezone="America/New_York"` in the handlebars helper. (@thekid) * Merged PR #4: Make template engine extensible; and extract the hard wired date formatting to its own extension diff --git a/README.md b/README.md index d08f5b9..850954f 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ The `date` helper accepts anything the `util.Date` class accepts as constructor {{date created}} {{date created format="d.m.Y"}} {{date created format="us:short"}} +{{date created timezone="America/New_York"}} ``` ### Logging diff --git a/src/main/php/web/frontend/helpers/Dates.class.php b/src/main/php/web/frontend/helpers/Dates.class.php index 5205b9d..38a8ef0 100755 --- a/src/main/php/web/frontend/helpers/Dates.class.php +++ b/src/main/php/web/frontend/helpers/Dates.class.php @@ -23,14 +23,15 @@ public function helpers() { yield 'date' => function($in, $context, $options) { static $resolution= ['s' => 1, 'ms' => 1000]; + $tz= isset($options['timezone']) ? TimeZone::getByName($options['timezone']) : $this->timezone; if (!isset($options[0])) { - $d= Date::now($this->timezone); + $d= Date::now($tz); } else if ($options[0] instanceof Date) { - $d= $this->timezone->translate($options[0]); + $d= $tz->translate($options[0]); } else if ($r= $options['timestamp'] ?? null) { - $d= new Date('@'.(int)($options[0] / $resolution[$r]), $this->timezone); + $d= new Date('@'.(int)($options[0] / $resolution[$r]), $tz); } else { - $d= $this->timezone->translate(new Date($options[0])); + $d= $tz->translate(new Date($options[0])); } return $d->toString($this->formats[$options['format'] ?? null] ?? $options['format']); diff --git a/src/test/php/web/frontend/unittest/DatesTest.class.php b/src/test/php/web/frontend/unittest/DatesTest.class.php index 5cafb9f..a15a344 100755 --- a/src/test/php/web/frontend/unittest/DatesTest.class.php +++ b/src/test/php/web/frontend/unittest/DatesTest.class.php @@ -12,15 +12,16 @@ class DatesTest extends HandlebarsTest { protected function extensions() { return [new Dates(new TimeZone(self::TZ), [ null => self::FORMAT, - 'us:short' => 'Y-m-d', + 'us:short' => 'm/d/Y', ])]; } /** @return iterable */ private function dates() { - yield new Date('2021-02-13'); - yield '13.02.2021'; - yield 1613209181; + yield new Date('2021-02-13 22:30:00', new TimeZone(self::TZ)); + yield new Date('13.02.2021 12:30:00+01:00'); + yield '2021-02-13T11:30:00Z'; + yield 1613215800; } #[Test, Values('dates')] @@ -34,7 +35,7 @@ public function dates_with_format($date) { #[Test, Values('dates')] public function dates_with_named_format($date) { Assert::equals( - '2021-02-13', + '02/13/2021', $this->transform('{{date tested format="us:short"}}', ['tested' => $date]) ); } @@ -57,11 +58,19 @@ public function timestamp_resolution($template) { Assert::equals('13.02.2021', $this->transform($template)); } - #[Test, Values(eval: '[new Date("13.02.2021 11:30:00", new TimeZone("UTC")), "13.02.2021 11:30:00 UTC", 1613215800]')] - public function converted_to_supplied_timezone($date) { + #[Test, Values('dates')] + public function converted_to_default_timezone($date) { Assert::equals( '13.02.2021 22:30:00', $this->transform('{{date tested format="d.m.Y H:i:s"}}', ['tested' => $date]) ); } + + #[Test, Values('dates')] + public function converted_to_supplied_timezone($date) { + Assert::equals( + '13.02.2021 06:30:00', + $this->transform('{{date tested format="d.m.Y H:i:s" timezone="America/New_York"}}', ['tested' => $date]) + ); + } } \ No newline at end of file