From 14133f591c2d35389f7ba7fbaf305c2d61db2be6 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Tue, 3 Oct 2023 09:27:14 +0100 Subject: [PATCH] Economist summer and Christmas double issues Closes: #9 --- agenda/__init__.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index 98f8a38..021fdba 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -152,10 +152,33 @@ def get_gbpusd() -> Decimal: return typing.cast(Decimal, 1 / data["quotes"]["USDGBP"]) -def next_economist() -> date: +def next_economist(input_date: date) -> date: """Next date that the Economist is published.""" - # TODO: handle the Christmas double issue correctly - return today + timedelta((3 - today.weekday()) % 7) + # 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) + current_day_of_week = input_date.weekday() + current_week_number = today.isocalendar().week + + # Define the list of weeks when The Economist is not published + non_publication_weeks = [26, 56] + + # Check if the input date is a publication day (Thursday) + if ( + current_day_of_week == publication_day + and current_week_number not in non_publication_weeks + ): + return input_date + + # Calculate the date for the next Thursday after the input date + days_until_next_thursday = (publication_day - current_day_of_week + 7) % 7 + next_thursday_date = input_date + timedelta(days=days_until_next_thursday) + + # Check if the next Thursday falls in a non-publication week + while next_thursday_date.isocalendar().week in non_publication_weeks: + # If it does, add 7 days to find the next Thursday + next_thursday_date += timedelta(days=7) + + return next_thursday_date def timedelta_display(delta: timedelta) -> str: @@ -225,7 +248,7 @@ def get_data() -> dict[str, str | object]: reply = { "now": now, "gbpusd": get_gbpusd(), - "next_economist": next_economist(), + "next_economist": next_economist(today), "bank_holiday": get_next_bank_holiday(), "us_holiday": get_us_holiday(), "next_uk_general_election": next_uk_general_election,