pymatgen
pymatgen copied to clipboard
[Dev] `datetime.datetime.utcnow()` deprecated and replacement breaks `strptime`
datetime.datetime.utcnow() deprecated
See discussion in https://github.com/materialsproject/pymatgen/pull/3705#discussion_r1537225117, datetime.datetime.utcnow() is marked as deprecated and might be removed after Python 3.12.
The same issue has been reported to monty as well: https://github.com/materialsvirtuallab/monty/issues/275.
But the new replacement returns slightly different output (an additional +00:00 at the end), which breaks strptime:
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_old = datetime.datetime.strptime(
str(start_time_old), "%Y-%m-%d %H:%M:%S.%f"
)
print(dt_old)
dt_new = datetime.datetime.strptime(
str(start_time_new), "%Y-%m-%d %H:%M:%S.%f"
)
print(dt_new)
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