# HG changeset patch # User Jeffrey Finkelstein # Date 1282684597 14400 # Branch py3k # Node ID 9297db1bb118841505fac5cffd6ca9626bf1adaa # Parent 232437f0ca1e1dc733efce8c2eee7664bd81f0b8 Lib/email/_parseaddr.py: updated parsedate_tz() to account for two-digit years in date strings, as described by RFC822 diff -r 232437f0ca1e -r 9297db1bb118 Lib/email/_parseaddr.py --- a/Lib/email/_parseaddr.py Tue Aug 24 07:20:30 2010 +0200 +++ b/Lib/email/_parseaddr.py Tue Aug 24 17:16:37 2010 -0400 @@ -107,6 +107,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 232437f0ca1e -r 9297db1bb118 Lib/email/test/test_email.py --- a/Lib/email/test/test_email.py Tue Aug 24 07:20:30 2010 +0200 +++ b/Lib/email/test/test_email.py Tue Aug 24 17:16:37 2010 -0400 @@ -2234,6 +2234,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('<>')), '')