diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index 84917038874ba1..f909208a4d31ab 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -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 diff --git a/Lib/test/test_email/test_utils.py b/Lib/test/test_email/test_utils.py index c9d09098b502f9..c3739dbcd3633c 100644 --- a/Lib/test/test_email/test_utils.py +++ b/Lib/test/test_email/test_utils.py @@ -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() 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 00000000000000..90932a35c1ce7c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-21-15-32-04.gh-issue-126845.QhEGg6.rst @@ -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.