]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Support dot as the time separator in timestamp strings
authorMenno Smits <menno@freshfoo.com>
Mon, 29 Jun 2015 22:52:02 +0000 (10:52 +1200)
committerMenno Smits <menno@freshfoo.com>
Mon, 29 Jun 2015 22:52:02 +0000 (10:52 +1200)
This is unconventional but has been seen in the wild. Fixes #154.

imapclient/datetime_util.py
imapclient/test/test_datetime_util.py

index 10758d40e127cdf31d113cdbfdac6aca6130fd29..bb807b44d874441d533d0bc679552bf72ef7190a 100644 (file)
@@ -4,6 +4,7 @@
 
 from __future__ import unicode_literals
 
+import re
 from datetime import datetime
 from email.utils import parsedate_tz
 
@@ -19,8 +20,7 @@ def parse_to_datetime(timestamp, normalise=True):
     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)
 
@@ -37,3 +37,14 @@ def parse_to_datetime(timestamp, normalise=True):
 
 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
index fb6deba2a848a7f9aa85e3abb99c8c846269bbff..38b2f61bb822898319e03c98ea7e312ec4d996e5 100644 (file)
@@ -35,5 +35,20 @@ class TestParsing(unittest.TestCase):
             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')