diff --git a/agenda/__init__.py b/agenda/__init__.py index 13f890e..bdf2b5e 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -107,12 +107,16 @@ def next_uk_fathers_day(input_date: date) -> date: return fathers_day -def get_next_timezone_transition(from_dt: datetime, tz_name: str) -> date: - """Datetime of the next time the clocks change.""" +def timezone_transition( + start_dt: datetime, end_dt: datetime, key: str, tz_name: str +) -> list[Event]: + """Clocks changes.""" tz = pytz.timezone(tz_name) - dt = next(t for t in tz._utc_transition_times if t > from_dt) - - return typing.cast(date, dt.date()) + return [ + Event(name=key, date=pytz.utc.localize(t).astimezone(tz)) + for t in tz._utc_transition_times # type: ignore + if start_dt <= t <= end_dt + ] async def get_next_bank_holiday(input_date: date) -> list[Event]: @@ -297,6 +301,9 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]: last_year = today - timedelta(days=365) next_year = today + timedelta(days=365) + minus_365 = now - timedelta(days=365) + plus_365 = now + timedelta(days=365) + ( gbpusd, gwr_advance_tickets, @@ -317,8 +324,12 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]: "us_holiday": get_us_holidays(today), "next_us_presidential_election": next_us_presidential_election, "stock_markets": stock_markets(), - "uk_clock_change": get_next_timezone_transition(now, "Europe/London"), - "us_clock_change": get_next_timezone_transition(now, "America/New_York"), + "uk_clock_change": timezone_transition( + minus_365, plus_365, "uk_clock_change", "Europe/London" + ), + "us_clock_change": timezone_transition( + minus_365, plus_365, "us_clock_change", "America/New_York" + ), "mothers_day": next_uk_mothers_day(today), "fathers_day": next_uk_fathers_day(today), "uk_financial_year_end": uk_financial_year_end(today),