summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Morega <alex@grep.ro>2014-01-06 21:56:17 +0200
committerAlex Morega <alex@grep.ro>2014-01-06 21:56:17 +0200
commit8afb60ae060dbba45a951f0ca3a7951c7be206b9 (patch)
treed7591c58d2c234f776372cb74b64aeb7a4c53905
parentb004036f8b2624235af20ba601607efa8c3db3ca (diff)
parent88c564228d30a6cda6368cf2077341fea0c6c6d2 (diff)
downloadbabel-8afb60ae060dbba45a951f0ca3a7951c7be206b9.tar.gz
Merge branch 'fix-time-parsing'
-rw-r--r--babel/messages/catalog.py88
-rw-r--r--tests/messages/test_catalog.py14
2 files changed, 48 insertions, 54 deletions
diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py
index 501763b..67c5425 100644
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -40,6 +40,38 @@ PYTHON_FORMAT = re.compile(r'''(?x)
''')
+def _parse_datetime_header(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')
+ 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)
+
+ # 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 = dt.replace(tzinfo=tzoffset)
+
+ return dt
+
+
class Message(object):
"""Representation of a single message in a catalog."""
@@ -379,63 +411,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.
diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py
index fcac34d..aac71ee 100644
--- a/tests/messages/test_catalog.py
+++ b/tests/messages/test_catalog.py
@@ -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