mothering_sunday_ical/main.py

69 lines
1.7 KiB
Python

"""
Generated by ChatGPT 3.5 (https://chatgpt.com/c/103d4f23-7c8f-484f-9598-d38187e933f2)
"""
from datetime import datetime, timedelta
from ics 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
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
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)
event = Event()
event.name = "Mothering Sunday"
event.begin = date
event.make_all_day()
cal.events.add(event)
with open("mothering_sunday.ics", "w", encoding="utf-8") as f:
f.write(cal.serialize())
if __name__ == "__main__":
generate_ical()