Skip to content

Commit 75af2a3

Browse files
Add configurable first day of the week
Fixes #293 Signed-off-by: Sandro Bonazzola <[email protected]>
1 parent dcda209 commit 75af2a3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

did/base.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from datetime import timedelta
1414

1515
from dateutil.relativedelta import FR as FRIDAY
16-
from dateutil.relativedelta import MO as MONDAY
1716
from dateutil.relativedelta import relativedelta as delta
1817

1918
from did import utils
@@ -125,6 +124,17 @@ def quarter(self):
125124
f"Invalid quarter start '{month}', should be integer.")
126125
return month
127126

127+
@property
128+
def week(self):
129+
""" The first day of the week, 0 (Monday) by default"""
130+
week = self.parser.get("general", "week", fallback=0)
131+
try:
132+
week = int(week) % 7
133+
except ValueError as exc:
134+
raise ConfigError(
135+
f"Invalid week start '{week}', should be integer.") from exc
136+
return week
137+
128138
@property
129139
def email(self):
130140
""" User email(s) """
@@ -256,14 +266,18 @@ def __sub__(self, subtrahend):
256266
@staticmethod
257267
def this_week():
258268
""" Return start and end date of the current week. """
259-
since = TODAY + delta(weekday=MONDAY(-1))
269+
since = TODAY + delta(day=1)
270+
while since.weekday() != Config().week:
271+
since -= delta(days=1)
260272
until = since + delta(weeks=1)
261273
return Date(since), Date(until)
262274

263275
@staticmethod
264276
def last_week():
265277
""" Return start and end date of the last week. """
266-
since = TODAY + delta(weekday=MONDAY(-2))
278+
since = TODAY - delta(weeks=1)
279+
while since.weekday() != Config().week:
280+
since -= delta(days=1)
267281
until = since + delta(weeks=1)
268282
return Date(since), Date(until)
269283

docs/config.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ Minimum config file should contain at least a ``general`` section
2626
with an email address which will be used for searching. Option
2727
``width`` specifies the maximum width of the report, ``quarter``
2828
can be used to choose a different start month of the quarter.
29+
Option ``week`` can be used to choose a different first day of the week:
30+
defaults to ``0`` which is Monday;
31+
other common setting for this option would be ``6`` which is Sunday.
2932
The ``separator`` and ``separator_width`` options control the
3033
character used, and width of the separator between users::
3134

3235
[general]
3336
email = Petr Šplíchal <[email protected]>
3437
width = 79
3538
quarter = 1
39+
week = 0
3640
separator = #
3741
separator_width = 20
3842

0 commit comments

Comments
 (0)