Can't parse AM/PM?

I cannot seem to figure out how to parse a datetime string with am/pm:

using Dates

DateTime("2021-02-01 11:12AM EST", dateformat"y-m-d H:Mp xxx")

gives this error:

ERROR: ArgumentError: Unable to parse date time. Expected directive DatePart(p) at char 16
Stacktrace:
 [1] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:104 [inlined]
 [2] tryparsenext_core(::String, ::Int64, ::Int64, ::DateFormat{Symbol("y-m-d H:Mp xxx"),Tuple{Dates.DatePart{'y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.DatePart{'p'},Dates.Delim{String,4}}}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:38
 [3] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:150 [inlined]
 [4] tryparsenext_internal at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:125 [inlined]
 [5] parse(::Type{DateTime}, ::String, ::DateFormat{Symbol("y-m-d H:Mp xxx"),Tuple{Dates.DatePart{'y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.DatePart{'p'},Dates.Delim{String,4}}}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:282
 [6] DateTime(::String, ::DateFormat{Symbol("y-m-d H:Mp xxx"),Tuple{Dates.DatePart{'y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.DatePart{'p'},Dates.Delim{String,4}}}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/io.jl:482
 [7] top-level scope at REPL[4]:1

I tried using ZonedDateTime, replacing the “xxx” with “Z”, but I get the same error. Is this a bug, or am I missing something obvious?

julia> DateTime("2021-02-01 11:12PM EST", dateformat"y-m-d I:MMp \E\S\T")
2021-02-01T23:12:00

seems to be a bug.

1 Like

I’m not sure it’s possible:

https://juliatime.github.io/TimeZones.jl/stable/conversions/

Note that with the exception of “UTC” and “GMT” time zone abbrevations cannot be parsed using the Z character code since most abbreviations are ambiguous. For example abbreviation “MST” could be interpreted as “Mountain Standard Time” (UTC-7) or “Moscow Summer Time” (UTC+3:31).

The time zone isn’t the bug I was refering to, sorry for being unclear, but I just ignored the time zone question of OP.
The bug I mean is that the minute needs to be a double M, so that the p (=AM/PM) is interpreted correctly.

2 Likes

Ahh, I see. I would be on the edge of if that is a bug or not. Without a space (or something) to separate the two fields the code would have to guess a little. For example these don’t work:

DateTime("20210201", dateformat"ymd")
DateTime("20210201", dateformat"ymmdd")

You have to do the full format for it to parse correctly:

DateTime("20210201", dateformat"yyyymmdd")

Granted with the minutes vs am/pm one is a numbers and the other letters so it could be inferred, but then that would make it special/different from the other identifiers. :man_shrugging:

Good points.
I think AM/PM and some time zone information is quite common when parsing date/time strings. So it should be part of the documentation as examples. When there are examples it would be explicit how the format string should be given, and than it could be better classified as bug or edge case, despite it would be less important then :slight_smile:
The point is: parsing date/time is best documented as a large set of examples.

3 Likes