From 4216edfd5f362d40f6ddcb27cbbdef1223dcd3cc Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Mon, 2 Jul 2012 11:35:23 +0200 Subject: [PATCH] [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 --- addons/mail/mail_message.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 5c2229d49bc..ab692879343 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -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')