From 5e717126f5e751edcca30fdd25b1a0e1abcbbfeb Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Sun, 15 Sep 2019 22:39:03 +0200 Subject: [PATCH] Add method to get calendar weeks within date range. --- biscuit/apps/chronos/util.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/biscuit/apps/chronos/util.py b/biscuit/apps/chronos/util.py index f3988f42..7ce3b0da 100644 --- a/biscuit/apps/chronos/util.py +++ b/biscuit/apps/chronos/util.py @@ -28,6 +28,21 @@ class CalendarWeek: return cls().week + @classmethod + def weeks_within(cls, start: date, end: date) -> Sequence[CalendarWeek]: + """ Get all calendar weeks within a date range. """ + + if start > end: + raise ValueError('End date must be after start date.') + + current = start + weeks = [] + while current < end: + weeks.append(cls.from_date(current)) + current += timedelta(days=7) + + return weeks + def __post_init__(self) -> None: today = date.today() -- GitLab