Lorenzo's daylight saving time fix

This commit is contained in:
Roland Hedberg
2011-03-14 12:58:15 +01:00
parent e9ee4d66ee
commit bb41d9b45e
3 changed files with 24 additions and 15 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -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