summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Morega <alex@grep.ro>2014-01-06 21:41:25 +0200
committerAlex Morega <alex@grep.ro>2014-01-06 21:41:25 +0200
commite92bbba373e6d56383723f71dd8d531c4cdd2806 (patch)
tree744abe63c478427ab8db2c8d638067242a68c971
parentb004036f8b2624235af20ba601607efa8c3db3ca (diff)
downloadbabel-e92bbba373e6d56383723f71dd8d531c4cdd2806.tar.gz
extract _parse_datetime_header function
-rw-r--r--babel/messages/catalog.py84
1 files changed, 30 insertions, 54 deletions
diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py
index 501763b..82c08c8 100644
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -40,6 +40,34 @@ PYTHON_FORMAT = re.compile(r'''(?x)
''')
+def _parse_datetime_header(value):
+ value, tzoffset, _ = re.split('([+-]\d{4})$', value, 1)
+
+ tt = time.strptime(value, '%Y-%m-%d %H:%M')
+ ts = time.mktime(tt)
+
+ # 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)
+ return dt.replace(tzinfo=tzoffset)
+
+
class Message(object):
"""Representation of a single message in a catalog."""
@@ -379,63 +407,11 @@ class Catalog(object):
self._num_plurals = int(params.get('nplurals', 2))
self._plural_expr = params.get('plural', '(n != 1)')
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)
-
- tt = time.strptime(value, '%Y-%m-%d %H:%M')
- ts = time.mktime(tt)
-
- # 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)
+ self.creation_date = _parse_datetime_header(value)
elif name == 'po-revision-date':
# Keep the value if it's not the default one
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)
- tt = time.strptime(value, '%Y-%m-%d %H:%M')
- ts = time.mktime(tt)
-
- # 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)
+ self.revision_date = _parse_datetime_header(value)
mime_headers = property(_get_mime_headers, _set_mime_headers, doc="""\
The MIME headers of the catalog, used for the special ``msgid ""`` entry.