mothering_sunday_ical/main.py

69 lines
1.7 KiB
Python
Raw Normal View History

2024-05-07 12:37:58 +01:00
"""
Generated by ChatGPT 3.5 (https://chatgpt.com/c/103d4f23-7c8f-484f-9598-d38187e933f2)
"""
from datetime import datetime, timedelta
2024-05-07 13:20:29 +01:00
from ics import Calendar, Event
2024-05-07 12:37:58 +01:00
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
mothers_day = easter_sunday - timedelta(weeks=3)
# Ensure Mothering Sunday falls on a Sunday
if mothers_day.weekday() != 6: # 6 corresponds to Sunday
mothers_day += timedelta(days=6 - mothers_day.weekday())
return mothers_day
2024-05-07 12:37:58 +01:00
def generate_ical() -> None:
"""
Generate an ICAL file for Mothering Sunday (UK)
"""
cal = Calendar()
for year in range(datetime.now().year, datetime.now().year + 10):
date = mothering_sunday(year)
2024-05-07 12:37:58 +01:00
event = Event()
2024-05-07 13:20:29 +01:00
event.name = "Mothering Sunday"
event.begin = date
event.make_all_day()
cal.events.add(event)
2024-05-07 12:37:58 +01:00
2024-05-07 13:20:29 +01:00
with open("mothering_sunday.ics", "w", encoding="utf-8") as f:
f.write(cal.serialize())
2024-05-07 12:37:58 +01:00
if __name__ == "__main__":
generate_ical()