-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
base: master
Are you sure you want to change the base?
Conversation
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.
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. |
I think this might be not backward compatible, because |
To create a naive datetime
|
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() |
There was a problem hiding this comment.
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()
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.