From cb7f7536a939d844dfb3f4274ae9904943779071 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 5 Nov 2023 18:01:08 +0000 Subject: [PATCH] Show economist publication times over the year --- agenda/__init__.py | 2 +- agenda/economist.py | 49 +++++++++++++++++++++++--------------------- templates/index.html | 2 +- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index d4c025e..e886588 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -336,7 +336,7 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]: reply = { "now": now, "gbpusd": gbpusd, - "next_economist": economist.next_pub_date(today), + "economist": economist.publication_dates(last_year, next_year), "bank_holiday": bank_holiday, "us_holiday": get_us_holidays(last_year, next_year), "next_us_presidential_election": next_us_presidential_election, diff --git a/agenda/economist.py b/agenda/economist.py index ac1df2f..99acdd8 100644 --- a/agenda/economist.py +++ b/agenda/economist.py @@ -1,32 +1,35 @@ """Next publication date of the Economist.""" -from datetime import date, timedelta +from datetime import date, datetime, time, timedelta + +import pytz + +from .types import Event + +uk_tz = pytz.timezone("Europe/London") -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 +def add_pub_time(pub_date: date) -> datetime: + """Publication time is 19:00.""" + return uk_tz.localize(datetime.combine(pub_date, time(19, 0))) + + +def publication_dates(start_date: date, end_date: date) -> list[Event]: + """List of Economist publication dates.""" + # Define the publication day (Thursday) and non-publication weeks 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] + non_publication_weeks = [26, 52] - # 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 + current_date = start_date + publication_dates = [] - # 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) + while current_date <= end_date: + if ( + current_date.weekday() == publication_day + and current_date.isocalendar().week not in non_publication_weeks + ): + publication_dates.append(add_pub_time(current_date)) + current_date += timedelta(days=1) - # 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 + return [Event(name="economist", date=pub_date) for pub_date in publication_dates] diff --git a/templates/index.html b/templates/index.html index 2354895..5a604a9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -55,7 +55,7 @@ {% set event_labels = { - "next_economist": "The Economist", + "economist": "The Economist", "mothers_day": "Mothers' day", "fathers_day": "Fathers' day", "uk_financial_year_end": "End of financial year",