Skip to content

Commit 130c581

Browse files
committed
Some formatting cleanup for relative dates
1 parent 8a6c8cd commit 130c581

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

beets/dbcore/query.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ class Period(object):
563563
('%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S') # second
564564
)
565565
relative = {'y': 365, 'm': 30, 'w': 7, 'd': 1}
566+
relative_re = '(?P<sign>[+|-]?)(?P<quantity>[0-9]+)' + \
567+
'(?P<timespan>[y|m|w|d])'
566568

567569
def __init__(self, date, precision):
568570
"""Create a period with the given date (a `datetime` object) and
@@ -606,24 +608,26 @@ def find_date_and_format(string):
606608
if not string:
607609
return None
608610

609-
pattern_dq = '(?P<sign>[+|-]?)(?P<quantity>[0-9]+)(?P<timespan>[y|m|w|d])' # noqa: E501
610-
match_dq = re.match(pattern_dq, string)
611-
# test if the string matches the relative date pattern, add the parsed
612-
# quantity to now in that case
613-
if match_dq is not None:
611+
# Check for a relative date.
612+
match_dq = re.match(cls.relative_re, string)
613+
if match_dq:
614614
sign = match_dq.group('sign')
615615
quantity = match_dq.group('quantity')
616616
timespan = match_dq.group('timespan')
617+
618+
# Add or subtract the given amount of time from the current
619+
# date.
617620
multiplier = -1 if sign == '-' else 1
618621
days = cls.relative[timespan]
619-
date = datetime.now() + multiplier * timedelta(
620-
days=int(quantity) * days)
622+
date = datetime.now() + \
623+
timedelta(days=int(quantity) * days) * multiplier
621624
string = date.strftime(cls.date_formats[5][0])
622625

626+
# Check for an absolute date.
623627
date, ordinal = find_date_and_format(string)
624628
if date is None:
625629
raise InvalidQueryArgumentValueError(string,
626-
'a valid datetime string')
630+
'a valid date/time string')
627631
precision = cls.precisions[ordinal]
628632
return cls(date, precision)
629633

0 commit comments

Comments
 (0)