From 130c581c282942e01c0ae4c50cc82d2ff070414e Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 15 Jun 2017 18:15:51 -0400 Subject: [PATCH] Some formatting cleanup for relative dates --- beets/dbcore/query.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 2feec08ab..5ef440b5e 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -563,6 +563,8 @@ class Period(object): ('%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S') # second ) relative = {'y': 365, 'm': 30, 'w': 7, 'd': 1} + relative_re = '(?P[+|-]?)(?P[0-9]+)' + \ + '(?P[y|m|w|d])' def __init__(self, date, precision): """Create a period with the given date (a `datetime` object) and @@ -606,24 +608,26 @@ class Period(object): if not string: return None - pattern_dq = '(?P[+|-]?)(?P[0-9]+)(?P[y|m|w|d])' # noqa: E501 - match_dq = re.match(pattern_dq, string) - # test if the string matches the relative date pattern, add the parsed - # quantity to now in that case - if match_dq is not None: + # Check for a relative date. + match_dq = re.match(cls.relative_re, string) + if match_dq: sign = match_dq.group('sign') quantity = match_dq.group('quantity') timespan = match_dq.group('timespan') + + # Add or subtract the given amount of time from the current + # date. multiplier = -1 if sign == '-' else 1 days = cls.relative[timespan] - date = datetime.now() + multiplier * timedelta( - days=int(quantity) * days) + date = datetime.now() + \ + timedelta(days=int(quantity) * days) * multiplier string = date.strftime(cls.date_formats[5][0]) + # Check for an absolute date. date, ordinal = find_date_and_format(string) if date is None: raise InvalidQueryArgumentValueError(string, - 'a valid datetime string') + 'a valid date/time string') precision = cls.precisions[ordinal] return cls(date, precision)