commit 37bc5948e2553560187de96d6f460ef3323ab344 Author: Scott Wallace Date: Tue May 7 12:37:58 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c92f82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.pyenv/ +mothering_sunday.ics diff --git a/main.py b/main.py new file mode 100644 index 0000000..c38b657 --- /dev/null +++ b/main.py @@ -0,0 +1,67 @@ +""" +Generated by ChatGPT 3.5 (https://chatgpt.com/c/103d4f23-7c8f-484f-9598-d38187e933f2) +""" + +from datetime import datetime, timedelta + +from icalendar import Calendar, Event + + +def mothering_sunday(year: int) -> datetime: # pylint: disable=too-many-locals + """ + Calculate Mothering Sunday (UK) for a particular year + + Args: + year (int): year to calculate Mothering Sunday + + Returns: + datetime: datetime object for Mothering Sunday + """ + + # Calculate Easter Sunday + a = year % 19 + b = year // 100 + c = year % 100 + d = b // 4 + e = b % 4 + f = (b + 8) // 25 + g = (b - f + 1) // 3 + h = (19 * a + b - d - g + 15) % 30 + i = c // 4 + k = c % 4 + l = (32 + 2 * e + 2 * i - h - k) % 7 + m = (a + 11 * h + 22 * l) // 451 + month = (h + l - 7 * m + 114) // 31 + day = ((h + l - 7 * m + 114) % 31) + 1 + easter_sunday = datetime(year, month, day) + + # Calculate Mothering Sunday + final_day = easter_sunday - timedelta(weeks=3) + return final_day + + +def generate_ical() -> None: + """ + Generate an ICAL file for Mothering Sunday (UK) + """ + cal = Calendar() + cal.add("prodid", "-//Mothering Sunday Calendar//wallace.sh//") + cal.add("version", "2.0") + + for year in range(datetime.now().year, datetime.now().year + 10): + date = mothering_sunday(year) + # Ensure Mothering Sunday falls on a Sunday + if date.weekday() != 6: # 6 corresponds to Sunday + date += timedelta(days=6 - date.weekday()) + event = Event() + event.add("summary", "Mothering Sunday") + event.add("dtstart", date) + event.add("dtend", date + timedelta(days=1)) + cal.add_component(event) + + with open("mothering_sunday.ics", "wb") as f: + f.write(cal.to_ical()) + + +if __name__ == "__main__": + generate_ical()