]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Extracted out timestamp parsing to new module
authorMenno Smits <menno@freshfoo.com>
Sun, 17 Nov 2013 21:58:13 +0000 (21:58 +0000)
committerMenno Smits <menno@freshfoo.com>
Sun, 17 Nov 2013 21:58:13 +0000 (21:58 +0000)
imapclient/datetime_util.py [new file with mode: 0644]
imapclient/test/test_datetime_util.py [new file with mode: 0644]

diff --git a/imapclient/datetime_util.py b/imapclient/datetime_util.py
new file mode 100644 (file)
index 0000000..6c66690
--- /dev/null
@@ -0,0 +1,38 @@
+# Copyright (c) 2013, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+
+from datetime import datetime
+from email.utils import parsedate_tz
+
+from .fixed_offset import FixedOffset
+
+
+def parse_to_datetime(timestamp, normalise=True):
+    """Convert an IMAP datetime string to a datetime.
+
+    If normalise is True (the default), then the returned datetime
+    will be timezone-naive but adjusted to the local time.
+
+    If normalise is False, then the returned datetime will be
+    unadjusted but will contain timezone information as per the input.
+    """
+    time_tuple = parsedate_tz(timestamp)
+    if time_tuple == None:
+        raise ValueError("couldn't parse datetime %r" % timestamp)
+
+    tz_offset_seconds = time_tuple[-1]
+    tz = None
+    if tz_offset_seconds is not None:
+        tz = FixedOffset(tz_offset_seconds / 60)
+
+    dt = datetime(*time_tuple[:6], tzinfo=tz)
+    if normalise and tz:
+       dt = datetime_to_native(dt)
+
+    return dt
+
+def datetime_to_native(dt):
+    return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
diff --git a/imapclient/test/test_datetime_util.py b/imapclient/test/test_datetime_util.py
new file mode 100644 (file)
index 0000000..2e61250
--- /dev/null
@@ -0,0 +1,39 @@
+# Copyright (c) 2013, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+
+from datetime import datetime
+
+from ..datetime_util import parse_to_datetime, datetime_to_native
+from ..fixed_offset import FixedOffset
+from .util import unittest
+
+
+class TestParsing(unittest.TestCase):
+
+    def check_normalised_and_not(self, in_string, expected_datetime):
+        self.assertEqual(
+            parse_to_datetime(in_string),
+            datetime_to_native(expected_datetime)
+        )
+        self.assertEqual(
+            parse_to_datetime(in_string, normalise=False),
+            expected_datetime
+        )
+
+    def test_rfc822_style(self):
+        self.check_normalised_and_not(
+            'Sun, 24 Mar 2013 22:06:10 +0200',
+            datetime(2013, 3, 24, 22, 6, 10, 0, FixedOffset(120))
+        )
+
+    def test_internaldate_style(self):
+        self.check_normalised_and_not(
+            ' 9-Feb-2007 17:08:08 -0430',
+            datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4*60 - 30))
+        )
+
+    def test_invalid(self):
+        self.assertRaises(ValueError, parse_to_datetime, 'ABC')