Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given an IANA Timezone and a local time, parse it into a Maybe Posix?

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.

like image 298
Robert K. Bell Avatar asked Mar 20 '19 23:03

Robert K. Bell


People also ask

What is Posix TZ format?

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).

How do I set UTC time zone in Python?

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.


1 Answers

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"

like image 161
Wex Avatar answered Oct 18 '22 19:10

Wex