Skip to content

gh-126845: Some edge cases in email.utils.parsedate_to_datetime seem to differ from RFC2822 spec #134438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion Lib/email/_parseaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _parsedate_tz(data):
# 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:
if yy < 999:
# The year is between 1969 and 1999 (inclusive).
if yy > 68:
yy += 1900
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_email/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,18 @@ def test_formatdate_with_localtime(self):
string = utils.formatdate(timeval, localtime=True)
self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300')

# Issue #126845: Some edge cases seem to differ from RFC28222 spec
class ParsedateToDatetimeTest(unittest.TestCase):
def test_year_parsing_edge_cases(self):
expectations = {
"Sat, 15 Aug 0001 23:12:09 +0500": "2001",
"Thu, 1 Sep 1 23:12:09 +0800": "2001",
"Thu, 7 Oct 123 23:12:09 +0500": "2023",
"Tue, 17 Nov 2026 12:12:09 +0500": "2026",
}
for input_string, output_string in expectations.items():
with self.subTest(input_string=input_string):
self.assertEqual(str(utils.parsedate_to_datetime(input_string))[:4], output_string)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed _parsedate_tz to not evaluate 1 digit years as as 4-digit years, adhering to RFC 2855 4.3
Contributed by Gustaf Gyllensporre.
Loading