I’ve explored two approaches for getting Time.Zone
values to use with elm/time
, both of which use the IANA Time Zone Database (TZ).
To be clear, the use case is that you have Posix
values and you want to extract parts from them in some local time, not just UTC.
Often you have UTC timestamps that you just need to display nicely formatted in the client’s local time zone. For this, it’s possible to push the job out to custom elements, like Github’s time-elements.
The following, however, are ways of getting time zone data into Elm.
1. Fetch only the zone data you need at runtime
Since Elm targets web browsers, this seems a natural fit.
The timezone-json repo contains a script you can use to build a set of JSON files from (the compiled artifacts of) the TZ database. It also contains Elm examples of fetching and decoding them to Time.Zone
values.
2. Include the time zone database in your compiled asset
This sounds a little crazy, but I can imagine use cases for this, and once minified and gzipped it only adds 18 KB.
The timezone-data package allows this. Data in the package is generated by a script that converts (the source files of) the TZ database to Elm source; each zone must be “unpacked” to get a Time.Zone
value. The unpacked Time.Zone
values match exactly those built from the timezone-json
approach above.
With both of these, the Time.Zone
values only cover changes between 1970 and 2037.
Each of the links above should contain examples to get you started, but please let me know if you have questions.