GH-70647: Deprecate strptime day of month parsing without a year present to avoid leap-year bugs (GH-117107)

This commit is contained in:
Gregory P. Smith 2024-04-03 05:19:49 -07:00 committed by GitHub
parent 595bb496b0
commit 33ee5cb3e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 117 additions and 1 deletions

View file

@ -277,6 +277,8 @@ class TimeTestCase(unittest.TestCase):
'j', 'm', 'M', 'p', 'S',
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
format = '%' + directive
if directive == 'd':
format += ',%Y' # Avoid GH-70647.
strf_output = time.strftime(format, tt)
try:
time.strptime(strf_output, format)
@ -299,6 +301,12 @@ class TimeTestCase(unittest.TestCase):
time.strptime('19', '%Y %')
self.assertIs(e.exception.__suppress_context__, True)
def test_strptime_leap_year(self):
# GH-70647: warns if parsing a format with a day and no year.
with self.assertWarnsRegex(DeprecationWarning,
r'.*day of month without a year.*'):
time.strptime('02-07 18:28', '%m-%d %H:%M')
def test_asctime(self):
time.asctime(time.gmtime(self.t))