From 96524298398e0f046c15bbc9efd17b77eac290d9 Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Sat, 11 May 2024 12:04:53 +0100 Subject: [PATCH] Move the Sunday check into the calculation function --- main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index e5981e8..1be06c5 100644 --- a/main.py +++ b/main.py @@ -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