-
Notifications
You must be signed in to change notification settings - Fork 30
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
Parsing ISO 8601 / RFC 3339 datetime string? #44
Comments
I think at the moment writing a parser using |
@Boscop you could build multiple formats and use myParser
= try (unformatParser format1)
<|> try (unformatParser format2)
<|> unformatParser format3
parse str = runParser str myParser
Also you can just use parse str
= unformat format1 str
<|> unformat format2 str
<|> unformat format3 str |
@safareli Thanks. But I also need support for microseconds like Also, is there a way that I only have to parse the format string once at the first use, and then not on subsequent uses? With a lazy variable somehow? |
There'll be a bit of a problem there since the You could just create the format string at the top level and re-use it, then the parse cost is at startup. |
@garyb But how can I make it re-use the evaluated value? fmt_rfc3339 = parseFormatString "YYYY-MM-DDTHH:mm:ss+00:00"
fmt_german = parseFormatString "DD.MM.YYYY, HH:mm"
humanTime s = either id id do
decode <- fmt_rfc3339
encode <- fmt_german
datetime <- unformat decode s
pure $ format encode datetime Is that the most efficient way to do it?
That's ok, it can round to the nearest millisecond.. Or even just truncate/ignore them. It should still be able to parse it though.. :) |
Yes fmt_rfc3339 :: Format
fmt_rfc3339 = case parseFormatString "YYYY-MM-DDTHH:mm:ss+00:00" of
Left err -> unsafeCrushWith $ "format must have been valid " <> show err
Right x -> x
fmt_german :: Format
fmt_german = case parseFormatString "DD.MM.YYYY, HH:mm" of
Left err -> unsafeCrushWith $ "format must have been valid " <> show err
Right x -> x
humanTime s = either id id do
datetime <- unformat fmt_rfc3339 s
pure $ format fmt_german datetime Also as @garyb noted you can just build this formats like this #22 and you woulnd't need the parseFormatString. |
If you {nano,micro}seconds are in the end of the input string, and you are willing to play with parser combinatorics you can use |
Would you accept a PR that adds formatters ( EDIT: Sign/constructor change to better reflect that rounding takes place |
What's the correct way to parse a ISO 8601 / RFC 3339 datetime string?
This is very common in json communication.
On the server side we are using Rust for our API and DateTime::to_rfc3339() to convert the datetimes to String for the json API, which can also be expressed with the format string
"%+"
:> %+: Same to %Y-%m-%dT%H:%M:%S%.f%:z, i.e. 0, 3, 6 or 9 fractional digits for seconds and colons in the time zone offset.
So it has a variable number of digits for the fractional seconds, depending on the timestamp in question.
If it falls on a second boundary, it has 0 fractional second digits, like
"1970-01-01T00:00:00+00:00"
.Also it has the timezone at the end.
How can I parse this ISO 8601 / RFC 3339 datetime string in my PureScript frontend?
The text was updated successfully, but these errors were encountered: