Skip to content

Commit

Permalink
Add optional "timezone" parameter to date helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Feb 14, 2021
1 parent a9f25f6 commit ce196a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/main/php/web/frontend/helpers/Dates.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
23 changes: 16 additions & 7 deletions src/test/php/web/frontend/unittest/DatesTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand All @@ -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])
);
}
Expand All @@ -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])
);
}
}

0 comments on commit ce196a1

Please sign in to comment.