Просмотр исходного кода

Update code to handle timezone-aware timestamps

jherve 1 год назад
Родитель
Сommit
7ee63666cd

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

+ 8 - 2
src/de_quoi_parle_le_monde/snapshots.py

@@ -4,6 +4,7 @@ import tempfile
 import urllib.parse
 import urllib.parse
 from pathlib import Path
 from pathlib import Path
 from datetime import date, datetime, time, timedelta
 from datetime import date, datetime, time, timedelta
+from zoneinfo import ZoneInfo
 from attrs import frozen
 from attrs import frozen
 from loguru import logger
 from loguru import logger
 
 
@@ -41,13 +42,18 @@ class SnapshotSearchJob(Job):
 
 
     @staticmethod
     @staticmethod
     def last_n_days_at_hours(n: int, hours: list[int]) -> list[datetime]:
     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 [
         return [
             dt
             dt
             for i in range(0, n)
             for i in range(0, n)
             for h in hours
             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
             < 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):
     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 self.backend.get_connection() as conn:
             async with conn.transaction():
             async with conn.transaction():