From: Zooko O'Whielacronx Date: Thu, 11 Jun 2009 22:11:29 +0000 (-0700) Subject: util: fix time_format.iso_utc_time_to_seconds() so that it works even in London X-Git-Tag: trac-4000~89 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/-?a=commitdiff_plain;h=8978cb073874b7d6ce24815d0fc5143a4643984b;p=tahoe-lafs%2Ftahoe-lafs.git util: fix time_format.iso_utc_time_to_seconds() so that it works even in London --- diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index ca64b1f4..0f0fbba9 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -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") diff --git a/src/allmydata/util/time_format.py b/src/allmydata/util/time_format.py index adaa5c1b..aa0b64d8 100644 --- a/src/allmydata/util/time_format.py +++ b/src/allmydata/util/time_format.py @@ -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\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