Rev 12: Ugly hacks to add tz aware info to JunitXml to make it compatible. in http://bazaar.launchpad.net/~jameinel/pyjunitxml

John Arbash Meinel john at arbash-meinel.com
Thu Dec 10 05:36:58 GMT 2009


At http://bazaar.launchpad.net/~jameinel/pyjunitxml

------------------------------------------------------------
revno: 12
revision-id: john at arbash-meinel.com-20091210053651-l9m35jeasrwn0ye2
parent: robertc at robertcollins.net-20091119084343-dj4hiz95co5s6p8u
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: pyjunitxml
timestamp: Wed 2009-12-09 23:36:51 -0600
message:
  Ugly hacks to add tz aware info to JunitXml to make it compatible.
  
  I think it would be much cleaner to remove the tz info from subunit, and have
  subunit just manually adjust the times based on tz.
-------------- next part --------------
=== modified file 'junitxml/__init__.py'
--- a/junitxml/__init__.py	2009-11-19 08:33:25 +0000
+++ b/junitxml/__init__.py	2009-12-10 05:36:51 +0000
@@ -8,6 +8,7 @@
 
 
 import datetime
+import time
 import unittest
 from xml.sax.saxutils import escape
 
@@ -17,6 +18,28 @@
     return junitxml.tests.test_suite()
 
 
+class LocalTimezone(datetime.tzinfo):
+
+    def __init__(self):
+        self._offset = None
+
+    # It seems that the minimal possible implementation is to just return all
+    # None for every function, but then it breaks...
+    def utcoffset(self, dt):
+        if self._offset is None:
+            t = 1260423030 # arbitrary, but doesn't handle dst very well
+            dt = datetime.datetime
+            self._offset = (dt.fromtimestamp(t) - dt.utcfromtimestamp(t))
+        return self._offset
+
+    def dst(self, dt):
+        return datetime.timedelta(0)
+
+    def tzname(self, dt):
+        return None
+
+
+
 class JUnitXmlResult(unittest.TestResult):
     """A TestResult which outputs JUnit compatible XML."""
     
@@ -34,16 +57,22 @@
         self._set_time = None
         self._test_start = None
         self._run_start = None
+        self._tz_info = None
 
     def startTestRun(self):
         """Start a test run."""
         self._run_start = self._now()
 
+    def _get_tzinfo(self):
+        if self._tz_info is None:
+            self._tz_info = LocalTimezone()
+        return self._tz_info
+
     def _now(self):
         if self._set_time is not None:
             return self._set_time
         else:
-            return datetime.datetime.utcnow()
+            return datetime.datetime.now(self._get_tzinfo())
 
     def time(self, a_datetime):
         self._set_time = a_datetime
@@ -53,7 +82,12 @@
         self._test_start = self._now()
 
     def _duration(self, from_datetime):
-        delta = self._now() - from_datetime
+        try:
+            delta = self._now() - from_datetime
+        except TypeError:
+            n = self._now()
+            print n, self._set_time, from_datetime
+            delta = datetime.timedelta(-1)
         seconds = delta.days * 3600*24 + delta.seconds
         return seconds + 0.000001 * delta.microseconds
 



More information about the bazaar-commits mailing list