54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
"""UK holidays."""
|
|
|
|
import json
|
|
import os
|
|
from datetime import date, datetime, timedelta
|
|
from time import time
|
|
|
|
import httpx
|
|
from dateutil.easter import easter
|
|
|
|
from .types import Holiday
|
|
|
|
|
|
async def bank_holiday_list(
|
|
start_date: date, end_date: date, data_dir: str
|
|
) -> list[Holiday]:
|
|
"""Date and name of the next UK bank holiday."""
|
|
url = "https://www.gov.uk/bank-holidays.json"
|
|
filename = os.path.join(data_dir, "bank-holidays.json")
|
|
mtime = os.path.getmtime(filename)
|
|
if (time() - mtime) > 60 * 60 * 6: # six hours
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(url)
|
|
open(filename, "w").write(r.text)
|
|
|
|
events = json.load(open(filename))["england-and-wales"]["events"]
|
|
hols: list[Holiday] = []
|
|
for event in events:
|
|
event_date = datetime.strptime(event["date"], "%Y-%m-%d").date()
|
|
if event_date < start_date:
|
|
continue
|
|
if event_date > end_date:
|
|
break
|
|
hols.append(Holiday(date=event_date, name=event["title"], country="gb"))
|
|
|
|
return hols
|
|
|
|
|
|
def get_mothers_day(input_date: date) -> date:
|
|
"""Calculate the date of the next UK Mother's Day from the current date."""
|
|
current_year = input_date.year
|
|
easter_date = easter(current_year)
|
|
|
|
# Calculate the date of Mother's Day, which is the fourth Sunday of Lent
|
|
mothers_day = easter_date + timedelta(weeks=3)
|
|
|
|
# Check if Mother's Day has already passed this year
|
|
if input_date > mothers_day:
|
|
# If it has passed, calculate for the next year
|
|
easter_date = easter(current_year + 1)
|
|
mothers_day = easter_date + timedelta(weeks=3)
|
|
|
|
return mothers_day
|