Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time.here practically useless #28

Open
pschultz opened this issue Aug 4, 2020 · 3 comments
Open

Time.here practically useless #28

pschultz opened this issue Aug 4, 2020 · 3 comments

Comments

@pschultz
Copy link

pschultz commented Aug 4, 2020

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 (it's more like hereAndNow), but ideally it would produce correct local times for all inputs, which is possible to do with plain JavaScript as demonstrated above.

Complete program:

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 (August 2020) in Europe/Berlin, but expected it to be 12.
    in
    p [] [ text <| String.fromInt hour ]
@tsmanuelanton
Copy link

Hi @pschult, I am also having problems with this, did you find any solution? Thank you.

@pschultz
Copy link
Author

@tsmanuelanton, I don't recall what I ended up doing (I don't even know anymore what I was working on, exactly). If I had to guess: I probably used moment.js to do the formatting.

@tsmanuelanton
Copy link

@pschultz thank you for replying so fast. I found the package justinmimbs/time-extra wich does the work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants