More functions have a date argument

This commit is contained in:
Edward Betts 2023-10-06 17:51:28 +01:00
parent f19a855532
commit 48bbd51216

View file

@ -31,10 +31,6 @@ warnings.simplefilter(action="ignore", category=FutureWarning)
# starlink visible # starlink visible
here = dateutil.tz.tzlocal() here = dateutil.tz.tzlocal()
now = datetime.now()
today = now.date()
now_str = now.strftime("%Y-%m-%d_%H:%M")
now_utc = datetime.now(timezone.utc)
next_us_presidential_election = date(2024, 11, 5) next_us_presidential_election = date(2024, 11, 5)
next_uk_general_election = date(2024, 5, 2) next_uk_general_election = date(2024, 5, 2)
@ -81,7 +77,7 @@ def next_uk_fathers_day(input_date: date) -> date:
days_until_sunday = (6 - current_day_of_week) % 7 days_until_sunday = (6 - current_day_of_week) % 7
# Calculate the date of the next Sunday # Calculate the date of the next Sunday
next_sunday = today + timedelta(days=days_until_sunday) next_sunday = input_date + timedelta(days=days_until_sunday)
# Calculate the date of Father's Day, which is the third Sunday of June # Calculate the date of Father's Day, which is the third Sunday of June
fathers_day = date(next_sunday.year, 6, 1) + timedelta( fathers_day = date(next_sunday.year, 6, 1) + timedelta(
@ -89,7 +85,7 @@ def next_uk_fathers_day(input_date: date) -> date:
) )
# Check if Father's Day has already passed this year # Check if Father's Day has already passed this year
if today > fathers_day: if input_date > fathers_day:
# If it has passed, calculate for the next year # If it has passed, calculate for the next year
fathers_day = date(fathers_day.year + 1, 6, 1) + timedelta( fathers_day = date(fathers_day.year + 1, 6, 1) + timedelta(
weeks=2, days=next_sunday.weekday() weeks=2, days=next_sunday.weekday()
@ -129,8 +125,9 @@ def get_next_bank_holiday(input_date: date) -> dict[str, date | str]:
return next_holiday return next_holiday
def get_gbpusd() -> Decimal: def get_gbpusd(now: datetime) -> Decimal:
"""Get the current value for GBPUSD, with caching.""" """Get the current value for GBPUSD, with caching."""
now_str = now.strftime("%Y-%m-%d_%H:%M")
fx_dir = os.path.join(data_dir, "fx") fx_dir = os.path.join(data_dir, "fx")
existing_data = os.listdir(fx_dir) existing_data = os.listdir(fx_dir)
existing = [f for f in existing_data if f.endswith("_GBPUSD.json")] existing = [f for f in existing_data if f.endswith("_GBPUSD.json")]
@ -161,7 +158,7 @@ def next_economist(input_date: date) -> date:
# Define the publication day (Thursday) and the day of the week of the input date # Define the publication day (Thursday) and the day of the week of the input date
publication_day = 3 # Thursday (0 - Monday, 1 - Tuesday, ..., 6 - Sunday) publication_day = 3 # Thursday (0 - Monday, 1 - Tuesday, ..., 6 - Sunday)
current_day_of_week = input_date.weekday() current_day_of_week = input_date.weekday()
current_week_number = today.isocalendar().week current_week_number = input_date.isocalendar().week
# Define the list of weeks when The Economist is not published # Define the list of weeks when The Economist is not published
non_publication_weeks = [26, 56] non_publication_weeks = [26, 56]
@ -268,13 +265,14 @@ def get_us_holiday(input_date: date) -> dict[str, date | str]:
return {"date": next_hol[0], "title": next_hol[1]} return {"date": next_hol[0], "title": next_hol[1]}
def get_data() -> dict[str, str | object]: def get_data(now: datetime) -> dict[str, str | object]:
"""Get data to display on agenda dashboard.""" """Get data to display on agenda dashboard."""
rocket_dir = os.path.join(data_dir, "thespacedevs") rocket_dir = os.path.join(data_dir, "thespacedevs")
today = now.date()
reply = { reply = {
"now": now, "now": now,
"gbpusd": get_gbpusd(), "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_holiday(today),