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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants