]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Moved datetime_to_imap to datetime_util.datetime_to_INTERNALDATE
authorMenno Smits <menno@freshfoo.com>
Mon, 29 Jun 2015 23:41:00 +0000 (11:41 +1200)
committerMenno Smits <menno@freshfoo.com>
Mon, 29 Jun 2015 23:41:00 +0000 (11:41 +1200)
This fixes #126.

NEWS.rst
imapclient/datetime_util.py
imapclient/imapclient.py
imapclient/test/test_datetime_util.py
imapclient/test/test_imapclient.py
imapclient/test/test_util_functions.py

index 73d465aba2ecef308afd8a359107dc418dcd81fc..08d5f1936a89b36b947cae8f5139693c64ffd0a2 100644 (file)
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -26,6 +26,11 @@ separator.
 Removed horrible INTERNALDATE parsing code (use parse_to_datetime
 instead).
 
+datetime_to_imap has been moved to the datetime_util module and is now
+called datetime_to_INTERNALDATE. This will only affect you in the
+unlikely case that you were importing this function out of the
+IMAPClient package.
+
 ======
  0.12
 ======
index 231979ebc2faf04652701ac936f15da7b2ece01e..530a56ded26f9180aba2d958e4052122a5b07ff3 100644 (file)
@@ -38,6 +38,16 @@ def parse_to_datetime(timestamp, normalise=True):
 def datetime_to_native(dt):
     return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
 
+def datetime_to_INTERNALDATE(dt):
+    """Convert a datetime instance to a IMAP INTERNALDATE string.
+
+    If timezone information is missing the current system
+    timezone is used.
+    """
+    if not dt.tzinfo:
+        dt = dt.replace(tzinfo=FixedOffset.for_system())
+    return dt.strftime("%d-%b-%Y %H:%M:%S %z")
+
 
 # Matches timestamp strings where the time separator is a dot (see
 # issue #154). For example: 'Sat, 8 May 2010 16.03.09 +0200'
index a1babcc07b27f09ee4bb07d160257b8b52dbbd53..b651d37347b05c822ff160239bed3a9c679c984c 100644 (file)
@@ -20,8 +20,8 @@ except ImportError:
     oauth_module = None
 
 from . import response_lexer
+from .datetime_util import datetime_to_INTERNALDATE
 from .imap_utf7 import encode as encode_utf7, decode as decode_utf7
-from .fixed_offset import FixedOffset
 from .response_types import SearchIds
 from .six import moves, iteritems, text_type, integer_types, PY3, binary_type, string_types
 xrange = moves.xrange
@@ -38,6 +38,7 @@ from .response_parser import parse_response, parse_fetch_response
 if 'XLIST' not in imaplib.Commands:
   imaplib.Commands['XLIST'] = imaplib.Commands['LIST']
 
+
 # ...and IDLE
 if 'IDLE' not in imaplib.Commands:
   imaplib.Commands['IDLE'] = imaplib.Commands['APPEND']
@@ -882,7 +883,7 @@ class IMAPClient(object):
         Returns the APPEND response as returned by the server.
         """
         if msg_time:
-            time_val = '"%s"' % datetime_to_imap(msg_time)
+            time_val = '"%s"' % datetime_to_INTERNALDATE(msg_time)
             if PY3:
                 time_val = to_unicode(time_val)
             else:
@@ -1082,16 +1083,6 @@ def normalise_untagged_responses(untagged):
         out[to_bytes(key)] = value
     return out
 
-def datetime_to_imap(dt):
-    """Convert a datetime instance to a IMAP datetime string.
-
-    If timezone information is missing the current system
-    timezone is used.
-    """
-    if not dt.tzinfo:
-        dt = dt.replace(tzinfo=FixedOffset.for_system())
-    return dt.strftime("%d-%b-%Y %H:%M:%S %z")
-
 def _parse_untagged_response(text):
     assert text.startswith(b'* ')
     text = text[2:]
index a930f42d091da93c387a70bc2fffa760cd1f564a..d1f352e22ff7d51c3295b9d65552fa1308467bf6 100644 (file)
@@ -6,7 +6,9 @@ from __future__ import unicode_literals
 
 from datetime import datetime
 
-from ..datetime_util import parse_to_datetime, datetime_to_native
+from mock import patch
+
+from ..datetime_util import parse_to_datetime, datetime_to_native, datetime_to_INTERNALDATE
 from ..fixed_offset import FixedOffset
 from .util import unittest
 
@@ -56,3 +58,17 @@ class TestParsing(unittest.TestCase):
 
     def test_invalid(self):
         self.assertRaises(ValueError, parse_to_datetime, b'ABC')
+
+
+class TestDatetimeToINTERNALDATE(unittest.TestCase):
+
+    def test_with_timezone(self):
+        dt = datetime(2009, 1, 2, 3, 4, 5, 0, FixedOffset(2*60 + 30))
+        self.assertEqual(datetime_to_INTERNALDATE(dt), '02-Jan-2009 03:04:05 +0230')
+
+    @patch('imapclient.datetime_util.FixedOffset.for_system')
+    def test_without_timezone(self, for_system):
+        dt = datetime(2009, 1, 2, 3, 4, 5, 0)
+        for_system.return_value = FixedOffset(-5 * 60)
+
+        self.assertEqual(datetime_to_INTERNALDATE(dt), '02-Jan-2009 03:04:05 -0500')
index 04e175ff443407b2bf878f697f6ce0e82b414840..4c2c7ca020481f53c0afd56010c63ac5f2c29107 100644 (file)
@@ -194,16 +194,16 @@ class TestAppend(IMAPClientTest):
         self.client._imap.append.assert_called_with(
             b'"foobar"', '(FLAG WAVE)', None, b'hi')
 
-    @patch('imapclient.imapclient.datetime_to_imap')
-    def test_with_msg_time(self, datetime_to_imap):
-        datetime_to_imap.return_value = 'somedate'
+    @patch('imapclient.imapclient.datetime_to_INTERNALDATE')
+    def test_with_msg_time(self, datetime_to_INTERNALDATE):
+        datetime_to_INTERNALDATE.return_value = 'somedate'
         self.client._imap.append.return_value = ('OK', [b'Good'])
         msg = b'bye'
 
         self.client.append('foobar', msg, ['FLAG', 'WAVE'],
                            datetime(2009, 4, 5, 11, 0, 5, 0, FixedOffset(2*60)))
 
-        self.assertTrue(datetime_to_imap.called)
+        self.assertTrue(datetime_to_INTERNALDATE.called)
         self.client._imap.append.assert_called_with(
             b'"foobar"', '(FLAG WAVE)', '"somedate"', msg)
 
index 05b45b96cd04a9b93bdcffa84923a308a42d1d54..0f9f07b74748d2fd54e6ba57a156a822e6576fae 100644 (file)
@@ -4,19 +4,13 @@
 
 from __future__ import unicode_literals
 
-from datetime import datetime
-
-from mock import patch
-
 from imapclient.imapclient import (
-    datetime_to_imap,
     join_message_ids,
     normalise_search_criteria,
     normalise_text_list,
     seq_to_parenstr,
     seq_to_parenstr_upper,
 )
-from imapclient.fixed_offset import FixedOffset
 from imapclient.test.util import unittest
 
 
@@ -143,16 +137,3 @@ class Test_normalise_search_criteria(unittest.TestCase):
 
     def test_empty(self):
         self.assertRaises(ValueError, normalise_search_criteria, '')
-
-class TestDateTimeToImap(unittest.TestCase):
-
-    def test_with_timezone(self):
-        dt = datetime(2009, 1, 2, 3, 4, 5, 0, FixedOffset(2*60 + 30))
-        self.assertEqual(datetime_to_imap(dt), '02-Jan-2009 03:04:05 +0230')
-
-    @patch('imapclient.imapclient.FixedOffset.for_system')
-    def test_without_timezone(self, for_system):
-        dt = datetime(2009, 1, 2, 3, 4, 5, 0)
-        for_system.return_value = FixedOffset(-5 * 60)
-
-        self.assertEqual(datetime_to_imap(dt), '02-Jan-2009 03:04:05 -0500')