mothering_sunday_ical/main.py
2024-05-07 12:37:58 +01:00

68 lines
1.8 KiB
Python

"""
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()