Skip to content

Commit c569598

Browse files
nsklikasc00kiemon5ter
authored andcommitted
Fix bug in time_util library
The add_duration library didn't calculate the days correctly. These fixes make it compliant with the format described in https://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes
1 parent b3635ec commit c569598

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/saml2/time_util.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,20 @@ def add_duration(tid, duration):
140140
carry = f_quotient(temp, 60)
141141
# hours
142142
temp = tid.tm_hour + dur["tm_hour"] + carry
143-
hour = modulo(temp, 60)
144-
carry = f_quotient(temp, 60)
143+
hour = modulo(temp, 24)
144+
carry = f_quotient(temp, 24)
145145
# days
146-
if dur["tm_mday"] > maximum_day_in_month_for(year, month):
146+
if tid.tm_mday > maximum_day_in_month_for(year, month):
147147
temp_days = maximum_day_in_month_for(year, month)
148-
elif dur["tm_mday"] < 1:
148+
elif tid.tm_mday < 1:
149149
temp_days = 1
150150
else:
151-
temp_days = dur["tm_mday"]
152-
days = temp_days + tid.tm_mday + carry
151+
temp_days = tid.tm_mday
152+
days = temp_days + dur["tm_mday"] + carry
153153
while True:
154154
if days < 1:
155-
pass
155+
days = days + maximum_day_in_month_for(year, month - 1)
156+
carry = -1
156157
elif days > maximum_day_in_month_for(year, month):
157158
days -= maximum_day_in_month_for(year, month)
158159
carry = 1

tests/test_10_time_util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_parse_duration_n():
9292
assert d == _val
9393

9494
def test_add_duration_1():
95-
#2000-01-12T12:13:14Z P1Y3M5DT7H10M3S 2001-04-17T19:23:17Z
95+
#2000-01-12T12:13:14Z P1Y3M5DT7H10M3S 2001-04-17T19:23:17Z
9696
t = add_duration(str_to_time("2000-01-12T12:13:14Z"), "P1Y3M5DT7H10M3S")
9797
assert t.tm_year == 2001
9898
assert t.tm_mon == 4
@@ -107,7 +107,7 @@ def test_add_duration_2():
107107
t = add_duration(str_to_time("2000-01-12T00:00:00Z"), "PT33H")
108108
assert t.tm_year == 2000
109109
assert t.tm_mon == 1
110-
assert t.tm_mday == 14
110+
assert t.tm_mday == 13
111111
assert t.tm_hour == 9
112112
assert t.tm_min == 0
113113
assert t.tm_sec == 0
@@ -119,7 +119,7 @@ def test_str_to_time():
119119
#t = time.mktime(str_to_time("2000-01-12T00:00:00Z"))
120120
#assert t == 947631600.0
121121
#TODO: add something to show how this time was arrived at
122-
# do this as an external method in the
122+
# do this as an external method in the
123123
assert t == 947635200
124124
# some IdPs omit the trailing Z, and SAML spec is unclear if it is actually required
125125
t = calendar.timegm(str_to_time("2000-01-12T00:00:00"))

0 commit comments

Comments
 (0)