7
7
import os
8
8
import sys
9
9
from 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
11
12
from datetime import date as datetime_date
12
13
13
14
import _strptime
@@ -289,54 +290,64 @@ def test_unconverteddata(self):
289
290
# Check ValueError is raised when there is unconverted data
290
291
self .assertRaises (ValueError , _strptime ._strptime_time , "10 12" , "%m" )
291
292
292
- def helper (self , directive , position ):
293
+ def roundtrip (self , fmt , position , time_tuple = None ):
293
294
"""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 )
296
298
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 ]))
301
306
302
307
def test_year (self ):
303
308
# 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
+
306
313
# 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 )
314
319
315
320
def test_month (self ):
316
321
# 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' , 'he_IL' , '' )
325
+ def test_month_locale (self ):
326
+ # Test for month directives
327
+ self .roundtrip ('%B' , 1 )
328
+ self .roundtrip ('%b' , 1 )
319
329
320
330
def test_day (self ):
321
331
# Test for day directives
322
- self .helper ( 'd ' , 2 )
332
+ self .roundtrip ( '%d %Y ' , 2 )
323
333
324
334
def test_hour (self ):
325
335
# 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 )
332
343
333
344
def test_minute (self ):
334
345
# Test minute directives
335
- self .helper ( ' M' , 4 )
346
+ self .roundtrip ( '% M' , 4 )
336
347
337
348
def test_second (self ):
338
349
# Test second directives
339
- self .helper ( ' S' , 5 )
350
+ self .roundtrip ( '% S' , 5 )
340
351
341
352
def test_fraction (self ):
342
353
# Test microseconds
@@ -347,12 +358,18 @@ def test_fraction(self):
347
358
348
359
def test_weekday (self ):
349
360
# 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 )
352
369
353
370
def test_julian (self ):
354
371
# Test julian directives
355
- self .helper ( ' j' , 7 )
372
+ self .roundtrip ( '% j' , 7 )
356
373
357
374
def test_offset (self ):
358
375
one_hour = 60 * 60
@@ -449,20 +466,26 @@ def test_bad_timezone(self):
449
466
"time.daylight set to %s and passing in %s" %
450
467
(time .tzname , tz_value , time .daylight , tz_name ))
451
468
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' , 'he_IL' , '' )
471
+ def test_date_time_locale (self ):
453
472
# 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 ) )
456
475
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 ):
458
480
# 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 ) )
461
483
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 ):
463
487
# Test %X directive
464
- for position in range (3 ,6 ):
465
- self .helper ('X' , position )
488
+ self .roundtrip ('%X' , slice (3 , 6 ))
466
489
467
490
def test_percent (self ):
468
491
# Make sure % signs are handled properly
0 commit comments