imapclient icon indicating copy to clipboard operation
imapclient copied to clipboard

ENVELOPE datetime is not aware

Open gauravjuvekar opened this issue 3 years ago • 1 comments

DEBUG:imapclient.imaplib:< b'* 1 FETCH (UID 27718 ENVELOPE ("Mon, 14 Mar 2022 04:26:15 +0000" "Folder shared with you==REDACTED=="" (("==REDACTED==" NIL "drive-shares-dm-noreply" "google.com")) (("==REDACTED==" NIL "drive-shares-dm-noreply" "google.com")) (("==REDACTED==" NIL "==REDACTED==" "==REDACTED==")) ((NIL NIL "==REDACTED==" "gmail.com")) ((NIL NIL "==REDACTED==" "==REDACTED==")(NIL NIL "==REDACTED==" "==REDACTED==")) NIL "<[email protected]>" "<[email protected]>"))'

Example from a real email from Google. This generates a naive datetime object datetime.datetime(2022, 3, 13, 21, 26, 15) The datetime object in the envelope instance should be aware because the IMAP server returns the time with timezone offset (UTC).

gauravjuvekar avatar Aug 02 '22 15:08 gauravjuvekar

I will look into this when I have time but I'm pretty sure this is intentional. Note that although the returned datetime object has no timezone attached it has been converted to the local time. If you need a timezone aware datetime you can just attach the local timezone using datetime's replace or astimezone methods (I'm not sure which is appropriate in this case).

mjs avatar Aug 08 '22 06:08 mjs

I'm pretty sure this is intentional. Note that although the returned datetime object has no timezone attached it has been converted to the local time.

I don't get it. Why would the library take a timestamp with a UTC offset and make it into a timezone-naive datetime object in local time, instead of a datetime equipped with the proper offset?

Kodiologist avatar Nov 03 '22 19:11 Kodiologist

IMAPClient instances have a normalise_times attribute which can be used to control whether times are returned as timezone naive (in local time) or with tz information attached. Set it to False if you want timezone-aware datetimes.

https://imapclient.readthedocs.io/en/master/api.html#imapclient.IMAPClient

mjs avatar Nov 04 '22 08:11 mjs

Nice, thanks.

Kodiologist avatar Nov 04 '22 11:11 Kodiologist