@@ -307,6 +307,43 @@ def test_strptime_leap_year(self):
307307 r'.*day of month without a year.*' ):
308308 time .strptime ('02-07 18:28' , '%m-%d %H:%M' )
309309
310+ def test_strptime_accepting_locale_specific_year_with_fewer_digits (self ):
311+ # GH-124529
312+ concerned_formats = '%c' , '%x'
313+
314+ def run_subtest ():
315+ input_str = sample_str .replace (sample_year_digits , year_digits )
316+ reason = (f"test strptime accepting locale-specific "
317+ f"year representation with fewer digits "
318+ f"- for { fmt = } and { input_str = } ({ year = } )" )
319+ fail_msg = f"{ reason } - failed"
320+ expected = (year ,) + sample_tt [1 :6 ]
321+ with self .subTest (reason = reason ):
322+ try :
323+ parsed_tt = time .strptime (input_str , fmt )
324+ except ValueError as exc :
325+ self .fail (f"{ fail_msg } ; parsing error: { exc !r} " )
326+ self .assertEqual (parsed_tt [:6 ], expected , fail_msg )
327+
328+ sample_tt = (1999 , 3 , 17 ) + (0 ,) * 6
329+ for fmt in concerned_formats :
330+ with self .subTest (fmt = fmt ):
331+ sample_str = time .strftime (fmt , sample_tt )
332+ if (sample_year_digits := '1999' ) in sample_str :
333+ for year in [1 , 9 , 10 , 99 , 100 , 999 ]:
334+ year_digits = str (year )
335+ run_subtest ()
336+ elif (sample_year_digits := '99' ) in sample_str :
337+ for year in [2000 , 2001 , 2009 ]:
338+ year_digits = str (year - 2000 )
339+ run_subtest ()
340+ else :
341+ self .fail (f"it seems that time.strftime(fmt, ...)="
342+ f"{ sample_str !r} does not include year="
343+ f"{ sample_tt [0 ]!r} in any expected format "
344+ f"(is there something severely wrong with "
345+ f"current locale?)" )
346+
310347 def test_asctime (self ):
311348 time .asctime (time .gmtime (self .t ))
312349
0 commit comments