소스 검색

Update code to handle timezone-aware timestamps

jherve 1 년 전
부모
커밋
7ee63666cd
3개의 변경된 파일14개의 추가작업 그리고 5개의 파일을 삭제
  1. 5 2
      src/de_quoi_parle_le_monde/internet_archive.py
  2. 8 2
      src/de_quoi_parle_le_monde/snapshots.py
  3. 1 1
      src/de_quoi_parle_le_monde/storage.py

+ 5 - 2
src/de_quoi_parle_le_monde/internet_archive.py

@@ -3,6 +3,7 @@ from pathlib import Path
 from attrs import frozen, field
 from typing import Optional, ClassVar, NewType
 from datetime import date, datetime, timedelta
+from zoneinfo import ZoneInfo
 import cattrs
 from aiohttp.client import (
     ClientSession,
@@ -15,12 +16,14 @@ from aiolimiter import AsyncLimiter
 from config import settings
 
 
+tz_utc = ZoneInfo("UTC")
 Timestamp = NewType("Timestamp", datetime)
 datetime_format = "%Y%m%d%H%M%S"
 
 
 def parse_timestamp(s: str) -> Timestamp:
-    return datetime.strptime(s, datetime_format)
+    timestamp = datetime.strptime(s, datetime_format)
+    return timestamp.replace(tzinfo=tz_utc)
 
 
 def timestamp_to_str(ts: Timestamp) -> str:
@@ -187,7 +190,7 @@ class InternetArchiveClient:
             # also allows to always send a new actual request and not
             # hit the cache, but this is obviously an implementation detail
             # of the HTTP layer that this client should not be aware of..
-            to_=min(dt + timedelta(hours=6.0), datetime.now()),
+            to_=min(dt + timedelta(hours=6.0), datetime.now(tz_utc)),
             filter="statuscode:200",
             # Just to be safe, add an arbitrary limit to the number of values returned
             limit=100,

+ 8 - 2
src/de_quoi_parle_le_monde/snapshots.py

@@ -4,6 +4,7 @@ import tempfile
 import urllib.parse
 from pathlib import Path
 from datetime import date, datetime, time, timedelta
+from zoneinfo import ZoneInfo
 from attrs import frozen
 from loguru import logger
 
@@ -41,13 +42,18 @@ class SnapshotSearchJob(Job):
 
     @staticmethod
     def last_n_days_at_hours(n: int, hours: list[int]) -> list[datetime]:
-        now = datetime.now()
+        utc_tz = ZoneInfo("UTC")
+        now = datetime.now(utc_tz)
 
         return [
             dt
             for i in range(0, n)
             for h in hours
-            if (dt := datetime.combine(date.today() - timedelta(days=i), time(hour=h)))
+            if (
+                dt := datetime.combine(
+                    date.today() - timedelta(days=i), time(hour=h), tzinfo=utc_tz
+                )
+            )
             < now
         ]
 

+ 1 - 1
src/de_quoi_parle_le_monde/storage.py

@@ -492,7 +492,7 @@ class Storage:
             ]
 
     async def add_page(self, collection, page, dt):
-        assert (dt.tzinfo is not None)
+        assert dt.tzinfo is not None
 
         async with self.backend.get_connection() as conn:
             async with conn.transaction():