Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement did last [monday..sunday] #342

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 38 additions & 6 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 @@ -318,7 +323,7 @@
@staticmethod
def period(argument):
""" Detect desired time period for the argument """
since, until, period = None, None, None

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.
This assignment to 'until' is unnecessary as it is
redefined
before this value is used.

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
This assignment to 'period' is unnecessary as it is
redefined
before this value is used.
if "today" in argument:
since = Date("today")
until = Date("today")
Expand All @@ -329,13 +334,40 @@
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()
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
Loading