From 600657b1a803e9b025c716544bbb46e450cd5415 Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 10 Jan 2025 00:31:38 +0100 Subject: [PATCH] `get_end_of_semester` util function --- core/utils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/core/utils.py b/core/utils.py index cdd72fa6..6b72bde8 100644 --- a/core/utils.py +++ b/core/utils.py @@ -14,7 +14,7 @@ # from dataclasses import dataclass -from datetime import date +from datetime import date, timedelta # Image utils from io import BytesIO @@ -77,6 +77,22 @@ def get_start_of_semester(today: date | None = None) -> date: return autumn.replace(year=autumn.year - 1) +def get_end_of_semester(today: date | None = None): + """Return the date of the end of the semester of the given date. + If no date is given, return the end date of the current semester. + """ + # the algorithm is simple, albeit somewhat imprecise : + # 1. get the start of the next semester + # 2. Remove a month and a half for the autumn semester (summer holidays) + # and 28 days for spring semester (february holidays) + if today is None: + today = localdate() + semester_start = get_start_of_semester(today + timedelta(days=365 // 2)) + if semester_start.month == settings.SITH_SEMESTER_START_AUTUMN[0]: + return semester_start - timedelta(days=45) + return semester_start - timedelta(days=28) + + def get_semester_code(d: date | None = None) -> str: """Return the semester code of the given date. If no date is given, return the semester code of the current semester.