Dates.Time Parsing a Time String with Nanosecond Resolution

Hi

Julia 1.9.3 MacOS x86_64

As I understand the Dates documentation, the resolution for a DateTime object is milliseconds and for a Time object it is nanoseconds.

So, I believe I should be able to parse a nanosecond time string into a Time object, but I’m struggling with this.

For example, this works…

julia> t = parse(Time, “00:00:00.123”)
00:00:00.123
julia> t
00:00:00.123
julia> t.instant
123000000 nanoseconds

But, I can’t get this to work…

parse(Time, “00:00:00.123456789”)
ERROR: ArgumentError: Unable to parse date time. Expected directive DatePart(s) at char 10

I’ve tried various DateFormat strings, but none seem to work.

An example in julia/stdlib/Dates/test/io.jl with finer resolution than milliseconds would be helpful.

Kind regards

1 Like

There’s a longstanding open issue for this. You could do something like this as a workaround:

julia> function ns_time(t_str)
           t_s, t_frac = split(t_str, '.')
           Time(t_s) + Nanosecond(rpad(t_frac, 9, '0'))
       end
ns_time (generic function with 1 method)

julia> ns_time("00:00:00.123")
00:00:00.123

julia> ns_time("00:00:00.123456789")
00:00:00.123456789
1 Like
3 Likes

Thank you all. I will give NanoDates a try.