Skip to content

Commit 219d1c8

Browse files
committed
#1194222: make parsedate always return RFC2822 four character years.
Two character years are now converted to four character years using the Posix standard rule (<68 == 2000, >=68==1900). This makes the parsed date RFC2822 compliant even if the input is not. Patch and test by Jeffrey Finkelstein.
1 parent 1970b62 commit 219d1c8

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

Lib/email/_parseaddr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ def parsedate_tz(data):
107107
tss = int(tss)
108108
except ValueError:
109109
return None
110+
# Check for a yy specified in two-digit format, then convert it to the
111+
# appropriate four-digit format, according to the POSIX standard. RFC 822
112+
# calls for a two-digit yy, but RFC 2822 (which obsoletes RFC 822)
113+
# mandates a 4-digit yy. For more information, see the documentation for
114+
# the time module.
115+
if yy < 100:
116+
# The year is between 1969 and 1999 (inclusive).
117+
if yy > 68:
118+
yy += 1900
119+
# The year is between 2000 and 2068 (inclusive).
120+
else:
121+
yy += 2000
110122
tzoffset = None
111123
tz = tz.upper()
112124
if tz in _timezones:

Lib/email/test/test_email.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,19 @@ def test_parsedate_acceptable_to_time_functions(self):
22342234
eq(time.localtime(t)[:6], timetup[:6])
22352235
eq(int(time.strftime('%Y', timetup[:9])), 2003)
22362236

2237+
def test_parsedate_y2k(self):
2238+
"""Test for parsing a date with a two-digit year.
2239+
2240+
Parsing a date with a two-digit year should return the correct
2241+
four-digit year. RFC822 allows two-digit years, but RFC2822 (which
2242+
obsoletes RFC822) requires four-digit years.
2243+
2244+
"""
2245+
self.assertEqual(utils.parsedate_tz('25 Feb 03 13:47:26 -0800'),
2246+
utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'))
2247+
self.assertEqual(utils.parsedate_tz('25 Feb 71 13:47:26 -0800'),
2248+
utils.parsedate_tz('25 Feb 1971 13:47:26 -0800'))
2249+
22372250
def test_parseaddr_empty(self):
22382251
self.assertEqual(utils.parseaddr('<>'), ('', ''))
22392252
self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '')

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ Niels Ferguson
260260
Sebastian Fernandez
261261
Vincent Fiack
262262
Tomer Filiba
263+
Jeffrey Finkelstein
263264
Russell Finn
264265
Nils Fischbeck
265266
Frederik Fix

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Extensions
126126
Library
127127
-------
128128

129+
- Issue #1194222: email.utils.parsedate now returns RFC2822 compliant four
130+
character years even if the message contains RFC822 two character years.
131+
129132
- Issue #8750: Fixed MutableSet's methods to correctly handle reflexive
130133
operations on its self, namely x -= x and x ^= x.
131134

0 commit comments

Comments
 (0)