Skip to content

Commit

Permalink
implement last [monday..sunday]
Browse files Browse the repository at this point in the history
Fixes: #305
  • Loading branch information
xsuchy committed Dec 5, 2023
1 parent 71e2580 commit b2c2ff6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 39 additions & 7 deletions did/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -329,13 +334,40 @@ def period(argument):
until = Date("yesterday")
until.date += delta(days=1)
period = "yesterday"
elif "friday" in argument:
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"
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 = today
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()
Expand Down
3 changes: 2 additions & 1 deletion did/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b2c2ff6

Please sign in to comment.