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

[master] Fix datetime.datetime.utcnow() deprecated in Python 3.12 #66042

Open
wants to merge 34 commits into
base: master
Choose a base branch
from

Conversation

chloekaii
Copy link

@chloekaii chloekaii commented Feb 11, 2024

What does this PR do?

This changes all instances of datetime.utcnow() to datetime.now(tz=timezone.utc).

What issues does this PR fix or reference?

Fixes: #65604

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

No

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

@chloekaii chloekaii requested a review from a team as a code owner February 11, 2024 22:43
@chloekaii chloekaii requested review from twangboy and removed request for a team February 11, 2024 22:43
Copy link

welcome bot commented Feb 11, 2024

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!

@salt-project-bot-prod-environment salt-project-bot-prod-environment bot changed the title Fix datetime.datetime.utcnow() deprecated in Python 3.12 [master] Fix datetime.datetime.utcnow() deprecated in Python 3.12 Feb 11, 2024
@max-arnold
Copy link
Contributor

max-arnold commented Feb 12, 2024

I think this might be not backward compatible, because utcnow() returns a naive datetime, and now(tz=datetime.UTC) returns a timezone-aware one.

@twangboy
Copy link
Contributor

To create a naive datetime

from datetime import datetime, UTC
datetime.now(UTC).replace(tzinfo=None)

@marmarek
Copy link
Contributor

I think this might be not backward compatible, because utcnow() returns a naive datetime, and now(tz=datetime.UTC) returns a timezone-aware one.

That's the point of this deprecation warning, no? To stop using non-timezone-aware objects.

utc_finish_time = datetime.datetime.utcnow()
timezone_delta = datetime.datetime.utcnow() - datetime.datetime.now()
utc_finish_time = datetime.datetime.now(tz=datetime.timezone.utc)
timezone_delta = datetime.datetime.now(tz=datetime.timezone.utc) - datetime.datetime.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work: TypeError: can't subtract offset-naive and offset-aware datetimes
A better approach is to use astimezone() instead of calculating timezone_delta and adding it manually:

local_start_time = utc_start_time.astimezone()
local_finish_time = utc_finish_time.astimezone()

@marmarek marmarek mentioned this pull request Sep 18, 2024
2 tasks
@twangboy
Copy link
Contributor

This needs to be rebased and there are quite a number of test failures. This will also need a changelog.
Some of these changes are in @marmarek's PR #66902

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.

[TECH DEBT] datetime.datetime.utcnow() is deprecated in Python 3.12
4 participants