Parsing datetime string

Hi all :slight_smile:

I want to parse the following datetime string but can’t find the right format.

julia> using Dates

julia> format = dateformat"y-m-dTH:M:S.s"
dateformat"y-m-dTH:M:S.s"

julia> dt_str = "2021-05-26T21:00:19.1816626Z"
"2021-05-26T21:00:19.1816626Z"

julia> DateTime(dt_str, format)
ERROR: InexactError: convert(Dates.Decimal3, 1816626)
Stacktrace:
 [1] tryparsenext
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\io.jl:153 [inlined]
 [2] tryparsenext
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\io.jl:41 [inlined]
 [3] macro expansion
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:64 [inlined]
 [4] tryparsenext_core(str::String, pos::Int64, len::Int64, df::DateFormat{Symbol("y-m-dTH:M:S.s"), 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.Delim{Char, 1}, Dates.DatePart{'S'}, Dates.Delim{Char, 1}, Dates.DatePart{'s'}}}, raise::Bool)
   @ Dates C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:38
 [5] macro expansion
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:150 [inlined]
 [6] tryparsenext_internal
   @ C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:125 [inlined]
 [7] parse(::Type{DateTime}, str::String, df::DateFormat{Symbol("y-m-dTH:M:S.s"), 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.Delim{Char, 1}, Dates.DatePart{'S'}, Dates.Delim{Char, 1}, Dates.DatePart{'s'}}})
   @ Dates C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\parse.jl:282
 [8] DateTime(dt::String, df::DateFormat{Symbol("y-m-dTH:M:S.s"), 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.Delim{Char, 1}, Dates.DatePart{'S'}, Dates.Delim{Char, 1}, Dates.DatePart{'s'}}})
   @ Dates C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\io.jl:483
 [9] top-level scope
   @ REPL[4]:1

I know that Julias DateTime isn’t that accurate. But it’s good enough for me.

It is complaining about the millisecond part. The “s” only matches three digit milliseconds. So both “20” and “200” get parsed as 200 milliseconds. And providing any more than three digits throws the same error you’re seeing.

Is there a way to parse all 1816626Z seven digits and cut it off?

You can just cutoff extra chars. Since this is ISO format, number of used characters is always the same.

julia> using Dates

julia> format = dateformat"y-m-dTH:M:S"
dateformat"y-m-dTH:M:S"

julia> dt_str = "2021-05-26T21:00:19.1816626Z"
"2021-05-26T21:00:19.1816626Z"

julia> DateTime(dt_str[1:19], format)
2021-05-26T21:00:19
1 Like

I don’t think that works for me. I want the most precision as possible. I see that I could use a longer substring, but what if the source string ends with .0. Does anyone thinks that such cases should be handled by Dates?

This one could work for me… But I would really prefer it if Dates could handle that.

DateTime(dt_str[1:min(end-1, 23)], format)

DateTime only supports up to millisecond resolution (per the docs), so I don’t think you can force higher precision than that by using DateTime.

DateTime is only a thin wrapper around UTInstant{Millisecond}, so if you require higher precision, you should be able to follow what it does if you want to implement your own:

The TimesDates package announced here handles nanoseconds:

using TimesDates
dt_str = "2021-05-26T21:00:19.5816626Z"
tnano = TimeDate(dt_str[1:26])
2021-05-26T21:00:19.581662

julia> typeof(tnano)
TimeDate
1 Like