diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index e311948cb09a7d6..eaccbde6ff4d0ab 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -154,7 +154,6 @@ def _parsedate_tz(data): # 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 diff --git a/Lib/test/test_email/test_utils.py b/Lib/test/test_email/test_utils.py index c9d09098b502f9d..9141230995cb9af 100644 --- a/Lib/test/test_email/test_utils.py +++ b/Lib/test/test_email/test_utils.py @@ -77,6 +77,30 @@ def test_parsedate_to_datetime_with_invalid_raises_valueerror(self): with self.subTest(dtstr=dtstr): self.assertRaises(ValueError, utils.parsedate_to_datetime, dtstr) + def test_parsedate_to_datetime_year_edge_cases(self): + expectations = { + # Various short-year formats that get expanded + "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", + # RFC 5322 section 4.3 boundaries for 2-digit years + "Mon, 1 Jan 0 00:00:00 +0000": "2000", + "Mon, 1 Jan 68 00:00:00 +0000": "2068", + "Mon, 1 Jan 69 00:00:00 +0000": "1969", + "Mon, 1 Jan 99 00:00:00 +0000": "1999", + # 3-digit year boundary + "Mon, 1 Jan 999 00:00:00 +0000": "2899", + # Pre-1900 four-digit year: illegal per RFC but we accept it + "Mon, 1 Jan 1000 00:00:00 +0000": "1000", + } + for input_string, expected_year in expectations.items(): + with self.subTest(input_string=input_string): + self.assertEqual( + str(utils.parsedate_to_datetime(input_string))[:4], + expected_year, + ) + class LocaltimeTests(unittest.TestCase): def test_localtime_is_tz_aware_daylight_true(self): diff --git a/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst new file mode 100644 index 000000000000000..a1c5db9e1110f08 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst @@ -0,0 +1,7 @@ +Fixed the :mod:`email` module parsing of three digit dates to +conform to :rfc:`5322`: three digit dates were previously +turned in to non-conformant four digit dates with a +leading ``0``. Now, per the RFC section 4.3, ``1990`` is added +to such dates to form compliant four digit years. + +Contributed by Gustaf Gyllensporre.