These are converted to the correctly formatted date strings.
Closes #170.
Search criteria
~~~~~~~~~~~~~~~
-The preferred way to specify criteria is as a list of strings. Valid
-examples are::
+The preferred way to specify criteria is as a list of strings, ints
+and dates (where relevant). Valid examples are::
c.search(['DELETED'])
c.search(['NOT', 'DELETED'])
c.search(['FLAGGED', 'SUBJECT', 'foo', 'BODY', 'hello world'])
c.search(['NOT', 'DELETED', 'SMALLER', 1000])
+ c.search(['SINCE', date(2006, 5, 3)])
IMAPClient will perform all required quoting and encoding. Callers
should not attempt to do this themselves.
c.search('NOT DELETED')
c.search('TEXT "foo"')
c.search('DELETED TEXT "foo" SMALLER 500')
+ c.search('SINCE 03-May-2006')
Search charset
~~~~~~~~~~~~~~
from .fixed_offset import FixedOffset
+_SHORT_MONTHS = ' Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ')
+
def parse_to_datetime(timestamp, normalise=True):
"""Convert an IMAP datetime string to a datetime.
if _rfc822_dotted_time.match(s):
return s.replace(".", ":")
return s
+
+
+def format_criteria_date(dt):
+ """Format a date or datetime instance for use in IMAP search criteria.
+ """
+ out = '%02d-%s-%d' % (dt.day, _SHORT_MONTHS[dt.month], dt.year)
+ return out.encode('ascii')
import socket
import sys
import re
-from datetime import datetime
+from datetime import datetime, date
from operator import itemgetter
from six import moves, iteritems, text_type, integer_types, PY3, binary_type, iterbytes
from . import imap4
from . import response_lexer
from . import tls
-from .datetime_util import datetime_to_INTERNALDATE
+from .datetime_util import datetime_to_INTERNALDATE, format_criteria_date
from .imap_utf7 import encode as encode_utf7, decode as decode_utf7
from .response_parser import parse_response, parse_message_list, parse_fetch_response
xrange = moves.xrange
[u'SMALLER', 500]
[b'NOT', b'DELETED']
[u'TEXT', u'foo bar', u'FLAGGED', u'SUBJECT', u'baz']
+ [u'SINCE', date(2005, 4, 3)]
IMAPClient will perform conversion and quoting as
required. The caller shouldn't do this.
u'SMALLER 500'
b'NOT DELETED'
u'TEXT "foo bar" FLAGGED SUBJECT "baz"'
+ b'SINCE 03-Apr-2005'
*charset* specifies the character set of the criteria. It
defaults to US-ASCII as this is the only charset that a server
attribute. This is set if the server included a MODSEQ value
to the search response (i.e. if a MODSEQ criteria was included
in the search).
-
"""
return self._search(criteria, charset)
def _handle_one_search_criteria(item, charset):
if isinstance(item, int):
return str(item).encode('ascii')
+ elif isinstance(item, (datetime, date)):
+ return format_criteria_date(item)
return _maybe_quote(to_bytes(item, charset))
from __future__ import unicode_literals
-from datetime import datetime
+from datetime import datetime, date
from mock import patch
-from ..datetime_util import parse_to_datetime, datetime_to_native, datetime_to_INTERNALDATE
+from ..datetime_util import (
+ datetime_to_INTERNALDATE,
+ datetime_to_native,
+ format_criteria_date,
+ parse_to_datetime,
+)
from ..fixed_offset import FixedOffset
from .util import unittest
for_system.return_value = FixedOffset(-5 * 60)
self.assertEqual(datetime_to_INTERNALDATE(dt), '02-Jan-2009 03:04:05 -0500')
+
+
+class TestCriteriaDateFormatting(unittest.TestCase):
+
+ def test_basic(self):
+ self.assertEqual(format_criteria_date(date(1996, 2, 22)), b'22-Feb-1996')
+
+ def test_single_digit_day(self):
+ self.assertEqual(format_criteria_date(date(1996, 4, 4)), b'04-Apr-1996')
from __future__ import unicode_literals
+from datetime import date, datetime
+
from mock import Mock
from .imapclient_test import IMAPClientTest
# Default conversion using us-ascii.
self.check_call([b'CHARSET', b'utf-8', b'FOO', b'\xe2\x98\xb9'])
+ def test_with_date(self):
+ self.client.search(['SINCE', date(2005, 4, 3)])
+ self.check_call([b'SINCE', b'03-Apr-2005'])
+
+ def test_with_datetime(self):
+ self.client.search(['SINCE', datetime(2005, 4, 3, 2, 1, 0)])
+ self.check_call([b'SINCE', b'03-Apr-2005']) # Time part is ignored
+
def test_quoting(self):
self.client.search(['TEXT', 'foo bar'])
-
self.check_call([b'TEXT', b'"foo bar"'])
def test_no_results(self):