77import os
88import sys
99from test import support
10- from test .support import skip_if_buggy_ucrt_strfptime , warnings_helper
10+ from test .support import warnings_helper
11+ from test .support import skip_if_buggy_ucrt_strfptime , run_with_locales
1112from datetime import date as datetime_date
1213
1314import _strptime
@@ -289,54 +290,64 @@ def test_unconverteddata(self):
289290 # Check ValueError is raised when there is unconverted data
290291 self .assertRaises (ValueError , _strptime ._strptime_time , "10 12" , "%m" )
291292
292- def helper (self , directive , position ):
293+ def roundtrip (self , fmt , position , time_tuple = None ):
293294 """Helper fxn in testing."""
294- fmt = "%d %Y" if directive == 'd' else "%" + directive
295- strf_output = time .strftime (fmt , self .time_tuple )
295+ if time_tuple is None :
296+ time_tuple = self .time_tuple
297+ strf_output = time .strftime (fmt , time_tuple )
296298 strp_output = _strptime ._strptime_time (strf_output , fmt )
297- self .assertTrue (strp_output [position ] == self .time_tuple [position ],
298- "testing of '%s' directive failed; '%s' -> %s != %s" %
299- (directive , strf_output , strp_output [position ],
300- self .time_tuple [position ]))
299+ self .assertEqual (strp_output [position ], time_tuple [position ],
300+ "testing of %r format failed; %r -> %r != %r" %
301+ (fmt , strf_output , strp_output [position ],
302+ time_tuple [position ]))
303+ if support .verbose >= 3 :
304+ print ("testing of %r format: %r -> %r" %
305+ (fmt , strf_output , strp_output [position ]))
301306
302307 def test_year (self ):
303308 # Test that the year is handled properly
304- for directive in ('y' , 'Y' ):
305- self .helper (directive , 0 )
309+ self .roundtrip ('%Y' , 0 )
310+ self .roundtrip ('%y' , 0 )
311+ self .roundtrip ('%Y' , 0 , (1900 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 ))
312+
306313 # Must also make sure %y values are correct for bounds set by Open Group
307- for century , bounds in ((1900 , ('69' , '99' )), (2000 , ('00' , '68' ))):
308- for bound in bounds :
309- strp_output = _strptime ._strptime_time (bound , '%y' )
310- expected_result = century + int (bound )
311- self .assertTrue (strp_output [0 ] == expected_result ,
312- "'y' test failed; passed in '%s' "
313- "and returned '%s'" % (bound , strp_output [0 ]))
314+ strptime = _strptime ._strptime_time
315+ self .assertEqual (strptime ('00' , '%y' )[0 ], 2000 )
316+ self .assertEqual (strptime ('68' , '%y' )[0 ], 2068 )
317+ self .assertEqual (strptime ('69' , '%y' )[0 ], 1969 )
318+ self .assertEqual (strptime ('99' , '%y' )[0 ], 1999 )
314319
315320 def test_month (self ):
316321 # Test for month directives
317- for directive in ('B' , 'b' , 'm' ):
318- self .helper (directive , 1 )
322+ self .roundtrip ('%m' , 1 )
323+
324+ @run_with_locales ('LC_TIME' , 'en_US' , 'fr_FR' , 'de_DE' , 'ja_JP' , '' )
325+ def test_month_locale (self ):
326+ # Test for month directives
327+ self .roundtrip ('%B' , 1 )
328+ self .roundtrip ('%b' , 1 )
319329
320330 def test_day (self ):
321331 # Test for day directives
322- self .helper ( 'd ' , 2 )
332+ self .roundtrip ( '%d %Y ' , 2 )
323333
324334 def test_hour (self ):
325335 # Test hour directives
326- self .helper ('H' , 3 )
327- strf_output = time .strftime ("%I %p" , self .time_tuple )
328- strp_output = _strptime ._strptime_time (strf_output , "%I %p" )
329- self .assertTrue (strp_output [3 ] == self .time_tuple [3 ],
330- "testing of '%%I %%p' directive failed; '%s' -> %s != %s" %
331- (strf_output , strp_output [3 ], self .time_tuple [3 ]))
336+ self .roundtrip ('%H' , 3 )
337+
338+ # NB: Only works on locales with AM/PM
339+ @run_with_locales ('LC_TIME' , 'en_US' , 'ja_JP' )
340+ def test_hour_locale (self ):
341+ # Test hour directives
342+ self .roundtrip ('%I %p' , 3 )
332343
333344 def test_minute (self ):
334345 # Test minute directives
335- self .helper ( ' M' , 4 )
346+ self .roundtrip ( '% M' , 4 )
336347
337348 def test_second (self ):
338349 # Test second directives
339- self .helper ( ' S' , 5 )
350+ self .roundtrip ( '% S' , 5 )
340351
341352 def test_fraction (self ):
342353 # Test microseconds
@@ -347,12 +358,18 @@ def test_fraction(self):
347358
348359 def test_weekday (self ):
349360 # Test weekday directives
350- for directive in ('A' , 'a' , 'w' , 'u' ):
351- self .helper (directive ,6 )
361+ self .roundtrip ('%w' , 6 )
362+ self .roundtrip ('%u' , 6 )
363+
364+ @run_with_locales ('LC_TIME' , 'en_US' , 'fr_FR' , 'de_DE' , 'ja_JP' , '' )
365+ def test_weekday_locale (self ):
366+ # Test weekday directives
367+ self .roundtrip ('%A' , 6 )
368+ self .roundtrip ('%a' , 6 )
352369
353370 def test_julian (self ):
354371 # Test julian directives
355- self .helper ( ' j' , 7 )
372+ self .roundtrip ( '% j' , 7 )
356373
357374 def test_offset (self ):
358375 one_hour = 60 * 60
@@ -449,20 +466,26 @@ def test_bad_timezone(self):
449466 "time.daylight set to %s and passing in %s" %
450467 (time .tzname , tz_value , time .daylight , tz_name ))
451468
452- def test_date_time (self ):
469+ # NB: Does not roundtrip on some locales like hif_FJ.
470+ @run_with_locales ('LC_TIME' , 'en_US' , 'fr_FR' , 'de_DE' , 'ja_JP' , '' )
471+ def test_date_time_locale (self ):
453472 # Test %c directive
454- for position in range ( 6 ):
455- self .helper ( ' c' , position )
473+ self . roundtrip ( '%c' , slice ( 0 , 6 ))
474+ self .roundtrip ( '% c' , slice ( 0 , 6 ), ( 1900 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 ) )
456475
457- def test_date (self ):
476+ # NB: Dates before 1969 do not work on locales: C, POSIX,
477+ # az_IR, fa_IR, sd_PK, uk_UA.
478+ @run_with_locales ('LC_TIME' , 'en_US' , 'fr_FR' , 'de_DE' , 'ja_JP' )
479+ def test_date_locale (self ):
458480 # Test %x directive
459- for position in range (0 ,3 ):
460- self .helper ( ' x' , position )
481+ self . roundtrip ( '%x' , slice (0 , 3 ))
482+ self .roundtrip ( '% x' , slice ( 0 , 3 ), ( 1900 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 ) )
461483
462- def test_time (self ):
484+ # NB: Does not distinguish AM/PM time on a number of locales.
485+ @run_with_locales ('LC_TIME' , 'en_US' , 'fr_FR' , 'de_DE' , 'ja_JP' )
486+ def test_time_locale (self ):
463487 # Test %X directive
464- for position in range (3 ,6 ):
465- self .helper ('X' , position )
488+ self .roundtrip ('%X' , slice (3 , 6 ))
466489
467490 def test_percent (self ):
468491 # Make sure % signs are handled properly
0 commit comments