mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-23 21:22:59 +00:00
WIP: EventDays rework
This commit is contained in:
parent
8626b1460a
commit
d4c0d1ef5e
1 changed files with 55 additions and 0 deletions
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue