Show economist publication times over the year

This commit is contained in:
Edward Betts 2023-11-05 18:01:08 +00:00
parent 592710a28c
commit cb7f7536a9
3 changed files with 28 additions and 25 deletions

View file

@ -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,

View file

@ -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]

View file

@ -55,7 +55,7 @@
</head>
{% 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",