diff options
author | Daniel Axtens <dja@axtens.net> | 2017-06-28 17:48:48 +1000 |
---|---|---|
committer | Stephen Finucane <stephen@that.guru> | 2017-06-28 21:41:28 +0100 |
commit | 06b5f052747adb3c903e6c5faae6eef5defdd75b (patch) | |
tree | 732a2a50bd1016e93236ee3e3b29a88db399ae3e | |
parent | a128310bfa1c34aaba832fd488213005f2ba997c (diff) | |
download | patchwork-06b5f052747adb3c903e6c5faae6eef5defdd75b.tar patchwork-06b5f052747adb3c903e6c5faae6eef5defdd75b.tar.gz |
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 <dja@axtens.net>
Signed-off-by: Stephen Finucane <stephen@that.guru>
Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com>
-rw-r--r-- | patchwork/parser.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/patchwork/parser.py b/patchwork/parser.py index 47407cb..8a07a69 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): |