Skip to content

Commit 3740d80

Browse files
author
Roland Hedberg
committed
Fixed parse_duration.
1 parent bfa1de3 commit 3740d80

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/saml2/time_util.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def parse_duration(duration):
8181
assert duration[index] == "P"
8282
index += 1
8383

84-
dic = dict([(typ, 0) for (code, typ) in D_FORMAT])
85-
84+
dic = dict([(typ, 0) for (code, typ) in D_FORMAT if typ])
85+
dlen = len(duration)
86+
8687
for code, typ in D_FORMAT:
8788
#print duration[index:], code
8889
if duration[index] == '-':
@@ -99,22 +100,31 @@ def parse_duration(duration):
99100
else:
100101
try:
101102
mod = duration[index:].index(code)
103+
_val = duration[index:index + mod]
102104
try:
103-
dic[typ] = int(duration[index:index + mod])
105+
dic[typ] = int(_val)
104106
except ValueError:
105-
if code == "S":
107+
# smallest value used may also have a decimal fraction
108+
if mod + index + 1 == dlen:
106109
try:
107-
dic[typ] = float(duration[index:index + mod])
110+
dic[typ] = float(_val)
108111
except ValueError:
109-
raise Exception("Not a float")
112+
if "," in _val:
113+
_val = _val.replace(",", ".")
114+
try:
115+
dic[typ] = float(_val)
116+
except ValueError:
117+
raise Exception("Not a float")
118+
else:
119+
raise Exception("Not a float")
110120
else:
111-
raise Exception(
112-
"Fractions not allow on anything byt seconds")
121+
raise ValueError(
122+
"Fraction not allowed on other than smallest value")
113123
index = mod + index + 1
114124
except ValueError:
115125
dic[typ] = 0
116126

117-
if index == len(duration):
127+
if index == dlen:
118128
break
119129

120130
return sign, dic

0 commit comments

Comments
 (0)