Skip to content

Commit

Permalink
Add support for milliseconds resolution in timestamps to date helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Feb 13, 2021
1 parent 3bb39d7 commit a0bed83
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Handlebars for XP web frontends change log

## ?.?.? / ????-??-??

## 0.3.0 / 2021-02-13

* Add support for milliseconds resolution in timestamps to `date` helper,
thus being able to use JavaScript timestamps.
(@thekid)

## 0.2.0 / 2021-02-13

* Add `any`, `none` and `all` helpers - @thekid
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ On top of the [built-in functionality in Handlebars](https://github.com/xp-forge
* `none`: Test whether none of the given arguments is truthy
* `all`: Test whether all of the given arguments is truthy
* `date`: Transforms dates and timestamps

### Date handling

The `date` helper accepts anything the `util.Date` class accepts as constructor argument, or a `util.Date` instance itself. To format the date, the `format` argument can be used with anything the `util.Date::toString()` method accepts. Here are some examples:

```handlebars
{{date "2021-02-13"}}
{{date "13.02.2021 17:56:00"}}
{{date 1613209181}}
{{date 1613209181279 timestamp="ms"}}
{{date created}}
{{date created format="d.m.Y"}}
```
4 changes: 4 additions & 0 deletions src/main/php/web/frontend/Handlebars.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ public function __construct($templates) {
return empty($options) ? 0 : 1;
})
->withHelper('date', function($in, $context, $options) {
static $resolution= ['s' => 1, 'ms' => 1000];

if (!isset($options[0])) {
$d= Date::now();
} else if ($options[0] instanceof Date) {
$d= $options[0];
} else if ($r= $options['timestamp'] ?? null) {
$d= new Date('@'.($options[0] / $resolution[$r]));
} else {
$d= new Date($options[0]);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/php/web/frontend/unittest/HandlebarsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public function current_date($template) {
Assert::equals(Date::now()->toString('d.m.Y'), $this->transform($template));
}

#[Test, Values(['{{date 1613235019279 timestamp="ms"}}', '{{date 1613235019 timestamp="s"}}'])]
public function timestamp_resolution($template) {
Assert::equals('13.02.2021', $this->transform($template));
}

#[Test, Values([['', 0], ['null', 0], ['"test"', 1], ['null "test"', 1], ['numbers', 1], ['empty', 0]])]
public function any($expr, $expected) {
Assert::equals((string)$expected, $this->transform('{{any '.$expr.'}}', [
Expand Down

0 comments on commit a0bed83

Please sign in to comment.