util: Brian's horrible hack to figure out how much localtime and utctime differ. Now we'll see if it works on Windows.

This commit is contained in:
Zooko O'Whielacronx 2009-06-12 13:45:56 -07:00
parent 4a30c5899c
commit 45928315f6
2 changed files with 13 additions and 14 deletions

View File

@ -792,6 +792,7 @@ class TimeFormat(unittest.TestCase):
time.tzset() time.tzset()
def _help_test_epoch(self): def _help_test_epoch(self):
origtzname = time.tzname
s = time_format.iso_utc_time_to_seconds("1970-01-01T00:00:01") s = time_format.iso_utc_time_to_seconds("1970-01-01T00:00:01")
self.failUnlessEqual(s, 1.0) self.failUnlessEqual(s, 1.0)
s = time_format.iso_utc_time_to_seconds("1970-01-01_00:00:01") s = time_format.iso_utc_time_to_seconds("1970-01-01_00:00:01")
@ -822,6 +823,7 @@ class TimeFormat(unittest.TestCase):
# Look for daylight-savings-related errors. # Look for daylight-savings-related errors.
thatmomentinmarch = time_format.iso_utc_time_to_seconds("2009-03-20 21:49:02.226536") thatmomentinmarch = time_format.iso_utc_time_to_seconds("2009-03-20 21:49:02.226536")
self.failUnlessEqual(thatmomentinmarch, 1237585742.226536) self.failUnlessEqual(thatmomentinmarch, 1237585742.226536)
self.failUnlessEqual(origtzname, time.tzname)
class CacheDir(unittest.TestCase): class CacheDir(unittest.TestCase):
def test_basic(self): def test_basic(self):

View File

@ -7,7 +7,7 @@
# ISO-8601: # ISO-8601:
# http://www.cl.cam.ac.uk/~mgk25/iso-time.html # http://www.cl.cam.ac.uk/~mgk25/iso-time.html
import datetime, os, re, time import datetime, re, time
def iso_utc_date(now=None, t=time.time): def iso_utc_date(now=None, t=time.time):
if now is None: if now is None:
@ -42,19 +42,16 @@ def iso_utc_time_to_seconds(isotime, _conversion_re=re.compile(r"(?P<year>\d{4})
else: else:
subsecfloat = 0 subsecfloat = 0
origtz = os.environ.get('TZ') # Brian's hack to figure out the offset from localtime to UTC.
os.environ['TZ'] = "UTC" localseconds = time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) )
if hasattr(time, 'tzset'): asifutcstr = iso_utc(localseconds)
time.tzset() asifm = _conversion_re.match(asifutcstr)
try: asifyear, asifmonth, asifday = int(asifm.group('year')), int(asifm.group('month')), int(asifm.group('day'))
return time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) ) + subsecfloat asifhour, asifminute, asifsecond = int(asifm.group('hour')), int(asifm.group('minute')), int(asifm.group('second'))
finally: asifutcsecs = time.mktime( (asifyear, asifmonth, asifday, asifhour, asifminute, asifsecond, 0, 1, 0) )
if origtz is None: offset = asifutcsecs - localseconds
del os.environ['TZ']
else: return localseconds - offset + subsecfloat
os.environ['TZ'] = origtz
if hasattr(time, 'tzset'):
time.tzset()
def parse_duration(s): def parse_duration(s):
orig = s orig = s