From d4c0d1ef5eac0e66720171b048fc3971f7cd5210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 19 Sep 2023 18:49:10 +0200 Subject: [PATCH] WIP: EventDays rework --- api/advent22_api/core/helpers.py | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/api/advent22_api/core/helpers.py b/api/advent22_api/core/helpers.py index 6cf02f1..cb124b0 100644 --- a/api/advent22_api/core/helpers.py +++ b/api/advent22_api/core/helpers.py @@ -72,6 +72,61 @@ async def api_return_image(img: Image.Image) -> StreamingResponse: ) +class EventDays2: + """ + Events in einem Ereigniszeitraum + """ + + __overall_duration: timedelta + __events: dict[int, date] + + def first(self) -> date: + """Datum des ersten Ereignisses""" + return self.__events[min(self.__events.keys())] + + def last(self) -> date: + """Datum des letzten Ereignisses""" + return self.__events[max(self.__events.keys())] + + def end(self) -> date: + """Letztes Datum des Ereigniszeitraums""" + return self.__events[max(self.__events.keys())] + + def __init__( + self, + *, + # current date + today: date, + # month/day when events begin + begin_month: int, + begin_day: int, + # events: e.g. a 2 means there is an event 2 days after begin + # -> assume sorted (asc) + events_after: list[int], + # countdown to closing begins after last event + closing_after: int, + ) -> None: + # account for the last event, then add closing period + self.__overall_duration = timedelta(days=events_after[-1] + closing_after) + + # the events may begin last year, this year or next year + maybe_begin = ( + datetime(today.year + year_diff, begin_month, begin_day).date() + for year_diff in (-1, 0, +1) + ) + + # find the first begin where the end date is in the future + begin = next( + begin for begin in maybe_begin if today <= (begin + self.__overall_duration) + ) + + # all event dates + self.__events = { + event_after: begin + timedelta(days=event_after - 1) + for event_after in events_after + } + + class EventDays(BaseModel): """ Kenndaten eines Ereigniszeitraums: