Repository: obspy/obspy

Issue:
support all versions of specification of timezones in ISO strings
Some allowed specifications of ISO time with timezone are failing..



Discussion:

Resolving patch:
diff --git a/obspy/core/utcdatetime.py b/obspy/core/utcdatetime.py
--- a/obspy/core/utcdatetime.py
+++ b/obspy/core/utcdatetime.py
@@ -410,13 +410,19 @@ def _parseISO8601(value):
         # check for time zone information
         # note that the zone designator is the actual offset from UTC and
         # does not include any information on daylight saving time
-        delta = 0
-        if time.count('+') == 1:
-            (time, tz) = time.split('+')
-            delta = -1 * (int(tz[0:2]) * 60 * 60 + int(tz[2:]) * 60)
-        elif time.count('-') == 1:
-            (time, tz) = time.split('-')
-            delta = int(tz[0:2]) * 60 * 60 + int(tz[2:]) * 60
+        if time.count('+') == 1 and '+' in time[-6:]:
+            (time, tz) = time.rsplit('+')
+            delta = -1
+        elif time.count('-') == 1 and '-' in time[-6:]:
+            (time, tz) = time.rsplit('-')
+            delta = 1
+        else:
+            delta = 0
+        if delta:
+            tz = tz.replace(':', '')
+            while len(tz) < 3:
+                tz += '0'
+            delta = delta * (int(tz[0:2]) * 60 * 60 + int(tz[2:]) * 60)
         # split microseconds
         ms = 0
         if '.' in time:


Test patch:
diff --git a/obspy/core/tests/test_utcdatetime.py b/obspy/core/tests/test_utcdatetime.py
--- a/obspy/core/tests/test_utcdatetime.py
+++ b/obspy/core/tests/test_utcdatetime.py
@@ -854,7 +854,7 @@ def test_richComparisonNonNumericTypes(self):
             self.assertFalse(obj >= dt)
             self.assertFalse(obj > dt)
 
-    def test_timezone_aware_datetime(self):
+    def test_datetime_with_timezone(self):
         """
         UTCDateTime from timezone-aware datetime.datetime
 
@@ -907,6 +907,31 @@ def test_abs(self):
         dt = UTCDateTime(1969, 12, 31, 23, 59, 59, 500000)
         self.assertEquals(abs(dt), 0.5)
 
+    def test_string_with_timezone(self):
+        """
+        Test that all valid ISO time zone specifications are parsed properly
+        http://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
+        """
+        # positive
+        t = UTCDateTime("2013-09-01T12:34:56Z")
+        time_strings = \
+            ["2013-09-01T14:34:56+02", "2013-09-01T14:34:56+02:00",
+             "2013-09-01T14:34:56+0200", "2013-09-01T14:49:56+02:15",
+             "2013-09-01T12:34:56+00:00", "2013-09-01T12:34:56+00",
+             "2013-09-01T12:34:56+0000"]
+        for time_string in time_strings:
+            self.assertEqual(t, UTCDateTime(time_string))
+
+        # negative
+        t = UTCDateTime("2013-09-01T12:34:56Z")
+        time_strings = \
+            ["2013-09-01T10:34:56-02", "2013-09-01T10:34:56-02:00",
+             "2013-09-01T10:34:56-0200", "2013-09-01T10:19:56-02:15",
+             "2013-09-01T12:34:56-00:00", "2013-09-01T12:34:56-00",
+             "2013-09-01T12:34:56-0000"]
+        for time_string in time_strings:
+            self.assertEqual(t, UTCDateTime(time_string))
+
 
 def suite():
     return unittest.makeSuite(UTCDateTimeTestCase, 'test')