Skip to content

Commit 8f142f9

Browse files
committed
Ensure timezone-aware times during Match validation
1 parent fcd98fe commit 8f142f9

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

tptools/match.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import logging
22
from datetime import datetime
33
from functools import partial
4-
from typing import Literal, Self, cast
4+
from typing import Annotated, Literal, Self, cast
5+
from zoneinfo import ZoneInfo
56

6-
from pydantic import BaseModel
7+
import tzlocal
8+
from pydantic import AfterValidator, BaseModel
79

810
from .court import Court
911
from .draw import Draw
@@ -16,20 +18,28 @@
1618

1719
logger = logging.getLogger(__name__)
1820

21+
TZ_LOCAL = tzlocal.get_localzone()
22+
23+
24+
def _ensure_tzaware_time(dt: datetime, tz_local: ZoneInfo = TZ_LOCAL) -> datetime:
25+
if dt.tzinfo is None:
26+
return dt.replace(tzinfo=tz_local)
27+
return dt
28+
1929

2030
class Match[EntryT: Entry = Entry, DrawT: Draw = Draw, CourtT: Court = Court](
2131
ComparableMixin, ReprMixin, StrMixin, BaseModel
2232
):
2333
id: str
2434
matchnr: int
2535
draw: DrawT
26-
time: datetime | None
36+
time: Annotated[datetime, AfterValidator(_ensure_tzaware_time)] | None
2737
court: CourtT | None
2838
A: EntryT | str
2939
B: EntryT | str
3040
status: TPMatchStatus
31-
starttime: datetime | None = None
32-
endtime: datetime | None = None
41+
starttime: Annotated[datetime, AfterValidator(_ensure_tzaware_time)] | None
42+
endtime: Annotated[datetime, AfterValidator(_ensure_tzaware_time)] | None
3343
winner: Literal["A"] | Literal["B"] | None = None
3444
scores: ScoresType = []
3545

0 commit comments

Comments
 (0)