Skip to content

Commit

Permalink
test: Use UTC timezone with datetime
Browse files Browse the repository at this point in the history
Running `flake8-datetimez` via Ruff yielded the following findings:

    example/test_examples.py:88:25:
    DTZ005 `datetime.datetime.now()` called without a `tz` argument
    example/test_examples.py:89:25:
    DTZ007 Naive datetime constructed using
    `datetime.datetime.strptime()` without %z
    example/test_examples.py:125:13:
    DTZ005 `datetime.datetime.now()` called without a `tz` argument
    example/test_examples.py:126:25:
    DTZ007 Naive datetime constructed using
    `datetime.datetime.strptime()` without %z
    Found 4 errors.

Specifying timezone info resolves the issues.
  • Loading branch information
jmgate committed Apr 22, 2024
1 parent 354b14d commit e06cee0
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions example/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import re
import shlex
import subprocess # nosec B404
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from pathlib import Path


Expand Down Expand Up @@ -85,10 +85,10 @@ def test_post_processing() -> None:
""".strip()
in result.stdout
)
thirty_miutes_ago = datetime.now() - timedelta(minutes=30)
thirty_miutes_ago = datetime.now(tz=timezone.utc) - timedelta(minutes=30)
time_from_example = datetime.strptime(
shlex.split(result.stdout)[-1], "%Y-%m-%d %H:%M:%S.%f"
)
).astimezone(timezone.utc)
assert ( # noqa: S101
thirty_miutes_ago - time_from_example < timedelta(seconds=1)
)
Expand Down Expand Up @@ -122,11 +122,11 @@ def test_pretty_printing() -> None:
in result.stdout
)
assert re.search(r"--src /\S+/file\.txt", result.stdout) # noqa: S101
today = datetime.now()
today = datetime.now(tz=timezone.utc)
time_from_example = datetime.strptime(
shlex.split(result.stdout.splitlines()[-1])[-1],
"%Y-%m-%d %H:%M:%S.%f",
)
).astimezone(timezone.utc)
assert today - time_from_example < timedelta(seconds=1) # noqa: S101


Expand Down

0 comments on commit e06cee0

Please sign in to comment.