Skip to content

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

Closed
chloekaii wants to merge 34 commits into
saltstack:masterfrom
chloekaii:master
Closed

chloekaii wants to merge 34 commits into
saltstack:masterfrom
chloekaii:master

Conversation

@chloekaii

@chloekaii chloekaii commented Feb 11, 2024 •

Copy link
Copy Markdown

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.

@twangboy

Copy link
Copy Markdown
Contributor

To create a naive datetime

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

@marmarek

Copy link
Copy Markdown
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.

Comment thread salt/state.py
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
Copy Markdown
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@chloekaii Can you address this please?

@marmarek marmarek mentioned this pull request Sep 18, 2024
2 tasks done
@twangboy

Copy link
Copy Markdown
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

@dwoz
dwoz requested a review from a team as a code owner March 16, 2025 22:09
@twangboy twangboy added the test:full Run the full test suite label Jul 1, 2025
@twangboy twangboy added this to the Argon v3008.0 milestone Jul 1, 2025
@twangboy

Copy link
Copy Markdown
Contributor

Please rebase and address conflicts

@dwoz

dwoz commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

This is no longer needed with #66902. If I'm missing something feel free to re-open it.

@dwoz dwoz closed this Mar 18, 2026

This branch was previously deployed

1 inactive deployment
ci — bab86dd8 Deployed Feb 12, 2024 by chloekaii
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants