@@ -70,28 +70,35 @@ def test_strftime(self):
7070 with self .assertRaises (ValueError ):
7171 time .strftime ('%f' )
7272
73- def _bounds_checking (self , func = time . strftime ):
73+ def _bounds_checking (self , func ):
7474 # Make sure that strftime() checks the bounds of the various parts
75- #of the time tuple (0 is valid for *all* values).
75+ # of the time tuple (0 is valid for *all* values).
7676
7777 # The year field is tested by other test cases above
7878
7979 # Check month [1, 12] + zero support
80+ func ((1900 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , - 1 ))
81+ func ((1900 , 12 , 1 , 0 , 0 , 0 , 0 , 1 , - 1 ))
8082 self .assertRaises (ValueError , func ,
8183 (1900 , - 1 , 1 , 0 , 0 , 0 , 0 , 1 , - 1 ))
8284 self .assertRaises (ValueError , func ,
8385 (1900 , 13 , 1 , 0 , 0 , 0 , 0 , 1 , - 1 ))
8486 # Check day of month [1, 31] + zero support
87+ func ((1900 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , - 1 ))
88+ func ((1900 , 1 , 31 , 0 , 0 , 0 , 0 , 1 , - 1 ))
8589 self .assertRaises (ValueError , func ,
8690 (1900 , 1 , - 1 , 0 , 0 , 0 , 0 , 1 , - 1 ))
8791 self .assertRaises (ValueError , func ,
8892 (1900 , 1 , 32 , 0 , 0 , 0 , 0 , 1 , - 1 ))
8993 # Check hour [0, 23]
94+ func ((1900 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , - 1 ))
95+ func ((1900 , 1 , 1 , 23 , 0 , 0 , 0 , 1 , - 1 ))
9096 self .assertRaises (ValueError , func ,
9197 (1900 , 1 , 1 , - 1 , 0 , 0 , 0 , 1 , - 1 ))
9298 self .assertRaises (ValueError , func ,
9399 (1900 , 1 , 1 , 24 , 0 , 0 , 0 , 1 , - 1 ))
94100 # Check minute [0, 59]
101+ func ((1900 , 1 , 1 , 0 , 59 , 0 , 0 , 1 , - 1 ))
95102 self .assertRaises (ValueError , func ,
96103 (1900 , 1 , 1 , 0 , - 1 , 0 , 0 , 1 , - 1 ))
97104 self .assertRaises (ValueError , func ,
@@ -101,15 +108,21 @@ def _bounds_checking(self, func=time.strftime):
101108 (1900 , 1 , 1 , 0 , 0 , - 1 , 0 , 1 , - 1 ))
102109 # C99 only requires allowing for one leap second, but Python's docs say
103110 # allow two leap seconds (0..61)
111+ func ((1900 , 1 , 1 , 0 , 0 , 60 , 0 , 1 , - 1 ))
112+ func ((1900 , 1 , 1 , 0 , 0 , 61 , 0 , 1 , - 1 ))
104113 self .assertRaises (ValueError , func ,
105114 (1900 , 1 , 1 , 0 , 0 , 62 , 0 , 1 , - 1 ))
106115 # No check for upper-bound day of week;
107116 # value forced into range by a ``% 7`` calculation.
108117 # Start check at -2 since gettmarg() increments value before taking
109118 # modulo.
119+ self .assertEqual (func ((1900 , 1 , 1 , 0 , 0 , 0 , - 1 , 1 , - 1 )),
120+ func ((1900 , 1 , 1 , 0 , 0 , 0 , + 6 , 1 , - 1 )))
110121 self .assertRaises (ValueError , func ,
111122 (1900 , 1 , 1 , 0 , 0 , 0 , - 2 , 1 , - 1 ))
112123 # Check day of the year [1, 366] + zero support
124+ func ((1900 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , - 1 ))
125+ func ((1900 , 1 , 1 , 0 , 0 , 0 , 0 , 366 , - 1 ))
113126 self .assertRaises (ValueError , func ,
114127 (1900 , 1 , 1 , 0 , 0 , 0 , 0 , - 1 , - 1 ))
115128 self .assertRaises (ValueError , func ,
@@ -299,6 +312,8 @@ def yearstr(self, y):
299312 raise NotImplementedError ()
300313
301314class _TestAsctimeYear :
315+ _format = '%d'
316+
302317 def yearstr (self , y ):
303318 return time .asctime ((y ,) + (0 ,) * 8 ).split ()[- 1 ]
304319
@@ -308,8 +323,41 @@ def test_large_year(self):
308323 self .assertEqual (self .yearstr (123456789 ), '123456789' )
309324
310325class _TestStrftimeYear :
326+
327+ # Issue 13305: For years < 1000, the value is not always
328+ # padded to 4 digits across platforms. The C standard
329+ # assumes year >= 1900, so it does not specify the number
330+ # of digits.
331+
332+ if time .strftime ('%Y' , (1 ,) + (0 ,) * 8 ) == '0001' :
333+ _format = '%04d'
334+ else :
335+ _format = '%d'
336+
311337 def yearstr (self , y ):
312- return time .strftime ('%Y' , (y ,) + (0 ,) * 8 ).split ()[- 1 ]
338+ return time .strftime ('%Y' , (y ,) + (0 ,) * 8 )
339+
340+ def test_4dyear (self ):
341+ # Check that we can return the zero padded value.
342+ if self ._format == '%04d' :
343+ self .test_year ('%04d' )
344+ else :
345+ def year4d (y ):
346+ return time .strftime ('%4Y' , (y ,) + (0 ,) * 8 )
347+ self .test_year ('%04d' , func = year4d )
348+
349+ class _Test4dYear (_BaseYearTest ):
350+ _format = '%d'
351+
352+ def test_year (self , fmt = None , func = None ):
353+ fmt = fmt or self ._format
354+ func = func or self .yearstr
355+ self .assertEqual (func (1 ), fmt % 1 )
356+ self .assertEqual (func (68 ), fmt % 68 )
357+ self .assertEqual (func (69 ), fmt % 69 )
358+ self .assertEqual (func (99 ), fmt % 99 )
359+ self .assertEqual (func (999 ), fmt % 999 )
360+ self .assertEqual (func (9999 ), fmt % 9999 )
313361
314362 def test_large_year (self ):
315363 # Check that it doesn't crash for year > 9999
@@ -321,23 +369,13 @@ def test_large_year(self):
321369 self .assertEqual (text , '12345' )
322370 self .assertEqual (self .yearstr (123456789 ), '123456789' )
323371
324- class _Test4dYear (_BaseYearTest ):
325-
326- def test_year (self ):
327- self .assertIn (self .yearstr (1 ), ('1' , '0001' ))
328- self .assertIn (self .yearstr (68 ), ('68' , '0068' ))
329- self .assertIn (self .yearstr (69 ), ('69' , '0069' ))
330- self .assertIn (self .yearstr (99 ), ('99' , '0099' ))
331- self .assertIn (self .yearstr (999 ), ('999' , '0999' ))
332- self .assertEqual (self .yearstr (9999 ), '9999' )
333-
334372 def test_negative (self ):
335373 try :
336374 text = self .yearstr (- 1 )
337375 except ValueError :
338376 # strftime() is limited to [1; 9999] with Visual Studio
339377 return
340- self .assertIn (text , ( '-1' , '-001' ) )
378+ self .assertEqual (text , self . _format % - 1 )
341379
342380 self .assertEqual (self .yearstr (- 1234 ), '-1234' )
343381 self .assertEqual (self .yearstr (- 123456 ), '-123456' )
0 commit comments