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

Handle deprecation of naive datetime functions like utcnow() #185

Merged
merged 10 commits into from
Nov 12, 2024

Conversation

tw4l
Copy link
Member

@tw4l tw4l commented Oct 28, 2024

Fixes #163

The approach taken follows the advice in #163 (comment) of returning naive datetimes by default, but (and this is a little different than suggested) allowing callers to specify tz_aware=True to receive an aware datetime with tzinfo=datetime.timezone.utc. This should prevent comparison errors between naive and aware datetimes for now while giving us a clear path forward if and when naive datetimes are fully deprecated in a future Python version.

tw4l added 2 commits October 28, 2024 12:32
Following the deprecation of utcnow in Python 3.12, we add a
tzinfo timezone argument to date utils. For now this defaults to
None so as not to break comparisons against existing naive
datetimes. If and when naive datetimes are fully deprecated in
a future Python release, we can change the default argument for
tzinfo from None to timezone.utc.
@tw4l tw4l requested a review from ikreymer October 28, 2024 16:55
@tw4l tw4l marked this pull request as draft October 28, 2024 16:57
@tw4l
Copy link
Member Author

tw4l commented Oct 28, 2024

Missed a few calls, putting back to draft for now

All good and ready for review. To verify, go to Python 3.12 CI run test output and verify that no datetime-related deprecation notices are present.

tw4l added 3 commits October 28, 2024 13:02
- Remove deprecated utcfromtimestamp()
- Feed timezone-aware datetime to utcttimetuple() to avoid
unexpected results
@tw4l tw4l marked this pull request as ready for review October 28, 2024 17:11
@wumpus
Copy link
Collaborator

wumpus commented Nov 7, 2024

Doesn't the WARC standard require utc? From looking at my crawler-that-uses-warcio I appear to have forced everything to UTC already, but then again my side-gig is astronomy.

@tw4l
Copy link
Member Author

tw4l commented Nov 7, 2024

Doesn't the WARC standard require utc? From looking at my crawler-that-uses-warcio I appear to have forced everything to UTC already, but then again my side-gig is astronomy.

Hi Greg, the question here is less about UTC vs other timezones and more about Python's naive vs aware datetime types. Miguel Grinberg has a pretty good blog post about how exactly this issue came about: https://blog.miguelgrinberg.com/post/it-s-time-for-a-change-datetime-utcnow-is-now-deprecated

With this PR, by default naive datetimes representing UTC will continue to be used, or for a UTC aware timezone a user can pass in datetime.timezone.utc as an argument to the methods. I suppose we could simply make the flag aware or similar to ensure that UTC is always being used if a timezone is specified.

@tw4l
Copy link
Member Author

tw4l commented Nov 11, 2024

@ikreymer @wumpus Since Greg had a good point that the WARC spec always expects UTC, I changed the tzinfo argument on the time utility functions to an aware bool defaulting to False. If set to true, the returned datetime will be timezone-aware with tzinfo=datetime.timezone.utc. Would just like to get a quick sanity check on that before merging.

@tw4l tw4l merged commit 5cf0d40 into master Nov 12, 2024
26 checks passed
@tw4l tw4l deleted the issue-163-utcnow-deprecation branch November 12, 2024 17:16
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 this pull request may close these issues.

DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version
3 participants