changeset 481:4adedf7d0f04 stable

Merged revisions 469,529 via svnmerge from http://svn.edgewall.org/repos/babel/trunk ........ r469 | jruigrok | 2009-03-16 18:49:48 +0100 (ma, 16 mrt 2009) | 5 lines Fix Catalog._set_mime_headers' handing of negative offsets. Submitted by: Asheesh Laroia (Creative Commons) Closes: #165 ........ r529 | jruigrok | 2010-04-11 10:43:36 +0200 (zo, 11 apr 2010) | 2 lines Fix typos. ........
author jruigrok
date Sun, 11 Apr 2010 08:48:25 +0000
parents 886d8c92c7f9
children 968851d8b1c6
files 0.9.x/ChangeLog 0.9.x/babel/messages/catalog.py 0.9.x/babel/messages/tests/catalog.py
diffstat 3 files changed, 53 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/0.9.x/ChangeLog
+++ b/0.9.x/ChangeLog
@@ -13,6 +13,7 @@
  * Keep the PO-Revision-Date if it is not the default value (ticket #148).
  * Make --no-wrap work by reworking --width's default and mimic xgettext's
    behaviour of always wrapping comments (ticket #145).
+ * Fixed negative offset handling of Catalog._set_mime_headers (ticket #165).
 
 
 Version 0.9.5
--- a/0.9.x/babel/messages/catalog.py
+++ b/0.9.x/babel/messages/catalog.py
@@ -351,11 +351,29 @@
             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:]))
+
+                # Separate 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':
@@ -363,11 +381,29 @@
                 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:]))
+
+                    # Separate 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)
 
--- a/0.9.x/babel/messages/tests/catalog.py
+++ b/0.9.x/babel/messages/tests/catalog.py
@@ -239,6 +239,16 @@
         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()
     suite.addTest(doctest.DocTestSuite(catalog, optionflags=doctest.ELLIPSIS))
Copyright (C) 2012-2017 Edgewall Software