Dates DateTime parse string with microseconds (decimal 6 places)

I’ve got files to import with postgres formatted timestamps to the microseconds (6 digits). I’m unable to find the proper date format. I’ve looked through Dates/test/io.jl as well in this forum. Curious if I’m missing something.

I’ll likely just truncate the string and use the millisecond formatting if I can’t find the microsecond parsing format.

import Dates
## need formatting for microseconds μ
#https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/test/io.jl
t1 = "2019-06-23 14:14:35.400963"
df1 = Dates.DateFormat("yyyy-mm-dd HH:MM:SS.s")
Dates.parse_components(t1, df1)
# ERROR: InexactError: convert(Dates.Decimal3, 400963)

df2 = Dates.DateFormat("yyyy-mm-dd HH:MM:SS.sss")
Dates.DateTime(t1, df2)
# ERROR: InexactError: convert(Dates.Decimal3, 400963)

df3 = Dates.DateFormat("yyyy-mm-dd HH:MM:SS.ssssss")
Dates.DateTime(t1, df3)
# ERROR: InexactError: convert(Dates.Decimal3, 400963)

I’ve verified the parsing works if I either remove the microseconds or truncate to 3 decimal places.

df4 = Dates.DateFormat("yyyy-mm-dd HH:MM:SS")
t2 = "2019-06-23 14:14:35"
Dates.DateTime(t2, df4)
#2019-06-23T14:14:35

t3 = "2019-06-23 14:14:35.400"
Dates.DateTime(t3, df2)
#2019-06-23T14:14:35.4
4 Likes

Did you ever get a response to this? I have the same problem…

DateTime only supports resolution up to millisecond precision, as it’s basically a wrapper around UTInstant{Millisecond}. See here.

If you need higher precision, it’s probably enough to split the string into a Date and Time component, as Time supports Nanosecond precision on a 24-hour day.

3 Likes

You can use TimesDates

using TimesDates

TimeDate("2022-01-07T09:30:21.383895100")
2 Likes