diff --git a/agenda/economist.py b/agenda/economist.py index d843303..4438b98 100644 --- a/agenda/economist.py +++ b/agenda/economist.py @@ -2,27 +2,36 @@ from datetime import date, time, timedelta +from dateutil.relativedelta import TH, relativedelta + from . import uk_time from .types import Event 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) # Define the list of weeks when The Economist is not published non_publication_weeks = [26, 52] + double_issue = { + 25: "Summer", + 51: "Christmas", + } - current_date = start_date - publication_dates = [] t = time(19, 0) - while current_date <= end_date: - if ( - current_date.weekday() == publication_day - and current_date.isocalendar().week not in non_publication_weeks - ): - publication_dates.append(uk_time(current_date, t)) - current_date += timedelta(days=1) + start_thursday = start_date + relativedelta(weekday=TH(+1)) + end_thursday = end_date + relativedelta(weekday=TH(+1)) + days = (end_thursday - start_thursday).days - return [Event(name="economist", date=pub_date) for pub_date in publication_dates] + events: list[Event] = [] + for offset in range(0, days, 7): + pub_date = start_thursday + timedelta(days=offset) + week = pub_date.isocalendar().week + if week in non_publication_weeks: + continue + e = Event(name="economist", date=uk_time(pub_date, t)) + if week in double_issue: + e.title = double_issue[week] + " double issue" + events.append(e) + + return events