Parsing Nanosecond Timestamps into DateTime

Hi

I receive streamed JSON data with nanosecond precision timestamps. I don’t need nanosecond precision for my application, millisecond precision is good enough, but I’m struggling to parse my timestamp string into a DateTime…

This works using a regular expression:

re = r"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})\d{6}Z"
m = match(re, "2022-01-01T00:00:00.002000001Z")
time = DateTime(m[1])

And this works using SubString():

time = DateTime(SubString("2022-01-01T00:00:00.002000001Z", 1, 23))

But I would like to use the following as it seems more succinct and clear:

time = DateTime("2022-01-01T00:00:00.002000001Z", dateformat"NEED_HELP_HERE")

I have tried lots of combinations of the format string but I can’t seem to get it right.

Any help would be very much appreciated.

Regards

You could try just parsing them to the nanosecond precision using a package like GitHub - JuliaTime/NanoDates.jl: Dates with nanosecond resolved days and then truncating to millisecond?

1 Like

Thank you, I will give that a try.

Hi John,

NanoDates.jl is to be introduced with a lightning talk at JuliaCon.
I have planned for an initial release some weeks earlier. Meanwhile, the package and the presentation are working in collaboration with Julia for our benefit.

TimesDates.jl is our current answer to your question. As a package in wide use it has been stable and, to the best of our knowledge, computing correctly for a good while. When NanoDates.jl is official, it offers more flexible use. However, it is not going to include timezone support.

As you have questions, please ask them here.

1 Like

Thank you Jeffrey

Looking forward to NanoDates release and the lightning talk.

Kind regards