]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Fix Catalog._set_mime_headers' handing of negative offsets.
authorJeroen Ruigrok van der Werven <asmodai@in-nomine.org>
Mon, 16 Mar 2009 17:49:48 +0000 (17:49 +0000)
committerJeroen Ruigrok van der Werven <asmodai@in-nomine.org>
Mon, 16 Mar 2009 17:49:48 +0000 (17:49 +0000)
Submitted by: Asheesh Laroia (Creative Commons)
Closes: #165
ChangeLog
babel/messages/catalog.py
babel/messages/tests/catalog.py

index 99196b07511f3e5be0952069cee1c9bcfe7be92f..9c78f0331217768906fc54b91a1cb8f4d17c1cf0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,7 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/
  * Added `format_timedelta` function to support localized formatting of
    relative times with strings such as "2 days" or "1 month" (ticket #126).
  * Fixed Python 2.3 compatibility (ticket #146).
+ * Fixed negative offset handling of Catalog._set_mime_headers (ticket #165).
 
 
 Version 0.9.4
index fec39b34bc65ced53541e438086ec46e613601bd..94223ce68580f1c0d306636158d5cd654a28583e 100644 (file)
@@ -349,11 +349,29 @@ class Catalog(object):
             elif name == 'pot-creation-date':
                 # FIXME: this should use dates.parse_datetime as soon as that
                 #        is ready
-                value, tzoffset, _ = re.split('[+-](\d{4})$', value, 1)
+                value, tzoffset, _ = re.split('([+-]\d{4})$', value, 1)
+
                 tt = time.strptime(value, '%Y-%m-%d %H:%M')
                 ts = time.mktime(tt)
-                tzoffset = FixedOffsetTimezone(int(tzoffset[:2]) * 60 +
-                                               int(tzoffset[2:]))
+
+                # Seprate the offset into a sign component, hours, and minutes
+                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)
+
+                # 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)
+
+                # Store the offset in a datetime object
                 dt = datetime.fromtimestamp(ts)
                 self.creation_date = dt.replace(tzinfo=tzoffset)
             elif name == 'po-revision-date':
@@ -361,11 +379,29 @@ class Catalog(object):
                 if 'YEAR' not in value:
                     # FIXME: this should use dates.parse_datetime as soon as
                     #        that is ready
-                    value, tzoffset, _ = re.split('[+-](\d{4})$', value, 1)
+                    value, tzoffset, _ = re.split('([+-]\d{4})$', value, 1)
                     tt = time.strptime(value, '%Y-%m-%d %H:%M')
                     ts = time.mktime(tt)
-                    tzoffset = FixedOffsetTimezone(int(tzoffset[:2]) * 60 +
-                                                   int(tzoffset[2:]))
+
+                    # Seprate the offset into a sign component, hours, and
+                    # minutes
+                    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)
+
+                    # 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)
+
+                    # Store the offset in a datetime object
                     dt = datetime.fromtimestamp(ts)
                     self.revision_date = dt.replace(tzinfo=tzoffset)
 
index 104dd6a60cd2735cad42e8c92130d98df3141a5b..df04d8e01f0e7c2e9727c17f48c7d477d5b0bfa5 100644 (file)
@@ -269,6 +269,15 @@ class CatalogTestCase(unittest.TestCase):
         localized_catalog.update(template)
         self.assertEqual(localized_catalog.revision_date, fake_rev_date)
 
+    def test_stores_datetime_correctly(self):
+        localized = catalog.Catalog()
+        localized.locale = 'de_DE'
+        localized[''] = catalog.Message('', 
+                       "POT-Creation-Date: 2009-03-09 15:47-0700\n" +
+                       "PO-Revision-Date: 2009-03-09 15:47-0700\n")
+        for key, value in localized.mime_headers:
+            if key in ('POT-Creation-Date', 'PO-Revision-Date'):
+                self.assertEqual(value, '2009-03-09 15:47-0700')
 
 def suite():
     suite = unittest.TestSuite()