Skip to content

Commit

Permalink
Merge pull request #2 from rtkrruvinskiy/master
Browse files Browse the repository at this point in the history
Fix expression progression logic and rounding
  • Loading branch information
nickmaccarthy authored Dec 28, 2016
2 parents b66f61a + 3896e2b commit bdaf549
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 5 additions & 11 deletions datemath/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bdaf549

Please sign in to comment.