From bb41d9b45efb302073cd446ae3f9dd08fe9c65bd Mon Sep 17 00:00:00 2001 From: Roland Hedberg Date: Mon, 14 Mar 2011 12:58:15 +0100 Subject: [PATCH] Lorenzo's daylight saving time fix --- src/saml2/time_util.py | 22 ++++++++++------------ src/saml2/validate.py | 4 ++-- tests/test_10_time_util.py | 13 ++++++++++++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/saml2/time_util.py b/src/saml2/time_util.py index d2fcb63..fb20dfb 100644 --- a/src/saml2/time_util.py +++ b/src/saml2/time_util.py @@ -39,7 +39,7 @@ TIME_FORMAT_WITH_FRAGMENT = re.compile( def f_quotient(arg0, arg1, arg2=0): if arg2: return int((arg0-arg1)/(arg2-arg1)) - elif arg0 == 0: + elif not arg0: return 0 else: return int(arg0/arg1) @@ -65,7 +65,7 @@ DAYS_IN_MONTH = { } def days_in_february(year): - if modulo(year, 400) == 0: + if not modulo(year, 400): return 29 elif (modulo(year, 100) != 0) and (modulo(year, 4) == 0): return 29 @@ -135,7 +135,7 @@ def parse_duration(duration): if index == len(duration): break - return (sign, dic) + return sign, dic def add_duration(tid, duration): @@ -270,11 +270,9 @@ def instant(format=None): # --------------------------------------------------------------------------- -def daylight_corrected_now(): - lgmt = list(time.gmtime()) - #lgmt[8] = time.daylight - return time.mktime(lgmt) - +def utc_now(): + return time.mktime(datetime.utcnow().timetuple()) + # --------------------------------------------------------------------------- def not_before(point): @@ -307,10 +305,10 @@ def not_on_or_after(not_on_or_after): elif isinstance(not_on_or_after, basestring): not_on_or_after = str_to_time(not_on_or_after) - if not_on_or_after == 0: + if not not_on_or_after: return True - now = daylight_corrected_now() + now = utc_now() if not_on_or_after and not_on_or_after < now: #self.reset(subject_id, entity_id) @@ -329,7 +327,7 @@ def valid( valid_until ): return True then = str_to_time( valid_until ) - now = time.gmtime() + now = datetime.utcnow().timetuple() if now <= then: return True @@ -341,4 +339,4 @@ def later_than(then, that): that = str_to_time( that ) return then >= that - \ No newline at end of file + diff --git a/src/saml2/validate.py b/src/saml2/validate.py index f472241..c7434c2 100644 --- a/src/saml2/validate.py +++ b/src/saml2/validate.py @@ -57,7 +57,7 @@ def valid_url(url): def validate_on_or_after(not_on_or_after, slack): if not_on_or_after: - now = time_util.daylight_corrected_now() + now = time_util.utc_now() nooa = time.mktime(time_util.str_to_time(not_on_or_after)) high = nooa+slack if now > high: @@ -68,7 +68,7 @@ def validate_on_or_after(not_on_or_after, slack): def validate_before(not_before, slack): if not_before: - now = time_util.daylight_corrected_now() + now = time_util.utc_now() nbefore = time.mktime(time_util.str_to_time(not_before)) if nbefore > now + slack: raise Exception("Can't use it yet %s <= %s" % (nbefore, now)) diff --git a/tests/test_10_time_util.py b/tests/test_10_time_util.py index 0d95ebd..5324cce 100644 --- a/tests/test_10_time_util.py +++ b/tests/test_10_time_util.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import datetime import time from saml2.time_util import f_quotient, modulo, parse_duration, add_duration from saml2.time_util import str_to_time, instant, valid, in_a_while @@ -80,8 +81,10 @@ def test_instant(): def test_valid(): assert valid("2000-01-12T00:00:00Z") == False - assert valid("2011-01-12T00:00:00Z") == True + current_year = datetime.datetime.today().year + assert valid("%d-01-12T00:00:00Z" % (current_year + 1)) == True this_instance = instant() + time.sleep(1) print this_instance assert valid(this_instance) == False # unless on a very fast machine :-) soon = in_a_while(seconds=10) @@ -91,3 +94,11 @@ def test_timeout(): soon = in_a_while(seconds=1) time.sleep(2) assert valid(soon) == False + + +# def test_validate_before(not_before, slack): +# not_before = in_a_while(minutes=5) +# now = time_util.daylight_corrected_now() +# nbefore = time.mktime(time_util.str_to_time(not_before)) +# print nbefore, now +# assert nbefore > now