From cf62044ce55c431740ce19c47df42705de5b18da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Such=C3=BD?= Date: Mon, 20 Nov 2023 12:36:37 +0100 Subject: [PATCH] Implement `did last [monday..sunday]` Fixes: #305 --- README.rst | 2 +- did/base.py | 44 ++++++++++++++++++++++++++++++++++++++------ did/cli.py | 3 ++- tests/test_base.py | 24 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 8eaf26b3..e726d8b8 100644 --- a/README.rst +++ b/README.rst @@ -31,7 +31,7 @@ Synopsis Usage is straightforward:: - did [this|last] [week|month|quarter|year] [opts] + did [this|last] [week|month|quarter|year|monday|..|sunday] [opts] Examples diff --git a/did/base.py b/did/base.py index 8cd8badd..1becb310 100644 --- a/did/base.py +++ b/did/base.py @@ -14,6 +14,11 @@ from dateutil.relativedelta import FR as FRIDAY from dateutil.relativedelta import MO as MONDAY +from dateutil.relativedelta import SA as SATURDAY +from dateutil.relativedelta import SU as SUNDAY +from dateutil.relativedelta import TH as THURSDAY +from dateutil.relativedelta import TU as TUESDAY +from dateutil.relativedelta import WE as WEDNESDAY from dateutil.relativedelta import relativedelta as delta from did import utils @@ -329,13 +334,40 @@ def period(argument): until = Date("yesterday") until.date += delta(days=1) period = "yesterday" - elif "friday" in argument: + elif "monday" in argument or "tuesday" in argument or \ + "wednesday" in argument or "thursday" in argument or \ + "friday" in argument or "saturday" in argument or \ + "sunday" in argument: + today = Date("today") since = Date("today") - until = Date("today") - since.date += delta(weekday=FRIDAY(-1)) - until.date += delta(weekday=FRIDAY(-1)) - until.date += delta(days=1) - period = "the last friday" + until = Date() + if "monday" in argument: + weekday = MONDAY(-1) + period = "the last monday" + elif "tuesday" in argument: + weekday = TUESDAY(-1) + period = "the last tuesday" + elif "wednesday" in argument: + weekday = WEDNESDAY(-1) + period = "the last wednesday" + elif "thursday" in argument: + weekday = THURSDAY(-1) + period = "the last thursday" + elif "friday" in argument: + weekday = FRIDAY(-1) + period = "the last friday" + elif "saturday" in argument: + weekday = SATURDAY(-1) + period = "the last saturday" + else: + weekday = SUNDAY(-1) + period = "the last sunday" + since.date += delta(weekday=weekday) + if since.date == today.date: + # technically last dayofweek is today, but we want + # the one week ago + since.date -= delta(days=7) + until.date = since.date + delta(days=1) elif "year" in argument: if "last" in argument: since, until = Date.last_year() diff --git a/did/cli.py b/did/cli.py index 8c51acc5..d7ca6004 100644 --- a/did/cli.py +++ b/did/cli.py @@ -169,7 +169,8 @@ def parse(self): def check(self): """ Perform additional check for given options """ keywords = [ - 'today', 'yesterday', 'friday', + 'today', 'yesterday', 'monday', 'tuesday', 'wednesday', 'thursday', + 'friday', 'saturday', 'sunday', 'this', 'last', 'week', 'month', 'quarter', 'year'] for argument in self.arg: diff --git a/tests/test_base.py b/tests/test_base.py index ae5f0315..ac757d97 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -73,6 +73,30 @@ def test_Date_period(): assert str(since) == "2015-09-21" assert str(until) == "2015-09-28" assert period == "the week 39" + # Last Monday + for argument in ["last monday"]: + since, until, period = Date.period(argument) + assert str(since) == "2015-09-28" + assert str(until) == "2015-09-29" + assert period == "the last monday" + # Last Tuesday + for argument in ["last tuesday"]: + since, until, period = Date.period(argument) + assert str(since) == "2015-09-29" + assert str(until) == "2015-09-30" + assert period == "the last tuesday" + # Last Wednesday + for argument in ["last wednesday"]: + since, until, period = Date.period(argument) + assert str(since) == "2015-09-30" + assert str(until) == "2015-10-01" + assert period == "the last wednesday" + # Last Thursday + for argument in ["last thursday"]: + since, until, period = Date.period(argument) + assert str(since) == "2015-10-01" + assert str(until) == "2015-10-02" + assert period == "the last thursday" # Last Friday for argument in ["last friday"]: since, until, period = Date.period(argument)