Skip to content

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

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 len(str(yy)) > 2 and yy < 100:
# The year is between 1969 and 1999 (inclusive).
if yy > 68:
yy += 1900
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_parsedate_to_datetime.py
Original file line number Diff line number Diff line change
@@ -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()

16 changes: 15 additions & 1 deletion Lib/test/test_strftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions issues.py
Original file line number Diff line number Diff line change
@@ -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"))
Loading