Skip to content

Commit

Permalink
Merge bd1fb00 into dc7e783
Browse files Browse the repository at this point in the history
  • Loading branch information
onegreyonewhite committed Aug 14, 2018
2 parents dc7e783 + bd1fb00 commit c1c10fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pytimeparse/tests/testtimeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ def test_timeparse_granularity_4(self):
self.assertEqual(timeparse.timeparse('+0:02', granularity='seconds'), 2)
self.assertEqual(timeparse.timeparse('-0:02', granularity='seconds'), -2)

def test_timeparse_unparsed(self):
'''Check that unparsed values tries to converts into int(). '''
self.assertEqual(timeparse.timeparse(100), 100)
self.assertEqual(timeparse.timeparse(-18.333), -18)
self.assertEqual(timeparse.timeparse('99.1'), 99)
self.assertEqual(timeparse.timeparse('-99.1'), -99)

def test_timeparse_11(self):
'''timeparse test case 11.'''
# uptime format
Expand Down
9 changes: 8 additions & 1 deletion pytimeparse/timeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import re

SIGN = r'(?P<sign>[+|-])?'
SIGN = r'(?P<sign>[+|-]|\+)?'
#YEARS = r'(?P<years>\d+)\s*(?:ys?|yrs?.?|years?)'
#MONTHS = r'(?P<months>\d+)\s*(?:mos?.?|mths?.?|months?)'
WEEKS = r'(?P<weeks>[\d.]+)\s*(?:w|wks?|weeks?)'
Expand Down Expand Up @@ -153,6 +153,8 @@ def timeparse(sval, granularity='seconds'):
>>> timeparse('1:30', granularity='minutes')
5400
'''
if isinstance(sval, (int, float)):
return int(sval)
match = COMPILED_SIGN.match(sval)
sign = -1 if match.groupdict()['sign'] == '-' else 1
sval = match.groupdict()['unsigned']
Expand All @@ -179,3 +181,8 @@ def timeparse(sval, granularity='seconds'):
# SECS is a float, we will return a float
return sign * sum([MULTIPLIERS[k] * float(v) for (k, v) in
list(mdict.items()) if v is not None])

try:
return int(float(sval)) * sign
except ValueError:
pass

0 comments on commit c1c10fe

Please sign in to comment.