I'm getting this error:
ValueError: time data '2022-01-20T15:30:57.2648531Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
here is my code:
import datetime
date_string = '2022-01-20T15:30:57.2648531Z'
date_format = datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%fZ")
unix_time = datetime.datetime.timestamp(date_format)
print(unix_time)
I have tested removing the final number in the time data and it worked. It seems you can only have 6 numbers in the millisecond spot. Is there a way to bypass/fix this?
CodePudding user response:
Unfortunately, the datetime.datetime class only supports microseconds ranging from 0 to 999999. Observe:
>>> import datetime
>>> datetime.datetime(2022, 1, 20, 15, 30, 57, 2648531)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
datetime.datetime(2022, 1, 20, 15, 30, 57, 2648531)
ValueError: microsecond must be in 0..999999
>>>
But even if you go out of your way to separate the microseconds from the string to add to the unix_time, you will still run into the problem of losing the 1:
import datetime
date_string = '2022-01-20T15:30:57.2648531Z'
index = date_string.rindex(".")
ms = float(date_string[index: -1])
date_format = datetime.datetime.strptime(date_string[:index], "%Y-%m-%dT%H:%M:%S")
unix_time = datetime.datetime.timestamp(date_format) ms
print(unix_time)
Output:
1642721457.264853
That is because python simply doesn't support such precision on floats:
>>> 1642721457.2648531
1642721457.264853
CodePudding user response:
It seems you can only have 6 numbers in the millisecond spot
It's actually microseconds, the %f specifier matches a 6-digit microsecond value. If you have a 7-digit number then the precision is more than one microsecond.
datetime objects do not support higher precision than microseconds, so you can just throw away the last digit. You can do this in multiple ways. Since according to your format specifier your string should always have the same length, you can simply trim it before converting.
import datetime
date_string = '2022-01-20T15:30:57.2648531Z'
date_string = date_string[:26] # trim anything past the 6th decimal digit
d = datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%f")
print(d.timestamp())
Since your date is in ISO format, you can also use datetime.datetime.fromisoformat(), which should make your life easier:
import datetime
date_string = '2022-01-20T15:30:57.2648531Z'
date_string = date_string[:26] # trim anything past the 6th decimal digit
d = datetime.datetime.fromisoformat(date_string)
print(d.timestamp())
CodePudding user response:
Using datetime will only get you so far (6-digits). Use pd.to_datetime instead:
unix_time = pd.to_datetime(date_string)
print(unix_time.value)
Output:
1642692657264853100
