ENVELOPE datetime is not aware
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).
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).
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?
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
Nice, thanks.