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

[Dev] datetime.datetime.utcnow() deprecated and replacement breaks strptime #3791

Closed
DanielYang59 opened this issue Apr 29, 2024 · 3 comments · Fixed by #3871
Closed

[Dev] datetime.datetime.utcnow() deprecated and replacement breaks strptime #3791

DanielYang59 opened this issue Apr 29, 2024 · 3 comments · Fixed by #3871

Comments

@DanielYang59
Copy link
Contributor

DanielYang59 commented Apr 29, 2024

datetime.datetime.utcnow() deprecated

See discussion in #3705 (comment), 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: materialsvirtuallab/monty#275.

But the new replacement returns slightly different output (an additional timezone info +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
@DanielYang59
Copy link
Contributor Author

DanielYang59 commented Jun 22, 2024

It seems I cannot re-open this for some reason. This was closed by the accidentally included to fix keyword but was not fixed.

7fac0b4 need to fix #3791

@DanielYang59
Copy link
Contributor Author

DanielYang59 commented Aug 4, 2024

Thanks for the fix @janosh, the Windows tests failing in CI is due to a matplotlib issue 628f737, but that PR #3887 would need some extra time to be reviewed. Perhaps you could fix that directly on master?

Meanwhile it looks like one unit test for util.provenance is broken, probably because the new API is not a drop-in replacement (with additional timezone info at the end):

import datetime


print(datetime.datetime.utcnow())  >>> 2024-08-04 12:55:17.167352
print(datetime.datetime.now(tz=datetime.timezone.utc))   >>> 2024-08-04 12:55:17.167478+00:00

@janosh
Copy link
Member

janosh commented Aug 4, 2024

thanks, good call!

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

Successfully merging a pull request may close this issue.

2 participants