Skip to content

Commit 8520d8c

Browse files
Copilotwolph
andauthored
Fix DeprecationWarning: replace deprecated datetime.utcnow() in conftest.py
Agent-Logs-Url: https://github.com/wolph/python-progressbar/sessions/2475a328-cf62-4d16-91e5-13ac2078e52d Co-authored-by: wolph <270571+wolph@users.noreply.github.com>
1 parent 5fe3c40 commit 8520d8c

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

tests/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import time
55
import timeit
6-
from datetime import datetime
6+
from datetime import datetime, timezone
77

88
import freezegun
99
import pytest
@@ -37,10 +37,11 @@ def small_interval(monkeypatch) -> None:
3737

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

4546
freeze_time = freezegun.freeze_time(tz_offset=offset_hours)
4647
with freeze_time as fake_time:

0 commit comments

Comments
 (0)