from __future__ import unicode_literals
+import re
from datetime import datetime
from email.utils import parsedate_tz
If normalise is False, then the returned datetime will be
unadjusted but will contain timezone information as per the input.
"""
- timestamp = timestamp.decode('latin-1') # parsedate_tz only works with strings
- time_tuple = parsedate_tz(timestamp)
+ time_tuple = parsedate_tz(_normalise(timestamp))
if time_tuple == None:
raise ValueError("couldn't parse datetime %r" % timestamp)
def datetime_to_native(dt):
return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
+
+
+# Matches timestamp strings where the time separator is a dot (see
+# issue #154). For example: 'Sat, 8 May 2010 16.03.09 +0200'
+_rfc822_dotted_time = re.compile("\w+, ?\d{1,2} \w+ \d\d(\d\d)? \d\d?\.\d\d?\.\d\d?.*")
+
+def _normalise(s):
+ s = s.decode('latin-1') # parsedate_tz only works with strings
+ if _rfc822_dotted_time.match(s):
+ return s.replace(".", ":")
+ return s
datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4*60 - 30))
)
+ def test_dots_for_time_separator(self):
+ # As reported in issue #154.
+ self.check_normalised_and_not(
+ b'Sat, 8 May 2010 16.03.09 +0200',
+ datetime(2010, 5, 8, 16, 3, 9, 0, FixedOffset(120))
+ )
+ self.check_normalised_and_not(
+ b'Tue, 18 May 2010 16.03.09 -0200',
+ datetime(2010, 5, 18, 16, 3, 9, 0, FixedOffset(-120))
+ )
+ self.check_normalised_and_not(
+ b'Wednesday,18 August 2010 16.03.09 -0200',
+ datetime(2010, 8, 18, 16, 3, 9, 0, FixedOffset(-120))
+ )
+
def test_invalid(self):
self.assertRaises(ValueError, parse_to_datetime, b'ABC')