@@ -563,6 +563,8 @@ class Period(object):
563
563
('%Y-%m-%dT%H:%M:%S' , '%Y-%m-%d %H:%M:%S' ) # second
564
564
)
565
565
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])'
566
568
567
569
def __init__ (self , date , precision ):
568
570
"""Create a period with the given date (a `datetime` object) and
@@ -606,24 +608,26 @@ def find_date_and_format(string):
606
608
if not string :
607
609
return None
608
610
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 :
614
614
sign = match_dq .group ('sign' )
615
615
quantity = match_dq .group ('quantity' )
616
616
timespan = match_dq .group ('timespan' )
617
+
618
+ # Add or subtract the given amount of time from the current
619
+ # date.
617
620
multiplier = - 1 if sign == '-' else 1
618
621
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
621
624
string = date .strftime (cls .date_formats [5 ][0 ])
622
625
626
+ # Check for an absolute date.
623
627
date , ordinal = find_date_and_format (string )
624
628
if date is None :
625
629
raise InvalidQueryArgumentValueError (string ,
626
- 'a valid datetime string' )
630
+ 'a valid date/time string' )
627
631
precision = cls .precisions [ordinal ]
628
632
return cls (date , precision )
629
633
0 commit comments