From f718468bf052385320c24cb4c3f01495260d0cd8 Mon Sep 17 00:00:00 2001 From: Gustaf <79180496+GGyll@users.noreply.github.com> Date: Mon, 19 May 2025 21:50:26 +0200 Subject: [PATCH 1/3] Let the system determine the correct tm_wday and tm_isdst --- Lib/test/test_strftime.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_strftime.py b/Lib/test/test_strftime.py index 752e31359cf206..375f6aaedd8934 100644 --- a/Lib/test/test_strftime.py +++ b/Lib/test/test_strftime.py @@ -39,7 +39,21 @@ def _update_variables(self, now): if now[3] < 12: self.ampm='(AM|am)' else: self.ampm='(PM|pm)' - self.jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0))) + jan1 = time.struct_time( + ( + now.tm_year, # Year + 1, # Month (January) + 1, # Day (1st) + 0, # Hour (0) + 0, # Minute (0) + 0, # Second (0) + -1, # tm_wday (will be determined) + 1, # tm_yday (day 1 of the year) + -1, # tm_isdst (let the system determine) + ) + ) + # use mktime to get the correct tm_wday and tm_isdst values + self.jan1 = time.localtime(time.mktime(jan1)) try: if now[8]: self.tz = time.tzname[1] From 940ec9869c03c5b9c955f37286b7ac288873b76a Mon Sep 17 00:00:00 2001 From: Gustaf Date: Mon, 19 May 2025 23:58:05 -0400 Subject: [PATCH 2/3] added digit check to remove forced 4-digit year evaluation --- Lib/email/_parseaddr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index 84917038874ba1..b97eede55424ca 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 len(str(yy)) > 2 and yy < 100: # The year is between 1969 and 1999 (inclusive). if yy > 68: yy += 1900 From 44084bd488dfaf04375b2b35183ba85470f7627c Mon Sep 17 00:00:00 2001 From: Gustaf Date: Mon, 19 May 2025 23:58:26 -0400 Subject: [PATCH 3/3] Added tests for obscure year inputs to parsedate_to_datetime --- Lib/test/test_parsedate_to_datetime.py | 18 ++++++++++++++++++ issues.py | 4 ++++ 2 files changed, 22 insertions(+) create mode 100644 Lib/test/test_parsedate_to_datetime.py create mode 100644 issues.py diff --git a/Lib/test/test_parsedate_to_datetime.py b/Lib/test/test_parsedate_to_datetime.py new file mode 100644 index 00000000000000..2656794b733ccf --- /dev/null +++ b/Lib/test/test_parsedate_to_datetime.py @@ -0,0 +1,18 @@ +# Test to see if parsedate_to_datetime returns the correct year for different digit numbers, adhering to the RFC2822 spec + +import unittest +from email.utils import parsedate_to_datetime + +class ParsedateToDatetimeTest(unittest.TestCase): + def test(self): + expectations = { + "Sat, 15 Aug 0001 23:12:09 +0500": "0001", + "Thu, 1 Sep 1 23:12:09 +0800": "0001", + "Thu, 7 Oct 123 23:12:09 +0500": "0123", + } + for input_string, output_string in expectations.items(): + self.assertEqual(str(parsedate_to_datetime(input_string))[:4], output_string) + +if __name__ == '__main__': + unittest.main() + diff --git a/issues.py b/issues.py new file mode 100644 index 00000000000000..4eb2dfd13e2011 --- /dev/null +++ b/issues.py @@ -0,0 +1,4 @@ +import datetime +from email.utils import parsedate_to_datetime + +print(parsedate_to_datetime("Sat, 15 Aug 5000 23:12:09 +0500"))