Hey I am working in python with datetime and I am wondering what the best way to parse this timestamp is.
The timestamps are ISO standard, here is an example "2010-06-19T08:17:14.078685237Z"
Now so far I have used
time = datetime.datetime.strptime(timestamp.split(".")[0], "%Y-%m-%dT%H:%M:%S")
precisetime = time + datetime.timedelta(0,float("." + timestamp[:-1].split(".")[0]))
This kind of works, but I feel like there should be a more streamlined way (I am very new to python, and I am sure I am doing this like an ass). Also, I have nanoseconds in my timestamp, but only microseconds in my datetime object, is there a better module to work with? I need to be able to do operations on the time such as subtracting times and putting them in the scheduler.
Any better way to go about this?
You can use Numpy's datetime64: http://docs.scipy.org/doc/numpy-dev/reference/arrays.datetime.html
It supports nanoseconds and higher precisions.
>>> import numpy as np
>>> np.version.version
'1.7.1'
>>> np.datetime64("2010-06-19T08:17:14.078685237Z", dtype="datetime64[ns]")
numpy.datetime64('2010-06-19T08:17:14.078685237+0000')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With