From 6ce5b0379a26b8ef79cd380f717aa960f4edd93e Mon Sep 17 00:00:00 2001 From: Sandro Bonazzola Date: Thu, 2 Feb 2023 15:50:00 +0100 Subject: [PATCH] Add configurable first day of the week Fixes #293 Signed-off-by: Sandro Bonazzola --- did/base.py | 20 +++++++++++++++++--- docs/config.rst | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/did/base.py b/did/base.py index 1d917789..d131598e 100644 --- a/did/base.py +++ b/did/base.py @@ -13,7 +13,6 @@ from datetime import timedelta from dateutil.relativedelta import FR as FRIDAY -from dateutil.relativedelta import MO as MONDAY from dateutil.relativedelta import relativedelta as delta from did import utils @@ -125,6 +124,17 @@ def quarter(self): f"Invalid quarter start '{month}', should be integer.") return month + @property + def week(self): + """ The first day of the week, 0 (Monday) by default""" + week = self.parser.get("general", "week", fallback=0) + try: + week = int(week) % 7 + except ValueError as exc: + raise ConfigError( + f"Invalid week start '{week}', should be integer.") from exc + return week + @property def email(self): """ User email(s) """ @@ -256,14 +266,18 @@ def __sub__(self, subtrahend): @staticmethod def this_week(): """ Return start and end date of the current week. """ - since = TODAY + delta(weekday=MONDAY(-1)) + since = TODAY + while since.weekday() != Config().week: + since -= delta(days=1) until = since + delta(weeks=1) return Date(since), Date(until) @staticmethod def last_week(): """ Return start and end date of the last week. """ - since = TODAY + delta(weekday=MONDAY(-2)) + since = TODAY - delta(weeks=1) + while since.weekday() != Config().week: + since -= delta(days=1) until = since + delta(weeks=1) return Date(since), Date(until) diff --git a/docs/config.rst b/docs/config.rst index 36404f0b..849998ce 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -26,6 +26,9 @@ Minimum config file should contain at least a ``general`` section with an email address which will be used for searching. Option ``width`` specifies the maximum width of the report, ``quarter`` can be used to choose a different start month of the quarter. +Option ``week`` can be used to choose a different first day of the week: +defaults to ``0`` which is Monday; +other common setting for this option would be ``6`` which is Sunday. The ``separator`` and ``separator_width`` options control the character used, and width of the separator between users:: @@ -33,6 +36,7 @@ character used, and width of the separator between users:: email = Petr Šplíchal width = 79 quarter = 1 + week = 0 separator = # separator_width = 20