Move the Sunday check into the calculation function

This commit is contained in:
Scott Wallace 2024-05-11 12:04:53 +01:00
parent 04baff10a6
commit 9652429839
Signed by: scott
SSH key fingerprint: SHA256:+LJug6Dj01Jdg86CILGng9r0lJseUrpI0xfRqdW9Uws

13
main.py
View file

@ -36,8 +36,13 @@ def mothering_sunday(year: int) -> datetime: # pylint: disable=too-many-locals
easter_sunday = datetime(year, month, day)
# Calculate Mothering Sunday
final_day = easter_sunday - timedelta(weeks=3)
return final_day
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:
@ -48,9 +53,7 @@ def generate_ical() -> None:
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.name = "Mothering Sunday"
event.begin = date