From: Daniel Axtens Date: Wed, 28 Jun 2017 07:48:48 +0000 (+1000) Subject: parser: better date parsing X-Git-Tag: v2.0.0~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=06b5f052747adb3c903e6c5faae6eef5defdd75b;p=thirdparty%2Fpatchwork.git parser: better date parsing It turns out that there is a lot that can go wrong in parsing a date. OverflowError, ValueError and OSError have all been observed. If these go wrong, substitute the current datetime. Signed-off-by: Daniel Axtens Signed-off-by: Stephen Finucane Reviewed-by: Andrew Donnellan --- diff --git a/patchwork/parser.py b/patchwork/parser.py index 47407cbe..8a07a698 100644 --- a/patchwork/parser.py +++ b/patchwork/parser.py @@ -344,10 +344,24 @@ def find_date(mail): h = clean_header(mail.get('Date', '')) if not h: return datetime.datetime.utcnow() + t = parsedate_tz(h) if not t: return datetime.datetime.utcnow() - return datetime.datetime.utcfromtimestamp(mktime_tz(t)) + + try: + d = datetime.datetime.utcfromtimestamp(mktime_tz(t)) + except (OverflowError, ValueError, OSError): + # If you have a date like: + # - Date: Wed, 4 Jun 207777777777777777777714 17:50:46 0 + # -> OverflowError + # - Date:, 11 Sep 2016 23:22:904070804030804 +0100 + # -> ValueError + # - Date:, 11 Sep 2016 407080403080105:04 +0100 + # -> OSError (Python 3) + d = datetime.datetime.utcnow() + + return d def find_headers(mail):