Show more upcoming holidays in the UK and US

Closes: #18
This commit is contained in:
Edward Betts 2023-10-07 09:12:27 +01:00
parent 27a9ed0cf6
commit 22adb0eeb0

View file

@ -112,7 +112,7 @@ def get_next_timezone_transition(from_dt: datetime, tz_name: str) -> date:
return typing.cast(date, dt.date()) return typing.cast(date, dt.date())
def get_next_bank_holiday(input_date: date) -> Event: def get_next_bank_holiday(input_date: date) -> list[Event]:
"""Date and name of the next UK bank holiday.""" """Date and name of the next UK bank holiday."""
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")
@ -121,18 +121,19 @@ def get_next_bank_holiday(input_date: date) -> Event:
r = requests.get(url) r = requests.get(url)
open(filename, "w").write(r.text) open(filename, "w").write(r.text)
year_later = input_date + timedelta(days=365)
events = json.load(open(filename))["england-and-wales"]["events"] events = json.load(open(filename))["england-and-wales"]["events"]
next_holiday = None hols: list[Event] = []
for event in events: for event in events:
event_date = datetime.strptime(event["date"], "%Y-%m-%d").date() event_date = datetime.strptime(event["date"], "%Y-%m-%d").date()
if event_date < input_date: if event_date < input_date:
continue continue
next_holiday = Event(name="bank_holiay", date=event_date, title=event["title"]) if event_date > year_later:
break break
hols.append(Event(name="bank_holiday", date=event_date, title=event["title"]))
assert next_holiday return hols
return next_holiday
def get_gbpusd(now: datetime) -> Decimal: def get_gbpusd(now: datetime) -> Decimal:
@ -267,12 +268,14 @@ def stock_markets() -> list[str]:
return reply return reply
def get_us_holiday(input_date: date) -> Event: def get_us_holidays(input_date: date) -> list[Event]:
"""Date and name of next US holiday.""" """Date and name of next US holiday."""
hols = holidays.UnitedStates(years=[input_date.year, input_date.year + 1]) hols = holidays.UnitedStates(years=[input_date.year, input_date.year + 1])
next_hol = next(x for x in sorted(hols.items()) if x[0] >= input_date) return [
Event(name="us_holiday", date=hol_date, title=title)
return Event(name="us_holiday", date=next_hol[0], title=next_hol[1]) for hol_date, title in sorted(hols.items())
if hol_date >= input_date
]
def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]: def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]:
@ -315,7 +318,7 @@ def get_data(now: datetime) -> dict[str, str | object]:
"gbpusd": get_gbpusd(now), "gbpusd": get_gbpusd(now),
"next_economist": next_economist(today), "next_economist": next_economist(today),
"bank_holiday": get_next_bank_holiday(today), "bank_holiday": get_next_bank_holiday(today),
"us_holiday": get_us_holiday(today), "us_holiday": get_us_holidays(today),
# "next_uk_general_election": next_uk_general_election, # "next_uk_general_election": next_uk_general_election,
"next_us_presidential_election": next_us_presidential_election, "next_us_presidential_election": next_us_presidential_election,
"stock_markets": stock_markets(), "stock_markets": stock_markets(),
@ -325,7 +328,7 @@ def get_data(now: datetime) -> dict[str, str | object]:
"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),
"xmas_last_posting_dates": xmas_last_posting_dates, "xmas_last_posting_dates": xmas_last_posting_dates,
"xmas_day": xmas_day(today), # "xmas_day": xmas_day(today),
"rockets": thespacedevs.get_launches(rocket_dir, limit=40), "rockets": thespacedevs.get_launches(rocket_dir, limit=40),
} }
@ -335,12 +338,12 @@ def get_data(now: datetime) -> dict[str, str | object]:
if key in skip: if key in skip:
continue continue
if "holiday" in key: if "holiday" in key:
assert isinstance(value, Event) assert isinstance(value, list)
event = value events += value
else: else:
assert isinstance(value, date) assert isinstance(value, date)
event = Event(name=key, date=value) event = Event(name=key, date=value)
events.append(event) events.append(event)
for key, value in xmas_last_posting_dates.items(): for key, value in xmas_last_posting_dates.items():
events.append(Event(name=f"xmas_last_{key}", date=value)) events.append(Event(name=f"xmas_last_{key}", date=value))