]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
parse datetime values with no timezone info
authorAlex Morega <alex@grep.ro>
Mon, 6 Jan 2014 19:55:27 +0000 (21:55 +0200)
committerAlex Morega <alex@grep.ro>
Mon, 6 Jan 2014 19:55:49 +0000 (21:55 +0200)
fixes #56

babel/messages/catalog.py
tests/messages/test_catalog.py

index 756d64cceebe26d64e8e267f605760cf5c266c13..67c54259104f32524c54bcb9a2c44574376073aa 100644 (file)
@@ -41,32 +41,35 @@ PYTHON_FORMAT = re.compile(r'''(?x)
 
 
 def _parse_datetime_header(value):
-    match = re.match(r'^(?P<datetime>.*)(?P<tzoffset>[+-]\d{4})$', value)
+    match = re.match(r'^(?P<datetime>.*?)(?P<tzoffset>[+-]\d{4})?$', value)
 
     tt = time.strptime(match.group('datetime'), '%Y-%m-%d %H:%M')
     ts = time.mktime(tt)
+    dt = datetime.fromtimestamp(ts)
 
     # Separate the offset into a sign component, hours, and # minutes
     tzoffset = match.group('tzoffset')
-    plus_minus_s, rest = tzoffset[0], tzoffset[1:]
-    hours_offset_s, mins_offset_s = rest[:2], rest[2:]
+    if tzoffset is not None:
+        plus_minus_s, rest = tzoffset[0], tzoffset[1:]
+        hours_offset_s, mins_offset_s = rest[:2], rest[2:]
 
-    # Make them all integers
-    plus_minus = int(plus_minus_s + '1')
-    hours_offset = int(hours_offset_s)
-    mins_offset = int(mins_offset_s)
+        # Make them all integers
+        plus_minus = int(plus_minus_s + '1')
+        hours_offset = int(hours_offset_s)
+        mins_offset = int(mins_offset_s)
 
-    # Calculate net offset
-    net_mins_offset = hours_offset * 60
-    net_mins_offset += mins_offset
-    net_mins_offset *= plus_minus
+        # Calculate net offset
+        net_mins_offset = hours_offset * 60
+        net_mins_offset += mins_offset
+        net_mins_offset *= plus_minus
 
-    # Create an offset object
-    tzoffset = FixedOffsetTimezone(net_mins_offset)
+        # Create an offset object
+        tzoffset = FixedOffsetTimezone(net_mins_offset)
 
-    # Store the offset in a datetime object
-    dt = datetime.fromtimestamp(ts)
-    return dt.replace(tzinfo=tzoffset)
+        # Store the offset in a datetime object
+        dt = dt.replace(tzinfo=tzoffset)
+
+    return dt
 
 
 class Message(object):
index fcac34d3da4c744647b560f272ba8c00686f31b8..aac71eeac8b518e99fa92b1cc5f6d203546cb116 100644 (file)
@@ -454,3 +454,17 @@ def test_catalog_update():
 
     assert not 'head' in cat
     assert list(cat.obsolete.values())[0].id == 'head'
+
+
+def test_datetime_parsing():
+    val1 = catalog._parse_datetime_header('2006-06-28 23:24+0200')
+    assert val1.year == 2006
+    assert val1.month == 6
+    assert val1.day == 28
+    assert val1.tzinfo.zone == 'Etc/GMT+120'
+
+    val2 = catalog._parse_datetime_header('2006-06-28 23:24')
+    assert val2.year == 2006
+    assert val2.month == 6
+    assert val2.day == 28
+    assert val2.tzinfo is None