From f3929438bda95b53eb77fb4493167c1f072a4690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Wed, 24 Apr 2024 15:49:34 +0200 Subject: [PATCH] fix: make all `datetime`s timezone-aware --- api/ovdashboard_api/core/calevent.py | 21 +++++++++++++++++---- api/ovdashboard_api/core/dav/caldav.py | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/api/ovdashboard_api/core/calevent.py b/api/ovdashboard_api/core/calevent.py index c22bbca..aa523e1 100644 --- a/api/ovdashboard_api/core/calevent.py +++ b/api/ovdashboard_api/core/calevent.py @@ -6,16 +6,26 @@ Caches events using `timed_alru_cache`. import functools import logging -from datetime import datetime +from datetime import UTC, datetime from typing import Annotated, Self -from pydantic import BaseModel, ConfigDict, StringConstraints +from pydantic import AfterValidator, BaseModel, ConfigDict, StringConstraints from vobject.base import Component _logger = logging.getLogger(__name__) type StrippedStr = Annotated[str, StringConstraints(strip_whitespace=True)] +def make_utc(v: datetime) -> datetime: + if v.tzinfo is None: + return v.replace(tzinfo=UTC) + + return v + + +type UTCDateTime = Annotated[datetime, AfterValidator(make_utc)] + + @functools.total_ordering class CalEvent(BaseModel): """ @@ -31,8 +41,8 @@ class CalEvent(BaseModel): summary: StrippedStr = "" description: StrippedStr = "" - dtstart: datetime = datetime.now() - dtend: datetime = datetime.now() + dtstart: UTCDateTime = datetime.now(UTC) + dtend: UTCDateTime = datetime.now(UTC) def __lt__(self, other: Self) -> bool: """ @@ -64,6 +74,9 @@ class CalEvent(BaseModel): except KeyError: pass + print(event) + print(data) + if "dtend" not in data: data["dtend"] = data["dtstart"] diff --git a/api/ovdashboard_api/core/dav/caldav.py b/api/ovdashboard_api/core/dav/caldav.py index 3a0acb7..8ac5108 100644 --- a/api/ovdashboard_api/core/dav/caldav.py +++ b/api/ovdashboard_api/core/dav/caldav.py @@ -1,5 +1,5 @@ import logging -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from typing import cast from asyncify import asyncify @@ -69,7 +69,7 @@ class CalDAV: _logger.info(f"downloading {calendar_name!r} ...") dt_start = datetime.combine( - datetime.now().date(), + datetime.now(UTC).date(), datetime.min.time(), ) dt_end = dt_start + timedelta(days=cfg.calendar.future_days)