Skip to content
Prev Previous commit
Next Next commit
added tests for obscure year inputs to parsedate_to_datetime
  • Loading branch information
gostak-dd committed May 21, 2025
commit df899ea9a666426b4e4f17db54d4e74881e0b3b2
17 changes: 17 additions & 0 deletions Lib/test/test_email/test_parsedate_to_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsedate_to_datetime tests belong in test_utils in DateTimeTests. (Those tests could maybe use some refactoring, but let's ignore that ;)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved it :)

def test(self):
expectations = {
"Sat, 15 Aug 0001 23:12:09 +0500": "0001",
"Thu, 1 Sep 1 23:12:09 +0800": "0001",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logically when doing a Postel "be generous in what you accept" recovery here, a single digit year should be treated as if it were a two digit year, which means the result here should 2001, which is what the current code produces.

Copy link
Copy Markdown
Contributor Author

@gostak-dd gostak-dd Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contradicts what @encukou said in the issue #126845. But I agree with you

"Thu, 7 Oct 123 23:12:09 +0500": "0123",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this result should be 2023, per the rfc text you quoted (three digit years get 1900 added). I have no idea why that settled on that logic, it makes no sense to me, but that's what the RFC says.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

}
for input_string, output_string in expectations.items():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subTest would be good here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

self.assertEqual(str(parsedate_to_datetime(input_string))[:4], output_string)

if __name__ == '__main__':
unittest.main()