Allow multiple date formats for each precision
We want to allow datetime queries to be entered in multiple formats,
e.g. with a 'T' or a space separator between date and time. This
commit sets up that possibility, albeit with an additional dummy
format for the time being.
@@ -534,7 +534,7 @@ class Period(object):
"""
precisions = ('year', 'month', 'day', 'hour', 'minute')
- date_formats = ('%Y', '%Y-%m', '%Y-%m-%d', '%Y-%m-%dT%H', '%Y-%m-%dT%H:%M')
+ date_formats = (('%Y',), ('%Y-%m',), ('%Y-%m-%d',), ('%Y-%m-%dT%H', 'dummy-format'), ('%Y-%m-%dT%H:%M',))
def __init__(self, date, precision):
"""Create a period with the given date (a `datetime` object) and
@@ -555,9 +555,15 @@ class Period(object):
if not string:
return None
date = None
+ found = False
for ordinal, date_format in enumerate(cls.date_formats):
+ if found is True:
+ ordinal -= 1
+ break
+ for format_option in date_format:
try:
- date = datetime.strptime(string, date_format)
+ date = datetime.strptime(string, format_option)
+ found = True
break
except ValueError:
# Parsing failed.
