[FIX] mail: preserve timezone offset when extracting the Date of received mails

Previously the date was treated as UTC, ignoring
the specified timezone offset. As a result the
displayed date was off by the actual UTC offset
of the sender's timezone

bzr revid: odo@openerp.com-20120702093523-fttsemnl1irc7917
This commit is contained in:
Olivier Dony
2012-07-02 11:35:23 +02:00
parent 433f7e7f17
commit 4216edfd5f
+10 -1
View File
@@ -24,6 +24,7 @@ import base64
import dateutil.parser
import email
import logging
import pytz
import re
import time
from email.header import decode_header
@@ -398,7 +399,15 @@ class mail_message(osv.osv):
if 'Date' in fields:
date_hdr = decode(msg_txt.get('Date'))
msg['date'] = dateutil.parser.parse(date_hdr).strftime("%Y-%m-%d %H:%M:%S")
parsed_date = dateutil.parser.parse(date_hdr)
if parsed_date.utcoffset() is None:
# naive datetime, so we arbitrarily decide to make it
# UTC, there's no better choice. Should not happen,
# as RFC2822 requires timezone offset in Date headers.
stored_date = parsed_date.replace(tzinfo=pytz.utc)
else:
stored_date = parsed_date.astimezone(pytz.utc)
msg['date'] = stored_date.strftime("%Y-%m-%d %H:%M:%S")
if 'Content-Transfer-Encoding' in fields:
msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')