Skip to content

Commit fcd98fe

Browse files
committed
Make match times local timezone-aware
1 parent d1d9623 commit fcd98fe

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ write_to = "_version.py"
1313

1414
[project]
1515
name = "tptools"
16-
version = "0.6.1"
16+
version = "0.6.2"
1717
authors = [{ name = "martin f. krafft", email = "[email protected]" }]
1818
description = "A set of tools to export data from with TournamentSoftware"
1919
readme = "README.md"
@@ -39,6 +39,7 @@ dependencies = [
3939
"fastapi",
4040
"uvicorn",
4141
"httpx",
42+
"tzlocal",
4243
"asyncinotify; sys_platform == 'linux'",
4344
"pywin32; sys_platform == 'win32'",
4445
"sqlalchemy-access; sys_platform == 'win32'",

tests/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
2-
from collections.abc import Mapping
32
import logging
43
import pathlib
4+
from collections.abc import Mapping
55
from contextlib import nullcontext
66
from datetime import datetime
77
from enum import IntEnum

tptools/match.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
from datetime import datetime
3+
from functools import partial
34
from typing import Literal, Self, cast
45

56
from pydantic import BaseModel
@@ -36,21 +37,13 @@ class Match[EntryT: Entry = Entry, DrawT: Draw = Draw, CourtT: Court = Court](
3637
"id",
3738
"matchnr",
3839
"draw",
39-
("time", TPMatch._time_repr, False),
40+
("time", partial(TPMatch._time_repr, attr="time"), False),
4041
"court",
4142
"A",
4243
"B",
4344
("status", TPMatch._status_repr, False),
44-
(
45-
"starttime",
46-
lambda s: s.starttime.isoformat() if s.starttime else None,
47-
False,
48-
),
49-
(
50-
"endtime",
51-
lambda s: s.endtime.isoformat() if s.endtime else None,
52-
False,
53-
),
45+
("starttime", partial(TPMatch._time_repr, attr="starttime"), False),
46+
("endtime", partial(TPMatch._time_repr, attr="endtime"), False),
5447
"winner",
5548
("scores", lambda s: scores_to_string(s.scores, nullstr="-"), False),
5649
]

tptools/tpmatch.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from functools import partial
77
from typing import Any, Callable, Literal, Self, cast
88

9+
import tzlocal
910
from pydantic import BaseModel, model_validator
1011

1112
from .mixins import ComparableMixin, ReprMixin, StrMixin
@@ -21,6 +22,8 @@
2122

2223
logger = logging.getLogger(__name__)
2324

25+
LOCAL_TZ = tzlocal = tzlocal.get_localzone()
26+
2427

2528
class TPMatchStatus(StrEnum):
2629
READY = auto()
@@ -163,7 +166,11 @@ def court(self) -> TPCourt | None:
163166

164167
@property
165168
def time(self) -> datetime | None:
166-
return self.pm1.time
169+
return (
170+
self.pm1.time.replace(tzinfo=LOCAL_TZ)
171+
if self.pm1.time is not None
172+
else None
173+
)
167174

168175
@property
169176
def played(self) -> bool:
@@ -191,11 +198,19 @@ def scores(self) -> ScoresType:
191198

192199
@property
193200
def starttime(self) -> datetime | None:
194-
return self.pm1.starttime
201+
return (
202+
self.pm1.starttime.replace(tzinfo=LOCAL_TZ)
203+
if self.pm1.starttime is not None
204+
else None
205+
)
195206

196207
@property
197208
def endtime(self) -> datetime | None:
198-
return self.pm1.endtime
209+
return (
210+
self.pm1.endtime.replace(tzinfo=LOCAL_TZ)
211+
if self.pm1.endtime is not None
212+
else None
213+
)
199214

200215
@property
201216
def van(self) -> tuple[int, int]:
@@ -253,8 +268,13 @@ def status(self) -> TPMatchStatus:
253268

254269
return ret
255270

256-
def _time_repr(self) -> str:
257-
return repr(self.time).replace("datetime.", "")
271+
def _time_repr(self, attr: str) -> str:
272+
dtobj = getattr(self, attr)
273+
return (
274+
repr(dtobj.replace(tzinfo=None)).replace("datetime.", "")
275+
if dtobj is not None
276+
else repr(None)
277+
)
258278

259279
def _van_repr(self) -> str | None:
260280
return self.pm1._van_repr()
@@ -286,7 +306,7 @@ def _status_repr(self) -> str:
286306
"id",
287307
"draw.name",
288308
"matchnr",
289-
("time", _time_repr, False),
309+
("time", partial(_time_repr, attr="time"), False),
290310
("court", _court_repr, True),
291311
"slot1?.name",
292312
"slot2?.name",
@@ -295,8 +315,8 @@ def _status_repr(self) -> str:
295315
("wnvn", _wnvn_repr, False),
296316
("status", _status_repr, False),
297317
"winner?.name",
298-
"starttime",
299-
"endtime",
318+
("starttime", partial(_time_repr, attr="starttime"), False),
319+
("endtime", partial(_time_repr, attr="endtime"), False),
300320
("scores", lambda s: scores_to_string(s.scores, nullstr="-"), False),
301321
)
302322

0 commit comments

Comments
 (0)