util: fix time_format.iso_utc_time_to_seconds() so that it works even in London

This commit is contained in:
Zooko O'Whielacronx 2009-06-11 15:11:29 -07:00
parent 2a51a7bb90
commit 8978cb0738
2 changed files with 37 additions and 5 deletions

View File

@ -765,6 +765,31 @@ class Limiter(unittest.TestCase):
class TimeFormat(unittest.TestCase):
def test_epoch(self):
return self._help_test_epoch()
def test_epoch_in_London(self):
# Europe/London is a particularly troublesome timezone. Nowadays, its
# offset from GMT is 0. But in 1970, its offset from GMT was 1.
# (Apparently in 1970 Britain had redefined standard time to be GMT+1
# and stayed in standard time all year round, whereas today
# Europe/London standard time is GMT and Europe/London Daylight
# Savings Time is GMT+1.) The current implementation of
# time_format.iso_utc_time_to_localseconds() breaks if the timezone is
# Europe/London. (As soon as this unit test is done then I'll change
# that implementation to something that works even in this case...)
origtz = os.environ.get('TZ')
os.environ['TZ'] = "Europe/London"
time.tzset()
try:
return self._help_test_epoch()
finally:
if origtz is None:
del os.environ['TZ']
else:
os.environ['TZ'] = origtz
time.tzset()
def _help_test_epoch(self):
s = time_format.iso_utc_time_to_seconds("1970-01-01T00:00:01")
self.failUnlessEqual(s, 1.0)
s = time_format.iso_utc_time_to_seconds("1970-01-01_00:00:01")

View File

@ -7,7 +7,7 @@
# ISO-8601:
# http://www.cl.cam.ac.uk/~mgk25/iso-time.html
import datetime, re, time
import datetime, os, re, time
def iso_utc_date(now=None, t=time.time):
if now is None:
@ -42,10 +42,17 @@ def iso_utc_time_to_seconds(isotime, _conversion_re=re.compile(r"(?P<year>\d{4})
else:
subsecfloat = 0
localsecondsnodst = time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) )
localsecondsnodst += subsecfloat
utcseconds = localsecondsnodst - time.timezone
return utcseconds
origtz = os.environ.get('TZ')
os.environ['TZ'] = "UTC"
time.tzset()
try:
return time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) ) + subsecfloat
finally:
if origtz is None:
del os.environ['TZ']
else:
os.environ['TZ'] = origtz
time.tzset()
def parse_duration(s):
orig = s