francodacosta / grav-plugin-page-stats

better page stats for Grav
MIT License
7 stars 4 forks source link

Display page stats in localtime by default and allow configuring a fixed offset #28 #41

Open djyotta opened 8 months ago

djyotta commented 8 months ago

Fixes #28

Summary

Timestamps are stored in db as strings (as far as I can tell), and include the date time offset from UTC.

The Problem

Timestamps were formerly displayed as output of sqlite date() and time() functions, which causes the times to be displayed as UTC regardless of server timezone.

The Solution

Given the following behaviour of sqlite3:

From https://www.sqlite.org/lang_datefunc.html:

PR Scope

This PR supports the sqlite3 +/-HH:MM and localtime modifiers.

  1. The default is localtime - which lets sqlite adjust the time by the the system timezone (ie, will correctly reflect daylight savings time if applicable).
  2. The user may also set an explicit offset from UTC - useful if the server is in a different time zone to the user.

Default value rationale: It is a assumed that typically, the admin user, and site visitors, and server location are (mostly) all in the same timezone.

Use case for fixed offset from UTC: If the user wants times displayed as UTC, then offset +00:00 my be set.

Implementation

When displaying timestamps, first call datetime(date) to get the time in UTC.

Next, call date or time with the user configured modifier.

Any calls to date or time in the GROUP BY clause are similarly adjusted so that bucket counts accurately reflect the displayed values.

This resolves the feature request, by allowing the user to set a "home" timezone (well, fixed offset rather than a zone due to sqlite limitations).

Further, the user may set to localtime, which is the true server time zone which will automatically adjust for daylight saving time if applicable.

Further improvements

Q. What if true timezone support is required - not just fixed UTC offset? A. Due to limitations in sqlite3 builtin date/time methods, we can not adjust timestamps by arbitrary timezone, only by fixed UTC offset or by the system timezone. However, if we were to pass the timestamps as UTC to the front end without calling date or time functions, then we could split the timestamp into date only and time only parts in JavaScript and apply arbitrary timezone adjustments and not just fixed offset.