diff --git a/datemath/helpers.py b/datemath/helpers.py index d3e5096..1a8af5f 100644 --- a/datemath/helpers.py +++ b/datemath/helpers.py @@ -168,23 +168,17 @@ def evaluate(expression, now, timeZone='UTC'): if debug: print('Expression: {0}'.format(expression)) if debug: print('Now: {0}'.format(now)) val = 0 - for i, c in enumerate(expression): + i = 0 + while i < len(expression): char = expression[i] - if i >= len(expression): - raise('Truncated datemath: {0}'.format(expression)) - if '/' in char: - # then we need to round up + # then we need to round next = str(expression[i+1]) - - roundUp = True + i += 1 now = roundDate(now, unitMap(next).rstrip('s'), timeZone) elif char == '+' or char == '-': - if i >= len(expression): - raise DateMathException('Truncated datemath: {0}'.format(expression)) - val = 0 try: @@ -202,7 +196,7 @@ def evaluate(expression, now, timeZone='UTC'): elif re.match('[a-zA-Z]+', char): now = calculate(now, val, unitMap(char)) - i = i+1 + i += 1 if debug: print("Fin: {0}".format(now)) if debug: print('\n\n') return now diff --git a/tests.py b/tests.py index d851e9f..a5d8b47 100644 --- a/tests.py +++ b/tests.py @@ -57,6 +57,10 @@ def testParse(self): self.assertEqual(dm('/M+2d').format(iso8601), arrow.utcnow().floor('month').replace(days=+2).format(iso8601)) self.assertEqual(dm('now/w+2d-2h').format(iso8601), arrow.utcnow().floor('week').replace(days=+2, hours=-2).format(iso8601)) self.assertEqual(dm('now/M+1w-2h+10s').format(iso8601), arrow.utcnow().floor('month').replace(weeks=+1, hours=-2, seconds=+10).format(iso8601)) + self.assertEqual(dm('now-1d/d').format(iso8601), arrow.utcnow().replace(days=-1).floor('day').format(iso8601)) + self.assertEqual(dm('now+1d/d').format(iso8601), arrow.utcnow().replace(days=1).floor('day').format(iso8601)) + self.assertEqual(dm('now-10d/d').format(iso8601), arrow.utcnow().replace(days=-10).floor('day').format(iso8601)) + self.assertEqual(dm('now+10d/d').format(iso8601), arrow.utcnow().replace(days=10).floor('day').format(iso8601)) # future