Skip to content

Commit fb3f432

Browse files
author
Radosław Kozicki
committed
Use calendar.timegm instead of time.mktime for UTC timestamps
time.mktime() converts a time tuple in local time to seconds since the Epoch, as stated in the docs: > Convert a time tuple in local time to seconds since the Epoch. > Note that mktime(gmtime(0)) will not generally return zero for most > time zones; instead the returned value will either be equal to that > of the timezone or altzone attributes on the time module. calendar.timegm() is guaranteed to produce UTC timestamp: > Unrelated but handy function to calculate Unix timestamp from GMT.
1 parent c8d7e75 commit fb3f432

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/check_jsonschema/cachedownloader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import calendar
34
import contextlib
45
import hashlib
56
import io
@@ -43,7 +44,7 @@ def _resolve_cache_dir(dirname: str) -> str | None:
4344

4445
def _lastmod_from_response(response: requests.Response) -> float:
4546
try:
46-
return time.mktime(
47+
return calendar.timegm(
4748
time.strptime(response.headers["last-modified"], _LASTMOD_FMT)
4849
)
4950
# OverflowError: time outside of platform-specific bounds

tests/unit/test_cachedownloader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,10 @@ def test_cachedownloader_handles_bad_lastmod_header(
274274
elif failure_mode == "time_overflow":
275275
add_default_response()
276276

277-
def fake_mktime(*args):
277+
def fake_timegm(*args):
278278
raise OverflowError("uh-oh")
279279

280-
monkeypatch.setattr("time.mktime", fake_mktime)
280+
monkeypatch.setattr("calendar.timegm", fake_timegm)
281281
else:
282282
raise NotImplementedError
283283

0 commit comments

Comments
 (0)