xp-forge / handlebars-templates

Handlebars templates for XP web frontends
1 stars 0 forks source link

Timezone handling #3

Closed thekid closed 3 years ago

thekid commented 3 years ago

Currently, dates are displayed as-is in the frontend. However, the user viewing the frontend may be in a different timezone, and in case we're using timestamps, most commonly will be.

One way would be to pass the timezone as optional argument:

{{date created timezone="Europe/Berlin"}}
{{date created timezone=user.tz}}

However, a default timezone may be a good idea. It could cascade as follows:

thekid commented 3 years ago

See also:

thekid commented 3 years ago

Idea:

$local= TimeZone::getLocal();

$templates= new Handlebars($path);
$templates->output(
  timezone: fn($req) => $req->value('user')['tz'] ?? $local,
  dateformats: ['day' => 'd.m.Y']
);
thekid commented 3 years ago

Modern browsers support this:

Intl.DateTimeFormat().resolvedOptions().timeZone;
// "Europe/Berlin" in my case

Source: https://stackoverflow.com/questions/13/determine-a-users-timezone

This could be transmitted to the server in a cookie, by redirect or by XHR but would not work for the first request to the page, thus requiring a redirect.

thekid commented 3 years ago

Idea based on use method which accepts a collection of helpers:

$local= TimeZone::getLocal();

$templates= new Handlebars($path);
$templates->use(new Dates(
  timezone : fn($req) => $req->value('user')['tz'] ?? $local,
  formats  : ['date' => 'd.m.Y', 'time' => 'H:i:s', 'datetime' => 'd.m.Y H:i'],
));
thekid commented 3 years ago

Basic functionality of using a server-side default timezone implemented by #4 , xp-forge/handlebars-templates@de77af5 and xp-forge/handlebars-templates@ce196a1 - extracting the timezone from the request will be handled separately.