fix: make all datetimes timezone-aware

This commit is contained in:
Jörn-Michael Miehe 2024-04-24 15:49:34 +02:00
parent b895b0e829
commit f3929438bd
2 changed files with 19 additions and 6 deletions

View file

@ -6,16 +6,26 @@ Caches events using `timed_alru_cache`.
import functools import functools
import logging import logging
from datetime import datetime from datetime import UTC, datetime
from typing import Annotated, Self from typing import Annotated, Self
from pydantic import BaseModel, ConfigDict, StringConstraints from pydantic import AfterValidator, BaseModel, ConfigDict, StringConstraints
from vobject.base import Component from vobject.base import Component
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
type StrippedStr = Annotated[str, StringConstraints(strip_whitespace=True)] 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 @functools.total_ordering
class CalEvent(BaseModel): class CalEvent(BaseModel):
""" """
@ -31,8 +41,8 @@ class CalEvent(BaseModel):
summary: StrippedStr = "" summary: StrippedStr = ""
description: StrippedStr = "" description: StrippedStr = ""
dtstart: datetime = datetime.now() dtstart: UTCDateTime = datetime.now(UTC)
dtend: datetime = datetime.now() dtend: UTCDateTime = datetime.now(UTC)
def __lt__(self, other: Self) -> bool: def __lt__(self, other: Self) -> bool:
""" """
@ -64,6 +74,9 @@ class CalEvent(BaseModel):
except KeyError: except KeyError:
pass pass
print(event)
print(data)
if "dtend" not in data: if "dtend" not in data:
data["dtend"] = data["dtstart"] data["dtend"] = data["dtstart"]

View file

@ -1,5 +1,5 @@
import logging import logging
from datetime import datetime, timedelta from datetime import UTC, datetime, timedelta
from typing import cast from typing import cast
from asyncify import asyncify from asyncify import asyncify
@ -69,7 +69,7 @@ class CalDAV:
_logger.info(f"downloading {calendar_name!r} ...") _logger.info(f"downloading {calendar_name!r} ...")
dt_start = datetime.combine( dt_start = datetime.combine(
datetime.now().date(), datetime.now(UTC).date(),
datetime.min.time(), datetime.min.time(),
) )
dt_end = dt_start + timedelta(days=cfg.calendar.future_days) dt_end = dt_start + timedelta(days=cfg.calendar.future_days)