Skip to content

Commit

Permalink
Add configurable first day of the week
Browse files Browse the repository at this point in the history
Fixes psss#293

Signed-off-by: Sandro Bonazzola <sbonazzo@redhat.com>
  • Loading branch information
sandrobonazzola committed Feb 8, 2023
1 parent dcda209 commit 6ce5b03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions did/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) """
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ 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::

[general]
email = Petr Šplíchal <psplicha@redhat.com>
width = 79
quarter = 1
week = 0
separator = #
separator_width = 20

Expand Down

0 comments on commit 6ce5b03

Please sign in to comment.