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

(Sonar) Fixed finding: "Using timezone-aware "datetime" objects should be preferred over using "datetime.datetime.utcnow" and "datetime.datetime.utcfromtimestamp"" #455

Merged

Conversation

pixeebot[bot]
Copy link
Contributor

@pixeebot pixeebot bot commented Jan 6, 2025

Remediation

This change fixes "Using timezone-aware "datetime" objects should be preferred over using "datetime.datetime.utcnow" and "datetime.datetime.utcfromtimestamp"" (id = python:S6903) identified by Sonar.

Details

Some datetime object calls use the machine's local timezone instead of a reasonable default like UTC. This may be okay in some cases, but it can lead to bugs. Misinterpretation of dates have been the culprit for serious issues in banking, satellite communications, and other industries.

The datetime documentation explicitly encourages using timezone aware objects to prevent bugs.

Our changes look like the following:

 from datetime import datetime
 import time

- datetime.utcnow()
- datetime.utcfromtimestamp(time.time())
+ datetime.now(tz=timezone.utc)
+ datetime.fromtimestamp(time.time(), tz=timezone.utc)
More reading

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: sonar:python/timezone-aware-datetime

…d be preferred over using "datetime.datetime.utcnow" and "datetime.datetime.utcfromtimestamp""
Copy link

sonarqubecloud bot commented Jan 6, 2025

Copy link
Contributor Author

pixeebot bot commented Jan 14, 2025

I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it?

If this change was not helpful, or you have suggestions for improvements, please let me know!

@Ludy87 Ludy87 merged commit 4d2d0a0 into main Jan 14, 2025
12 checks passed
@github-actions github-actions bot added PR: released and removed in progress wait in examination labels Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant