refactored readTimeDelta function.
This commit is contained in:
parent
30c78107af
commit
011a0859a0
|
@ -104,50 +104,41 @@ timeUnits = FuzzyDict({
|
||||||
def readTimeDelta(s):
|
def readTimeDelta(s):
|
||||||
"""convert a string like "2 days" or "1h2d3w" into seconds"""
|
"""convert a string like "2 days" or "1h2d3w" into seconds"""
|
||||||
# split number and words
|
# split number and words
|
||||||
L = []
|
digit = string = number = None
|
||||||
digit = buffer = None
|
seconds = 0
|
||||||
for c in s:
|
for c in s:
|
||||||
if c.isdigit():
|
if c.isdigit():
|
||||||
if buffer is None:
|
if string is None:
|
||||||
digit, buffer = True, ''
|
# start
|
||||||
|
digit, string = True, ''
|
||||||
elif digit is False:
|
elif digit is False:
|
||||||
digit = True
|
digit = True
|
||||||
# add unit to list
|
# completed an unit, add to seconds
|
||||||
buffer = buffer.strip()
|
string = string.strip()
|
||||||
if buffer:
|
if string:
|
||||||
L.append(buffer)
|
mult = timeUnits[string]
|
||||||
buffer = ''
|
seconds += number * mult
|
||||||
buffer += c
|
string = ''
|
||||||
|
string += c
|
||||||
else:
|
else:
|
||||||
if digit is None:
|
if digit is None:
|
||||||
# need a number first
|
# need a number first
|
||||||
continue
|
continue
|
||||||
if digit is True:
|
if digit is True:
|
||||||
digit = False
|
digit = False
|
||||||
# add number to list
|
# completed a number
|
||||||
L.append(int(buffer))
|
number, string = int(string), ''
|
||||||
buffer = ''
|
string += c
|
||||||
buffer += c
|
|
||||||
# check last
|
|
||||||
if buffer.isdigit():
|
|
||||||
L.append(int(buffer))
|
|
||||||
else:
|
|
||||||
buffer = buffer.strip()
|
|
||||||
if buffer:
|
|
||||||
L.append(buffer)
|
|
||||||
|
|
||||||
# sum
|
# check last string
|
||||||
seconds = 0
|
if string.isdigit():
|
||||||
number = None
|
seconds += int(string)
|
||||||
for item in L:
|
else:
|
||||||
if isinstance(item, int):
|
string = string.strip()
|
||||||
number = item
|
if string:
|
||||||
else:
|
mult = timeUnits[string]
|
||||||
mult = timeUnits[item]
|
|
||||||
seconds += number * mult
|
seconds += number * mult
|
||||||
number = None
|
|
||||||
if number:
|
|
||||||
seconds += number
|
|
||||||
return seconds
|
return seconds
|
||||||
|
|
||||||
def capab(user, capability):
|
def capab(user, capability):
|
||||||
|
|
Loading…
Reference in New Issue