Commit b46691b6 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Allow for supplying an offset for relative dates.

parent 0788dad0
......@@ -3,7 +3,7 @@
from datetime import date, timedelta
import re
def _convert_pattern(p_length, p_periodunit):
def _convert_pattern(p_length, p_periodunit, p_offset=date.today()):
"""
Converts a pattern in the form [0-9][dwmy] and returns a date from today
with the period of time added to it.
......@@ -13,15 +13,15 @@ def _convert_pattern(p_length, p_periodunit):
p_length = int(p_length)
if p_periodunit == 'd':
result = date.today() + timedelta(p_length)
result = p_offset + timedelta(p_length)
elif p_periodunit == 'w':
result = date.today() + timedelta(weeks=p_length)
result = p_offset + timedelta(weeks=p_length)
elif p_periodunit == 'm':
# we'll consider a month to be 30 days
result = date.today() + timedelta(30 * p_length)
result = p_offset + timedelta(30 * p_length)
elif p_periodunit == 'y':
# we'll consider a year to be 365 days (yeah, I'm aware of leap years)
result = date.today() + timedelta(365 * p_length)
result = p_offset + timedelta(365 * p_length)
return result
......@@ -50,7 +50,7 @@ def _convert_weekday_pattern(p_weekday):
shift = (target_day - day) % 7
return date.today() + timedelta(shift)
def relative_date_to_date(p_date):
def relative_date_to_date(p_date, p_offset=date.today()):
"""
Transforms a relative date into a date object.
......@@ -70,7 +70,7 @@ def relative_date_to_date(p_date):
if relative:
length = relative.group('length')
period = relative.group('period')
result = _convert_pattern(length, period)
result = _convert_pattern(length, period, p_offset)
elif weekday:
result = _convert_weekday_pattern(weekday.group(0))
......
......@@ -60,6 +60,11 @@ class RelativeDateTester(unittest.TestCase):
result = RelativeDate.relative_date_to_date('tod')
self.assertEquals(result, self.today)
def test_today3(self):
result = RelativeDate.relative_date_to_date('today', \
date.today() + timedelta(1))
self.assertEquals(result, self.today)
def test_tomorrow1(self):
result = RelativeDate.relative_date_to_date('Tomorrow')
self.assertEquals(result, self.tomorrow)
......@@ -83,3 +88,7 @@ class RelativeDateTester(unittest.TestCase):
def test_monday4(self):
result = RelativeDate.relative_date_to_date('mondayy')
self.assertFalse(result)
def test_offset1(self):
result = RelativeDate.relative_date_to_date('1d', self.tomorrow)
self.assertEquals(result, date.today() + timedelta(2))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment