Skip to content
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

Open
shyamd opened this issue Jun 24, 2021 · 2 comments
Open

datetime serialization/deserialization broken #275

shyamd opened this issue Jun 24, 2021 · 2 comments

Comments

@shyamd
Copy link
Contributor

shyamd commented Jun 24, 2021

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:

  • "%Y-%m-%d %H:%M:%S.%f"
  • "%Y-%m-%d %H:%M:%S"

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

@DanielYang59
Copy link
Contributor

I could confirm this issue for strptime with time zone info, materialsproject/pymatgen#3705 (comment).

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/python@3.12/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/python@3.12/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

@shyuep
Copy link
Contributor

shyuep commented Mar 31, 2024

I don't have time to fix this. But feel free to submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants