Commits

Brandon Nielsen committed d34e4ab

Change UTCOffset to an init with no arguments so it can be pickled.

Also added tests for the pickling functionality.

  • Participants
  • Parent commits 4fcd059

Comments (0)

Files changed (3)

File aniso8601/tests/test_timezone.py

 
 import unittest
 import datetime
+import pickle
 
-from aniso8601.timezone import parse_timezone
+from aniso8601.timezone import parse_timezone, build_utcoffset
 
 class TestTimezoneFunctions(unittest.TestCase):
     def test_parse_timezone(self):
 
         with self.assertRaises(ValueError):
             parse_timezone('-00')
+
+    def test_pickle(self):
+        #Make sure timezone objects are pickleable
+        testutcoffset = build_utcoffset('UTC', datetime.timedelta(0))
+
+        utcoffsetpickle = pickle.dumps(testutcoffset)
+
+        resultutcoffset = pickle.loads(utcoffsetpickle)
+
+        self.assertEqual(resultutcoffset._name, testutcoffset._name)
+        self.assertEqual(resultutcoffset._utcdelta, testutcoffset._utcdelta)

File aniso8601/time.py

 
 import datetime
 
-from timezone import parse_timezone, UTCOffset
+from timezone import parse_timezone, build_utcoffset
 from date import parse_date
 
 def parse_time(isotimestr):
     if tzstr == None:
         return parse_time_naive(timestr)
     elif tzstr == 'Z':
-        return parse_time_naive(timestr).replace(tzinfo=UTCOffset('UTC', datetime.timedelta(hours=0)))
+        return parse_time_naive(timestr).replace(tzinfo=build_utcoffset('UTC', datetime.timedelta(hours=0)))
     else:
         return parse_time_naive(timestr).replace(tzinfo=parse_timezone(tzstr))
 

File aniso8601/timezone.py

         tzminute = int(tzstr[4:6])
 
         if tzstr[0] == '+':
-            return UTCOffset(tzstr, datetime.timedelta(hours=tzhour, minutes=tzminute))
+            return build_utcoffset(tzstr, datetime.timedelta(hours=tzhour, minutes=tzminute))
         else:
             if tzhour == 0 and tzminute == 0:
                 raise ValueError('String is not a valid ISO8601 time offset.')
             else:
-                return UTCOffset(tzstr, -datetime.timedelta(hours=tzhour, minutes=tzminute))
+                return build_utcoffset(tzstr, -datetime.timedelta(hours=tzhour, minutes=tzminute))
     elif tzstrlen == 5:
         #±hhmm
         tzhour = int(tzstr[1:3])
         tzminute = int(tzstr[3:5])
 
         if tzstr[0] == '+':
-            return UTCOffset(tzstr, datetime.timedelta(hours=tzhour, minutes=tzminute))
+            return build_utcoffset(tzstr, datetime.timedelta(hours=tzhour, minutes=tzminute))
         else:
             if tzhour == 0 and tzminute == 0:
                 raise ValueError('String is not a valid ISO8601 time offset.')
             else:
-                return UTCOffset(tzstr, -datetime.timedelta(hours=tzhour, minutes=tzminute))
+                return build_utcoffset(tzstr, -datetime.timedelta(hours=tzhour, minutes=tzminute))
     elif tzstrlen == 3:
         #±hh
         tzhour = int(tzstr[1:3])
 
         if tzstr[0] == '+':
-            return UTCOffset(tzstr, datetime.timedelta(hours=tzhour))
+            return build_utcoffset(tzstr, datetime.timedelta(hours=tzhour))
         else:
             if tzhour == 0:
                 raise ValueError('String is not a valid ISO8601 time offset.')
             else:
-                return UTCOffset(tzstr, -datetime.timedelta(hours=tzhour))
+                return build_utcoffset(tzstr, -datetime.timedelta(hours=tzhour))
     else:
         raise ValueError('String is not a valid ISO8601 time offset.')
 
+def build_utcoffset(name, utcdelta):
+    #We build an offset in this manner since the
+    #tzinfo class must have an init that can
+    #"method that can be called with no arguments"
+
+    returnoffset = UTCOffset()
+
+    returnoffset.setname(name)
+    returnoffset.setutcdelta(utcdelta)
+
+    return returnoffset
+
 class UTCOffset(datetime.tzinfo):
-    def __init__(self, name, utcdelta):
+    def setname(self, name):
         self._name = name
+
+    def setutcdelta(self, utcdelta):
         self._utcdelta = utcdelta
 
     def utcoffset(self, dt):