Skip to content

Commit 675087a

Browse files
fix(dates): use UTC for all date-to-timestamp conversions
Replace datetime.timestamp() (timezone-dependent) with calendar.timegm() (always UTC) in all date parsing functions. Fixes CI failure where UTC runner produced different timestamps than local CET/CEST system.
1 parent 6fa8920 commit 675087a

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

steam_library_manager/utils/date_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from __future__ import annotations
1717

18+
import calendar
1819
from datetime import datetime
1920

2021
__all__ = ["parse_date_to_timestamp", "format_timestamp_to_date", "to_timestamp", "year_from_timestamp"]
@@ -58,7 +59,7 @@ def parse_date_to_timestamp(date_str: str) -> str:
5859
for fmt in formats:
5960
try:
6061
dt = datetime.strptime(date_str, fmt)
61-
return str(int(dt.timestamp()))
62+
return str(calendar.timegm(dt.timetuple()))
6263
except ValueError:
6364
continue
6465

@@ -91,7 +92,7 @@ def to_timestamp(value) -> int:
9192
# Numeric date formats (locale-independent)
9293
for fmt in ("%d.%m.%Y", "%Y-%m-%d", "%Y/%m/%d", "%d-%m-%Y"):
9394
try:
94-
return int(datetime.strptime(s, fmt).timestamp())
95+
return calendar.timegm(datetime.strptime(s, fmt).timetuple())
9596
except ValueError:
9697
continue
9798
# Steam store format uses English month names ("Mar 23, 2020")
@@ -120,7 +121,7 @@ def _parse_english_date(s: str) -> int:
120121
locale.setlocale(locale.LC_TIME, "C")
121122
for fmt in ("%b %d, %Y", "%d %b, %Y", "%b %Y", "%B %d, %Y"):
122123
try:
123-
return int(datetime.strptime(s, fmt).timestamp())
124+
return calendar.timegm(datetime.strptime(s, fmt).timetuple())
124125
except ValueError:
125126
continue
126127
except locale.Error:
@@ -136,7 +137,7 @@ def _parse_english_date(s: str) -> int:
136137
def _year_to_ts(year: int) -> int:
137138
"""Converts a bare year (e.g. 2020) to Jan 1 00:00 UTC timestamp."""
138139
try:
139-
return int(datetime(year, 1, 1).timestamp())
140+
return calendar.timegm(datetime(year, 1, 1).timetuple())
140141
except (ValueError, OverflowError):
141142
return 0
142143

tests/unit/test_services/test_game_detail_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_applies_developer_and_publisher(self):
6868

6969
assert game.developer == "Valve"
7070
assert game.publisher == "Valve"
71-
assert game.release_year == 1191967200 # Oct 10, 2007
71+
assert game.release_year == 1191974400 # Oct 10, 2007 UTC
7272
assert "Action" in game.genres
7373
assert "Multi-player" in game.tags
7474

0 commit comments

Comments
 (0)