Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_INTERNALDATE_normalised(self):
output = parse_fetch_response([b'3 (INTERNALDATE " 9-Feb-2007 17:08:08 -0430")'])
dt = output[3][b'INTERNALDATE']
self.assertTrue(dt.tzinfo is None) # Returned date should be in local timezone
expected_dt = datetime_to_native(
datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4 * 60 - 30)))
self.assertEqual(dt, expected_dt)
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_INTERNALDATE.called)
self.client._imap.append.assert_called_with(
b'"foobar"', '(FLAG WAVE)', '"somedate"', msg)
def test_INTERNALDATE(self):
out = parse_fetch_response(
[b'1 (INTERNALDATE " 9-Feb-2007 17:08:08 -0430")'],
normalise_times=False
)
self.assertEqual(
out[1][b'INTERNALDATE'],
datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(-4 * 60 - 30))
)
b'"subject" '
b'(("name" NIL "address1" "domain1.com")) ' # from (name and address)
b'((NIL NIL "address2" "domain2.com")) ' # sender (just address)
b'(("name" NIL "address3" "domain3.com") NIL) ' # reply to
b'NIL' # to (no address)
b'((NIL NIL "address4" "domain4.com") ' # cc
b'("person" NIL "address4b" "domain4b.com")) '
b'NIL ' # bcc
b'"" '
b'""))')
output = parse_fetch_response([envelope_str], normalise_times=False)
self.assertSequenceEqual(output[1][b'ENVELOPE'],
Envelope(
datetime(2013, 3, 24, 22, 6, 10, tzinfo=FixedOffset(120)),
b"subject",
(Address(b"name", None, b"address1", b"domain1.com"),),
(Address(None, None, b"address2", b"domain2.com"),),
(Address(b"name", None, b"address3", b"domain3.com"),),
None,
(Address(None, None, b"address4", b"domain4.com"),
Address(b"person", None, b"address4b", b"domain4b.com")),
None, b"", b""
)
def test_mixed_types(self):
self.assertEqual(parse_fetch_response([(
b'1 (INTERNALDATE " 9-Feb-2007 17:08:08 +0100" RFC822 {21}',
b'Subject: test\r\n\r\nbody'
), b')']), {
1: {
b'INTERNALDATE': datetime_to_native(datetime(2007, 2, 9, 17, 8, 8, 0, FixedOffset(60))),
b'RFC822': b'Subject: test\r\n\r\nbody',
b'SEQ': 1
}
def datetime_to_native(dt):
return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None)
"""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(_munge(timestamp))
if time_tuple is 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