Is there a way in Elm to take a local time (such as the string 2019-03-18T09:10:12.4
; no offset specifed) and a timezone (such as Australia/Sydney
) to a possible Posix value (i.e, that time converted to UTC), without using ports?
There's waratuman/time-extra, but it seems to only work on the Date portion. And sadly rtfeldman/elm-iso8601-date-strings doesn't take timezones.
In JS, there's options such as moment-tz and date-fns-timezone, but it would be much simpler to avoid JS interop for frequent date parsing.
The TZ variable specified in POSIX format contains all the information required to identify the time zone, specify when to switch DST on and off, and specify the offset from Coordinated Universal Time (UTC).
Convert local datetime string to UTC in Python Then we will change the timezone of the datetime object to UTC by calling the astimezone() function on the datetime object. The astimezone() function accepts a timezone instance tz as an argument.
If we can assume that all of your dates come without a timezone, and that you always want to cast them to Australia/Sydney
before converting them to Posix, you should be able to just concatenate the offset yourself to create 2019-03-18T09:10:12.4+11:00
:
toPosix : String -> String -> Maybe Posix
toPosix timezone timestampWithoutTimezone =
let
timestampWithTimezone = timestampWithoutTimezone ++ timezone
in
timestampWithTimezone |> toTime |> Result.toMaybe
{- If you always want to use Australia/Sydney -}
localPosix =
toPosix "+11:00"
posix =
localPosix "2019-03-18T09:10:12.4"
{- Custom timezone -}
posix =
toPosix "+11:00" "2019-03-18T09:10:12.4"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With