diff --git a/agenda/__init__.py b/agenda/__init__.py index f0a25e4..7dfb3e1 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -23,7 +23,7 @@ from dateutil.relativedelta import FR, relativedelta from agenda import thespacedevs -from . import birthday, calendar, fx, gwr, markets, sun, waste_schedule +from . import birthday, calendar, economist, fx, gwr, markets, sun, waste_schedule from .types import Event warnings.simplefilter(action="ignore", category=FutureWarning) @@ -129,35 +129,6 @@ def get_next_bank_holiday(input_date: date) -> list[Event]: return hols -def next_economist(input_date: date) -> date: - """Next date that the Economist is published.""" - # 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 = input_date.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 uk_financial_year_end(input_date: date) -> date: """Next date of the end of the UK financial year, April 5th.""" # Determine the year of the input date @@ -406,7 +377,7 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]: reply = { "now": now, "gbpusd": fx.get_gbpusd(config), - "next_economist": next_economist(today), + "next_economist": economist.next_pub_date(today), "bank_holiday": get_next_bank_holiday(today), "us_holiday": get_us_holidays(today), "next_us_presidential_election": next_us_presidential_election, diff --git a/agenda/economist.py b/agenda/economist.py new file mode 100644 index 0000000..ac1df2f --- /dev/null +++ b/agenda/economist.py @@ -0,0 +1,32 @@ +"""Next publication date of the Economist.""" + +from datetime import date, timedelta + + +def next_pub_date(input_date: date) -> date: + """Next date that the Economist is published.""" + # 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 = input_date.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