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

Use timezone-aware datetimes -- ruff check --select=DTZ #84

Merged
merged 4 commits into from
Mar 1, 2024

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Feb 29, 2024

https://docs.astral.sh/ruff/rules/#flake8-datetimez-dtz

% ruff check --select=DTZ --statistics

17	DTZ001	The use of `datetime.datetime()` without `tzinfo` argument is not allowed
 5	DTZ005	The use of `datetime.datetime.now()` without `tz` argument is not allowed
 4	DTZ003	The use of `datetime.datetime.utcnow()` is not allowed, use `datetime.datetime.now(tz=)` instead

% ruff rule DTZ001

call-datetime-without-tzinfo (DTZ001)

Derived from the flake8-datetimez linter.

What it does

Checks for datetime instantiations that lack a tzinfo argument.

Why is this bad?

datetime objects are "naive" by default, in that they do not include
timezone information. "Naive" objects are easy to understand, but ignore
some aspects of reality, which can lead to subtle bugs. Timezone-aware
datetime objects are preferred, as they represent a specific moment in
time, unlike "naive" objects.

By providing a tzinfo value, a datetime can be made timezone-aware.

Example

import datetime

datetime.datetime(2000, 1, 1, 0, 0, 0)

Use instead:

import datetime

datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)

Or, for Python 3.11 and later:

import datetime

datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=datetime.UTC)

Copy link
Collaborator

@jcjones jcjones left a comment

Choose a reason for hiding this comment

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

Woohoo!

@jcjones jcjones merged commit b3a0376 into letsencrypt:main Mar 1, 2024
4 checks passed
@cclauss cclauss deleted the datetimez branch March 1, 2024 00:30
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.

2 participants