Add a date query precision of ‘hour’

This commit is contained in:
discopatrick 2017-04-25 01:05:46 +01:00
parent 68089ac8e9
commit 61b832990f
2 changed files with 8 additions and 2 deletions

View file

@ -533,8 +533,8 @@ class Period(object):
instants of time during January 2014.
"""
precisions = ('year', 'month', 'day')
date_formats = ('%Y', '%Y-%m', '%Y-%m-%d')
precisions = ('year', 'month', 'day', 'hour')
date_formats = ('%Y', '%Y-%m', '%Y-%m-%d', '%Y-%m-%dT%H')
def __init__(self, date, precision):
"""Create a period with the given date (a `datetime` object) and
@ -582,6 +582,8 @@ class Period(object):
return date.replace(year=date.year + 1, month=1)
elif 'day' == precision:
return date + timedelta(days=1)
elif 'hour' == precision:
return date + timedelta(hours=1)
else:
raise ValueError(u'unhandled precision {0}'.format(precision))

View file

@ -58,6 +58,10 @@ class DateIntervalTest(unittest.TestCase):
self.assertExcludes('1999-12..2000-02', '1999-11-30T23:59:59')
self.assertExcludes('1999-12..2000-02', '2000-03-01T00:00:00')
def test_hour_precision_intervals(self):
self.assertContains('2000-01-01T12..2000-01-01T13',
'2000-01-01T12:30:00')
def test_unbounded_endpoints(self):
self.assertContains('..', date=datetime.max)
self.assertContains('..', date=datetime.min)