Compare commits

...

2 commits

Author SHA1 Message Date
Edward Betts cc47762e9d All clock changes for now-365 to now+365
Closes: #57
2023-11-05 17:13:10 +00:00
Edward Betts 76fdfc6f48 Cache UK bank holidays for six hours 2023-11-05 17:12:28 +00:00

View file

@ -107,12 +107,16 @@ def next_uk_fathers_day(input_date: date) -> date:
return fathers_day return fathers_day
def get_next_timezone_transition(from_dt: datetime, tz_name: str) -> date: def timezone_transition(
"""Datetime of the next time the clocks change.""" start_dt: datetime, end_dt: datetime, key: str, tz_name: str
) -> list[Event]:
"""Clocks changes."""
tz = pytz.timezone(tz_name) tz = pytz.timezone(tz_name)
dt = next(t for t in tz._utc_transition_times if t > from_dt) return [
Event(name=key, date=pytz.utc.localize(t).astimezone(tz))
return typing.cast(date, dt.date()) 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]: async def get_next_bank_holiday(input_date: date) -> list[Event]:
@ -120,7 +124,7 @@ async def get_next_bank_holiday(input_date: date) -> list[Event]:
url = "https://www.gov.uk/bank-holidays.json" url = "https://www.gov.uk/bank-holidays.json"
filename = os.path.join(data_dir, "bank-holidays.json") filename = os.path.join(data_dir, "bank-holidays.json")
mtime = os.path.getmtime(filename) mtime = os.path.getmtime(filename)
if (unixtime() - mtime) > 3600: # one hour if (unixtime() - mtime) > 60 * 60 * 6: # six hours
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
r = await client.get(url) r = await client.get(url)
open(filename, "w").write(r.text) open(filename, "w").write(r.text)
@ -297,6 +301,9 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
last_year = today - timedelta(days=365) last_year = today - timedelta(days=365)
next_year = today + timedelta(days=365) next_year = today + timedelta(days=365)
minus_365 = now - timedelta(days=365)
plus_365 = now + timedelta(days=365)
( (
gbpusd, gbpusd,
gwr_advance_tickets, gwr_advance_tickets,
@ -317,8 +324,12 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
"us_holiday": get_us_holidays(today), "us_holiday": get_us_holidays(today),
"next_us_presidential_election": next_us_presidential_election, "next_us_presidential_election": next_us_presidential_election,
"stock_markets": stock_markets(), "stock_markets": stock_markets(),
"uk_clock_change": get_next_timezone_transition(now, "Europe/London"), "uk_clock_change": timezone_transition(
"us_clock_change": get_next_timezone_transition(now, "America/New_York"), 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), "mothers_day": next_uk_mothers_day(today),
"fathers_day": next_uk_fathers_day(today), "fathers_day": next_uk_fathers_day(today),
"uk_financial_year_end": uk_financial_year_end(today), "uk_financial_year_end": uk_financial_year_end(today),