0

I'm trying to parse a datetime using Haskell's Date.Time package, but I don't seem to have the format right.

Here are a few examples of dates that I'm trying to parse:

2015年01月01日T01:00:00.000Z
2015年01月11日T03:00:00.000Z
2015年01月11日T03:00:00.000Z

I have a function like:

parseStringToDateTime :: String -> UTCTime
parseStringToDateTime = parseTimeOrError False defaultTimeLocale "%Y-%m-%d %HH:%MM:%SS"

But this isn't quite right and I get a runtime error.

What's the correct format string to parse a datetime like this?

asked Sep 5, 2021 at 2:16

1 Answer 1

3

Using time-1.9.3 I managed to format your date-time strings like this:

λ> import Date.Time
λ> import Data.Time.Format.ISO8601 -- I think it's Date.Time.Format in newer versions
λ> formatParseM (iso8601Format @UTCTime) "2015年01月11日T03:00:00.000Z" >>= print
2015年01月11日 03:00:00 UTC

Now we can replicate parseTimeOrError for your use case:

λ> parseStringToDateTime s = maybe (error $ "bad date-time string " ++ s) id $ formatParseM (iso8601Format @UTCTime) s
λ> parseStringToDateTime "2015年01月11日T03:00:00.000Z" 
2015年01月11日 03:00:00 UTC
λ> parseStringToDateTime "BAD"
*** Exception: bad date-time string BAD
answered Sep 5, 2021 at 3:52
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.