Functions take input_date instead of assuming today

This commit is contained in:
Edward Betts 2023-10-03 09:27:03 +01:00
parent a3f455d1de
commit b68654e4ac

View file

@ -49,9 +49,9 @@ access_key = config.get("exchangerate", "access_key")
data_dir = config.get("data", "dir") data_dir = config.get("data", "dir")
def next_uk_mothers_day() -> date: def next_uk_mothers_day(input_date: date) -> date:
"""Calculate the date of the next UK Mother's Day from the current date.""" """Calculate the date of the next UK Mother's Day from the current date."""
current_year = date.today().year current_year = input_date.year
easter_date = easter(current_year) easter_date = easter(current_year)
@ -59,23 +59,19 @@ def next_uk_mothers_day() -> date:
mothers_day = easter_date + timedelta(weeks=3) mothers_day = easter_date + timedelta(weeks=3)
# Check if Mother's Day has already passed this year # Check if Mother's Day has already passed this year
today = date.today() if input_date > mothers_day:
if today > mothers_day:
# If it has passed, calculate for the next year # If it has passed, calculate for the next year
current_year += 1 easter_date = easter(current_year + 1)
easter_date = easter(current_year)
mothers_day = easter_date + timedelta(weeks=3) mothers_day = easter_date + timedelta(weeks=3)
return mothers_day return mothers_day
def next_uk_fathers_day() -> date: def next_uk_fathers_day(input_date: date) -> date:
"""Calculate the date of the next UK Father's Day from the current date.""" """Calculate the date of the next UK Father's Day from the current date."""
# Get the current date # Get the current date
today = date.today()
# Calculate the day of the week for the current date (0 = Monday, 6 = Sunday) # Calculate the day of the week for the current date (0 = Monday, 6 = Sunday)
current_day_of_week = today.weekday() current_day_of_week = input_date.weekday()
# Calculate the number of days until the next Sunday # Calculate the number of days until the next Sunday
days_until_sunday = (6 - current_day_of_week) % 7 days_until_sunday = (6 - current_day_of_week) % 7
@ -238,8 +234,8 @@ def get_data() -> dict[str, str | object]:
"stock_markets": stock_markets(), "stock_markets": stock_markets(),
"uk_clock_change": get_next_timezone_transition("Europe/London"), "uk_clock_change": get_next_timezone_transition("Europe/London"),
"us_clock_change": get_next_timezone_transition("America/New_York"), "us_clock_change": get_next_timezone_transition("America/New_York"),
"mothers_day": next_uk_mothers_day(), "mothers_day": next_uk_mothers_day(today),
"fathers_day": next_uk_fathers_day(), "fathers_day": next_uk_fathers_day(today),
} }
return reply return reply