Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import time
import timeit
from datetime import datetime
from datetime import datetime, timezone

import freezegun
import pytest
Expand Down Expand Up @@ -37,10 +37,11 @@ def small_interval(monkeypatch) -> None:

@pytest.fixture(autouse=True)
def sleep_faster(monkeypatch):
# The timezone offset in seconds, add 10 seconds to make sure we don't
# accidentally get the wrong hour
offset_seconds = (datetime.now() - datetime.utcnow()).seconds + 10
offset_hours = int(offset_seconds / 3600)
# Compute the local UTC offset so freezegun uses the same timezone as
# the local system. Using datetime.now(timezone.utc).astimezone() avoids
# the deprecated datetime.utcnow() (deprecated since Python 3.12).
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
offset_hours = local_offset.total_seconds() / 3600

freeze_time = freezegun.freeze_time(tz_offset=offset_hours)
Comment on lines +43 to 46
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The freezegun.freeze_time function accepts a timedelta object directly for the tz_offset parameter. You can simplify this logic by passing local_offset directly, which avoids the manual conversion to hours and handles fractional offsets more cleanly without precision loss.

Suggested change
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
offset_hours = local_offset.total_seconds() / 3600
freeze_time = freezegun.freeze_time(tz_offset=offset_hours)
local_offset = datetime.now(timezone.utc).astimezone().utcoffset()
freeze_time = freezegun.freeze_time(tz_offset=local_offset)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot what do you think of this change?

with freeze_time as fake_time:
Expand Down
Loading