From 45928315f6546185e5ad3933e60fb99c23ea95a7 Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Fri, 12 Jun 2009 13:45:56 -0700
Subject: [PATCH] util: Brian's horrible hack to figure out how much localtime
 and utctime differ.  Now we'll see if it works on Windows.

---
 src/allmydata/test/test_util.py   |  2 ++
 src/allmydata/util/time_format.py | 25 +++++++++++--------------
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py
index adb9bca4..ac75acfb 100644
--- a/src/allmydata/test/test_util.py
+++ b/src/allmydata/test/test_util.py
@@ -792,6 +792,7 @@ class TimeFormat(unittest.TestCase):
                 time.tzset()
 
     def _help_test_epoch(self):
+        origtzname = time.tzname
         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")
@@ -822,6 +823,7 @@ class TimeFormat(unittest.TestCase):
         # Look for daylight-savings-related errors.
         thatmomentinmarch = time_format.iso_utc_time_to_seconds("2009-03-20 21:49:02.226536")
         self.failUnlessEqual(thatmomentinmarch, 1237585742.226536)
+        self.failUnlessEqual(origtzname, time.tzname)
 
 class CacheDir(unittest.TestCase):
     def test_basic(self):
diff --git a/src/allmydata/util/time_format.py b/src/allmydata/util/time_format.py
index 42e6aee0..e9b081f6 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, os, re, time
+import datetime, re, time
 
 def iso_utc_date(now=None, t=time.time):
     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:
         subsecfloat = 0
 
-    origtz = os.environ.get('TZ')
-    os.environ['TZ'] = "UTC"
-    if hasattr(time, 'tzset'):
-        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
-        if hasattr(time, 'tzset'):
-            time.tzset()
+    # Brian's hack to figure out the offset from localtime to UTC.
+    localseconds = time.mktime( (year, month, day, hour, minute, second, 0, 1, 0) )
+    asifutcstr = iso_utc(localseconds)
+    asifm = _conversion_re.match(asifutcstr)
+    asifyear, asifmonth, asifday = int(asifm.group('year')), int(asifm.group('month')), int(asifm.group('day'))
+    asifhour, asifminute, asifsecond = int(asifm.group('hour')), int(asifm.group('minute')), int(asifm.group('second'))
+    asifutcsecs = time.mktime( (asifyear, asifmonth, asifday, asifhour, asifminute, asifsecond, 0, 1, 0) )
+    offset = asifutcsecs - localseconds
+
+    return localseconds - offset + subsecfloat
 
 def parse_duration(s):
     orig = s
-- 
2.45.2