From c6bc2f5f723366918bf4ce604290a7d247fdbdca Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 29 Oct 2023 15:48:32 +0000 Subject: [PATCH] Add Windmill Hill Market dates Closes: #36 --- agenda/__init__.py | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index b44ceb2..2f7f67b 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -5,7 +5,7 @@ import os import re import typing import warnings -from datetime import date, datetime, timedelta, timezone +from datetime import date, datetime, time, timedelta, timezone from decimal import Decimal from time import time as unixtime from typing import List @@ -21,7 +21,7 @@ import pytz import requests import yaml from dateutil.easter import easter -from dateutil.relativedelta import FR, relativedelta +from dateutil.relativedelta import FR, SA, relativedelta from agenda import thespacedevs @@ -327,6 +327,50 @@ def critical_mass(start_date: date, limit: int = 12) -> list[Event]: return events +def windmill_hill_market_days(start_date: date) -> list[Event]: + """Windmill Hill Market days for the next 12 months from a given date.""" + events: list[Event] = [] + current_date = start_date + + # To keep count of how many market days have been calculated + count = 0 + + tz = pytz.timezone("Europe/London") + + while count < 12: + # Skip months outside of April to December + if current_date.month < 4 or current_date.month > 12: + current_date += relativedelta(months=1) + current_date = date(current_date.year, current_date.month, 1) + continue + + # Calculate the first Saturday of the current month + first_saturday = current_date + relativedelta(day=1, weekday=SA(+1)) + + # Include it in the list only if it's on or after the start_date + if first_saturday >= start_date: + events.append( + Event( + name="market", + title="Windmill Hill Market", + date=tz.localize(datetime.combine(first_saturday, time(10, 0))), + ) + ) + count += 1 + + # Move to the next month + current_date += relativedelta(months=1) + current_date = date(current_date.year, current_date.month, 1) + + return events + + +# Test the function +if __name__ == "__main__": + start_date = date(2023, 10, 29) + print(windmill_hill_market_days(start_date)) + + def as_date(d: date | datetime) -> date: """Return date for given date or datetime.""" return d.date() if isinstance(d, datetime) else d @@ -500,6 +544,7 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]: "xmas_last_posting_dates": xmas_last_posting_dates, "gwr_advance_tickets": find_gwr_advance_ticket_date(), "critical_mass": critical_mass(today), + "market": windmill_hill_market_days(today), "rockets": thespacedevs.get_launches(rocket_dir, limit=40), }