# HG changeset patch # User Jeffrey Finkelstein # Date 1282675641 14400 # Branch trunk # Node ID 832be5d5e657cd145576f180b2a221afd4c5c854 # Parent d2c464d2ee82764d3ad2e132cf94f5a4328ae053 Lib/email/_parseaddr.py: updated parsedate_tz() to account for two-digit years in date strings, as described by RFC822 diff -r d2c464d2ee82 -r 832be5d5e657 Lib/email/_parseaddr.py --- a/Lib/email/_parseaddr.py Sat Jul 03 15:57:30 2010 +0200 +++ b/Lib/email/_parseaddr.py Tue Aug 24 14:47:21 2010 -0400 @@ -1,4 +1,6 @@ # Copyright (C) 2002-2007 Python Software Foundation +# Copyright 2010 Jeffrey Finkelstein. Licensed to PSF under a Contributor +# Agreement. # Contact: email-sig@python.org """Email address parsing code. @@ -107,6 +109,18 @@ tss = int(tss) except ValueError: return None + # Check for a yy specified in two-digit format, then convert it to the + # appropriate four-digit format, according to the POSIX standard. RFC 822 + # calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822) + # mandates a 4-digit yy. For more information, see the documentation for + # the time module. + if yy < 100: + # The year is between 1969 and 1999 (inclusive). + if yy > 68: + yy += 1900 + # The year is between 2000 and 2068 (inclusive). + else: + yy += 2000 tzoffset = None tz = tz.upper() if tz in _timezones: diff -r d2c464d2ee82 -r 832be5d5e657 Lib/email/test/test_email.py --- a/Lib/email/test/test_email.py Sat Jul 03 15:57:30 2010 +0200 +++ b/Lib/email/test/test_email.py Tue Aug 24 14:47:21 2010 -0400 @@ -1,4 +1,6 @@ # Copyright (C) 2001-2010 Python Software Foundation +# Copyright 2010 Jeffrey Finkelstein. Licensed to PSF under a Contributor +# Agreement. # Contact: email-sig@python.org # email package unit tests @@ -2225,6 +2227,19 @@ eq(time.localtime(t)[:6], timetup[:6]) eq(int(time.strftime('%Y', timetup[:9])), 2003) + def test_parsedate_y2k(self): + """Test for parsing a date with a two-digit year. + + Parsing a date with a two-digit year should return the correct + four-digit year. RFC822 allows two-digit years, but RFC2822 (which + obsoletes RFC822) requires four-digit years. + + """ + self.assertEqual(Utils.parsedate_tz('25 Feb 03 13:47:26 -0800'), + Utils.parsedate_tz('25 Feb 2003 13:47:26 -0800')) + self.assertEqual(Utils.parsedate_tz('25 Feb 71 13:47:26 -0800'), + Utils.parsedate_tz('25 Feb 1971 13:47:26 -0800')) + def test_parseaddr_empty(self): self.assertEqual(Utils.parseaddr('<>'), ('', '')) self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '')