elm/core

Time.here practically useless

pschultz opened this issue · 3 comments

Time.here produces a timezone with fixed offset. Since time zones are only required when formatting times for humans this is useless for all values other than "right now", because we can't be sure that the offset "here" doesn't change in the next five minutes.

I want to display a time in the future in the browser's local time zone (in my case, Europe/Berlin). This seems to be impossible to do correctly. I understand that browsers don't have an API that allows to compute local times in arbitrary locations, but it is no problem to compute local times in the browser's configured location:

// Noon on December 1st 2020 in Berlin
new Date(1606820400000).getHours() == 12

I would expect Time.millisToPosix 1606820400000 |> Time.toHour tz (where tz is the Zone produced by Time.here) to return 12, but it returns 13 instead. I understand that Time.here produces a Time.Zone with a fixed UTC offset (currently +2 here in Berlin), but here where I am the offset isn't fixed (the offset is going to be +1 in December)! So Time.here is useless for any time on the other side of a DST change.

To summarize, Time.here is at the very least a misnomer, but ideally it would produce correct local times for all inputs, which is possible to do with plain JavaScript as demonstrated above.

Complete program for completeness:

module Main exposing (main)

import Browser
import Html exposing (p, text)
import Task
import Time


type Msg
    = GotTZ Time.Zone


main =
    Browser.element
        { init = init
        , update = update
        , view = view
        , subscriptions = always Sub.none
        }


init : () -> ( Time.Zone, Cmd Msg )
init _ =
    ( Time.utc, Task.perform GotTZ Time.here )


update msg _ =
    case msg of
        GotTZ tz ->
            ( tz, Cmd.none )


view tz =
    let
        hour = 
            Time.millisToPosix 1606820400000 |> Time.toHour tz 
            -- Returns 13 at the time of writing (July 2020) in Europe/Berlin, but expected it to be 12.
    in
    p [] [ text <| String.fromInt hour ]

Thanks for reporting this! To set expectations:

  • Issues are reviewed in batches, so it can take some time to get a response.
  • Ask questions a community forum. You will get an answer quicker that way!
  • If you experience something similar, open a new issue. We like duplicates.

Finally, please be patient with the core team. They are trying their best with limited resources.

This issue belongs in elm/time

Oops. Moved it to elm/time#28. Sorry for the noise.