Skip to content

Commit 8cc5a96

Browse files
committed
Robustify Date.set.
1 parent ef421e2 commit 8cc5a96

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

roundup/date.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ def set(self, spec, offset=0, date_re=date_re,
295295

296296
info = m.groupdict()
297297

298-
# determine whether we need to add anything at the end
298+
# If add_granularity is true, construct the maximum time given
299+
# the precision of the input. For example, given the input
300+
# "12:15", construct "12:15:59". Or, for "2008", construct
301+
# "2008-12-31.23:59:59".
299302
if add_granularity:
300303
for gran in 'SMHdmy':
301304
if info[gran] is not None:
@@ -308,6 +311,8 @@ def set(self, spec, offset=0, date_re=date_re,
308311
else:
309312
add_granularity = Interval('+1%s'%gran)
310313
break
314+
else:
315+
raise ValueError(self._('Could not determine granularity'))
311316

312317
# get the current date as our default
313318
dt = datetime.datetime.utcnow()

test/test_dates.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def testDateInterval(self):
3737
ae(str(date), '2000-02-29.00:00:00')
3838
date = Date("2001-02-27 + 2d")
3939
ae(str(date), '2001-03-01.00:00:00')
40-
40+
date = Date("2009", add_granularity=True)
41+
self.assertRaises(ValueError, Date, ". +30d", add_granularity=True)
42+
4143
def testDate(self):
4244
ae = self.assertEqual
4345
date = Date("2000-04-17")

0 commit comments

Comments
 (0)