Skip to content

Commit b9a7f28

Browse files
committed
catch invalid timeunit and throw error
1 parent eb12034 commit b9a7f28

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

datemath/helpers.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# !/usr/bin/python
2+
# coding=utf-8
3+
14
'''
25
A basic utility module for parsing math like strings relating to dates
36
@@ -195,18 +198,24 @@ def evaluate(expression, now, timeZone='UTC', roundDown=True):
195198

196199
try:
197200
m = re.match('(\d*[.]?\d+)[\w+-/]', expression[i+1:])
198-
num = m.group(1)
199-
val = val * 10 + float(num)
200-
i = i + len(num)
201+
if m:
202+
num = m.group(1)
203+
val = val * 10 + float(num)
204+
i = i + len(num)
205+
else:
206+
raise DateMathException('''Unable to determine a proper time qualifier. Do you have a proper numerical number followed by a valid time unit? i.e. '+1d', '-3d/d', etc.''')
201207
except Exception as e:
202-
raise DateMathException("Invalid numerical datematch: What I got was - match: {0}, expression: {1}, error: {2}".format(expression[i+1:], expression, e))
208+
'''Invalid numerical datematch'''
209+
raise DateMathException("Invalid datematch: What I got was - re.match: {0}, expression: {1}, error: {2}".format(expression[i+1:], expression, e))
203210

204211
if char == '+':
205212
val = float(val)
206213
else:
207214
val = float(-val)
208215
elif re.match('[a-zA-Z]+', char):
209216
now = calculate(now, val, unitMap(char))
217+
else:
218+
raise DateMathException(''''{}' is not a valid timeunit for expression: '{}' '''.format(char, expression))
210219

211220
i += 1
212221
if debug: print("Fin: {0}".format(now))

0 commit comments

Comments
 (0)