-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime serialization/deserialization broken #275
Comments
I could confirm this issue for import datetime
start_time_old = datetime.datetime.utcnow() # 2024-03-25 08:46:23.748342
start_time_new = datetime.datetime.now(datetime.UTC) # 2024-03-25 08:46:23.748472+00:00
print(start_time_old, start_time_new)
dt = datetime.datetime.strptime(
str(start_time_old), "%Y-%m-%d %H:%M:%S.%f"
)
print(dt)
dt = datetime.datetime.strptime(
str(start_time_new), "%Y-%m-%d %H:%M:%S.%f"
)
print(dt) Which raises: Traceback (most recent call last):
File "/Users/yang/developer/test/test.py", line 16, in <module>
dt = datetime.datetime.strptime(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/_strptime.py", line 554, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/_strptime.py", line 336, in _strptime
raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: +00:00 |
I don't have time to fix this. But feel free to submit a PR. |
I'm currently working on this, here I enclose a code snippet to recreate this issue from import json
import datetime
from monty.json import MontyEncoder, MontyDecoder
# created_at = datetime.datetime.utcnow() # deprecated API
created_at = datetime.datetime.now(tz=datetime.timezone.utc)
data = json.loads(json.dumps(created_at, cls=MontyEncoder))
created_at = MontyDecoder().process_decoded(data) Gives:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
datetime objects with time zone info break the serializatin because
__str__
doesn't output in the format that monty expects. Example:datetime.datetime(2018, 12, 19, 21, 45, 41, 99000, tzinfo=datetime.timezone.utc).__str__()
yields
"2018-12-19 21:45:41.099000+00:00"
When deserializing, this causes monty to throw an error because the two datetime formatting strings it tries are:
resulting in value error like this from within
strptime
:ValueError: unconverted data remains: .967000+00:00
We could just add the timezone format, or just use a formatting string when outputting from datetime to a string during serialization. There are also generic datetime parsing libraries like
dateutil
The text was updated successfully, but these errors were encountered: