justinmimbs/timezone-data/1.0.0

NOTE: the latest version is 11.1.2

timezone-data

This Elm package contains time zone data from the IANA Time Zone Database for using with elm/time.

The elm/time library provides a Posix type for representing an instant in time. Extracting human-readable parts from a Posix time requires a Time.Zone. This library provides such Time.Zone data.

Examples

Get a specific time zone

Data is contained in the TimeZone.Data module, where data for each zone is packaged as a TimeZone.Data.Pack type. A Pack must be unpacked to a Time.Zone with the TimeZone.unpack function.

import Time
import TimeZone
import TimeZone.Data

zone : Time.Zone
zone =
    TimeZone.unpack TimeZone.Data.america__new_york

Each Pack in the TimeZone.Data module is named after its zone name (e.g. America/New_York), where slashes are replaced by __, dashes are replaced by _, and the name is lowercased. For example, America/Port-au-Prince becomes america__port_au_prince.

You can look up a Pack by its zone name in the TimeZone.Data.packs dictionary.

import Dict
import TimeZone.Data

Dict.get "America/New_York" TimeZone.Data.packs

-- Just TimeZone.Data.america__new_york

Get the local time zone

The elm/time library provides a task for getting the local zone name, Time.getZoneName. This library provides a convenience task for getting the local zone, TimeZone.getZone.

type Model
    = Loading
    | Failure TimeZone.Error
    | Success String Time.Zone


type Msg
    = ReceiveTimeZone (Result TimeZone.Error ( String, Time.Zone ))


init : ( Model, Cmd Msg )
init =
    ( Loading
    , TimeZone.getZone |> Task.attempt ReceiveTimeZone
    )


update : Msg -> Model -> ( Model, Cmd Msg )
update (ReceiveTimeZone result) _ =
    ( case result of
        Ok ( zoneName, zone ) ->
            Success zoneName zone

        Err error ->
            Failure error
    , Cmd.none
    )

See this page for a full example.

Alternatives

Using this library to include all time zones in your compiled asset should increase its minified and gzipped size by about 18 KB. For a more lightweight approach to getting time zones into Elm, you may consider fetching only the data you need at runtime. And if your use case is taking UTC timestamps and displaying them nicely formatted in the client's local time, then you may look into using custom elements, like Github's time-elements.