handling of duration was not correct
This commit is contained in:
@@ -91,15 +91,27 @@ D_FORMAT = [
|
||||
]
|
||||
|
||||
def parse_duration(duration):
|
||||
# PnYnMnDTnHnMnS
|
||||
assert duration[0] == "P"
|
||||
index = 1
|
||||
dic = {}
|
||||
# (-)PnYnMnDTnHnMnS
|
||||
index = 0
|
||||
if duration[0] == '-':
|
||||
sign = '-'
|
||||
index += 1
|
||||
else:
|
||||
sign = '+'
|
||||
assert duration[index] == "P"
|
||||
index += 1
|
||||
|
||||
dic = dict([(typ, 0) for (code, typ) in D_FORMAT])
|
||||
|
||||
for code, typ in D_FORMAT:
|
||||
#print duration[index:], code
|
||||
if duration[index] == '-':
|
||||
raise Exception("Negation not allowed on individual items")
|
||||
if code == "T":
|
||||
if duration[index] == "T":
|
||||
index += 1
|
||||
if index == len(duration):
|
||||
raise Exception("Not allowed to end with 'T'")
|
||||
else:
|
||||
raise Exception("Missing T")
|
||||
else:
|
||||
@@ -108,17 +120,28 @@ def parse_duration(duration):
|
||||
try:
|
||||
dic[typ] = int(duration[index:index+mod])
|
||||
except ValueError:
|
||||
if code == "S":
|
||||
try:
|
||||
dic[typ] = float(duration[index:index+mod])
|
||||
except ValueError:
|
||||
raise Exception("Not a float")
|
||||
else:
|
||||
raise Exception(
|
||||
"Fractions not allow on anything byt seconds")
|
||||
index = mod+index+1
|
||||
except ValueError:
|
||||
dic[typ] = 0
|
||||
|
||||
return dic
|
||||
if index == len(duration):
|
||||
break
|
||||
|
||||
return (sign, dic)
|
||||
|
||||
def add_duration(tid, duration):
|
||||
|
||||
dur = parse_duration(duration)
|
||||
(sign, dur) = parse_duration(duration)
|
||||
|
||||
if sign == '+':
|
||||
#Months
|
||||
temp = tid.tm_mon + dur["tm_mon"]
|
||||
month = modulo(temp, 1, 13)
|
||||
@@ -159,6 +182,8 @@ def add_duration(tid, duration):
|
||||
|
||||
return time.localtime(time.mktime((year, month, days, hour, minutes,
|
||||
secs, 0, 0, -1)))
|
||||
else:
|
||||
pass
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -175,6 +200,19 @@ def time_in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0,
|
||||
soon = now + delta
|
||||
return soon
|
||||
|
||||
def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0,
|
||||
minutes=0, hours=0, weeks=0):
|
||||
"""
|
||||
format of timedelta:
|
||||
timedelta([days[, seconds[, microseconds[, milliseconds[,
|
||||
minutes[, hours[, weeks]]]]]]])
|
||||
"""
|
||||
now = datetime.utcnow()
|
||||
delta = timedelta(*[days, seconds, microseconds, milliseconds, minutes,
|
||||
hours, weeks])
|
||||
prev = now - delta
|
||||
return prev
|
||||
|
||||
def in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0,
|
||||
minutes=0, hours=0, weeks=0):
|
||||
"""
|
||||
@@ -185,6 +223,11 @@ def in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0,
|
||||
return time_in_a_while(days, seconds, microseconds, milliseconds,
|
||||
minutes, hours, weeks).strftime(TIME_FORMAT)
|
||||
|
||||
def a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0,
|
||||
minutes=0, hours=0, weeks=0):
|
||||
return time_a_while_ago(days, seconds, microseconds, milliseconds,
|
||||
minutes, hours, weeks).strftime(TIME_FORMAT)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def str_to_time(timestr):
|
||||
@@ -214,6 +257,18 @@ def daylight_corrected_now():
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def not_before(point):
|
||||
if not point:
|
||||
return True
|
||||
|
||||
then = str_to_time(point)
|
||||
now = time.gmtime()
|
||||
|
||||
if now > then:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def valid( valid_until ):
|
||||
""" Checks whether a valid_until specification is still valid
|
||||
:param valid_until: The string representation of a time
|
||||
@@ -233,7 +288,7 @@ def valid( valid_until ):
|
||||
|
||||
def later_than(then, that):
|
||||
then = str_to_time( then )
|
||||
then = str_to_time( that )
|
||||
that = str_to_time( that )
|
||||
|
||||
return then >= that
|
||||
|
||||
@@ -37,7 +37,8 @@ def test_modulo_2():
|
||||
#assert modulo(13+x, 1, 13) == 1+x
|
||||
|
||||
def test_parse_duration():
|
||||
d = parse_duration("P1Y3M5DT7H10M3.3S")
|
||||
(sign, d) = parse_duration("P1Y3M5DT7H10M3.3S")
|
||||
assert sign == "+"
|
||||
assert d['tm_sec'] == 3.3
|
||||
assert d['tm_mon'] == 3
|
||||
assert d['tm_hour'] == 7
|
||||
|
||||
Reference in New Issue
Block a user