41
43
# Make sure that strftime() checks the bounds of the various parts
42
44
#of the time tuple (0 is valid for *all* values).
44
# Check year [1900, max(int)]
45
self.assertRaises(ValueError, func,
46
(1899, 1, 1, 0, 0, 0, 0, 1, -1))
48
self.assertRaises(ValueError, func,
49
(-1, 1, 1, 0, 0, 0, 0, 1, -1))
50
self.assertRaises(ValueError, func,
51
(100, 1, 1, 0, 0, 0, 0, 1, -1))
46
# The year field is tested by other test cases above
52
48
# Check month [1, 12] + zero support
53
49
self.assertRaises(ValueError, func,
54
50
(1900, -1, 1, 0, 0, 0, 0, 1, -1))
122
119
def test_asctime(self):
123
120
time.asctime(time.gmtime(self.t))
122
# Max year is only limited by the size of C int.
123
sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
124
bigyear = (1 << 8 * sizeof_int - 1) - 1
125
asc = time.asctime((bigyear, 6, 1) + (0,)*6)
126
self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
127
self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
124
128
self.assertRaises(TypeError, time.asctime, 0)
129
self.assertRaises(TypeError, time.asctime, ())
130
self.assertRaises(TypeError, time.asctime, (0,) * 10)
126
132
def test_asctime_bounding_check(self):
127
133
self._bounds_checking(time.asctime)
135
def test_ctime(self):
136
t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
137
self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
138
t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
139
self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
140
for year in [-100, 100, 1000, 2000, 10000]:
142
testval = time.mktime((year, 1, 10) + (0,)*6)
143
except (ValueError, OverflowError):
144
# If mktime fails, ctime will fail too. This may happen
148
self.assertEqual(time.ctime(testval)[20:], str(year))
129
150
@unittest.skipIf(not hasattr(time, "tzset"),
130
151
"time module has no attribute tzset")
131
152
def test_tzset(self):
240
261
# This should not cause an exception
241
262
time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
265
class _BaseYearTest(unittest.TestCase):
269
self.saved_accept2dyear = time.accept2dyear
270
time.accept2dyear = self.accept2dyear
273
time.accept2dyear = self.saved_accept2dyear
275
def yearstr(self, y):
276
raise NotImplementedError()
278
class _TestAsctimeYear:
279
def yearstr(self, y):
280
return time.asctime((y,) + (0,) * 8).split()[-1]
282
def test_large_year(self):
283
# Check that it doesn't crash for year > 9999
284
self.assertEqual(self.yearstr(12345), '12345')
285
self.assertEqual(self.yearstr(123456789), '123456789')
287
class _TestStrftimeYear:
288
def yearstr(self, y):
289
return time.strftime('%Y', (y,) + (0,) * 8).split()[-1]
291
def test_large_year(self):
292
# Check that it doesn't crash for year > 9999
294
text = self.yearstr(12345)
296
# strftime() is limited to [1; 9999] with Visual Studio
298
# Issue #10864: OpenIndiana is limited to 4 digits,
299
# but Python doesn't raise a ValueError
300
#self.assertEqual(text, '12345')
301
#self.assertEqual(self.yearstr(123456789), '123456789')
302
self.assertIn(text, ('2345', '12345'))
303
self.assertIn(self.yearstr(123456789), ('123456789', '6789'))
305
class _Test2dYear(_BaseYearTest):
309
with support.check_warnings():
310
self.assertEqual(self.yearstr(0), '2000')
311
self.assertEqual(self.yearstr(69), '1969')
312
self.assertEqual(self.yearstr(68), '2068')
313
self.assertEqual(self.yearstr(99), '1999')
315
def test_invalid(self):
316
self.assertRaises(ValueError, self.yearstr, -1)
317
self.assertRaises(ValueError, self.yearstr, 100)
318
self.assertRaises(ValueError, self.yearstr, 999)
320
class _Test4dYear(_BaseYearTest):
324
self.assertIn(self.yearstr(1), ('1', '0001'))
325
self.assertIn(self.yearstr(68), ('68', '0068'))
326
self.assertIn(self.yearstr(69), ('69', '0069'))
327
self.assertIn(self.yearstr(99), ('99', '0099'))
328
self.assertIn(self.yearstr(999), ('999', '0999'))
329
self.assertEqual(self.yearstr(9999), '9999')
331
def test_negative(self):
333
text = self.yearstr(-1)
335
# strftime() is limited to [1; 9999] with Visual Studio
337
self.assertIn(text, ('-1', '-001'))
339
self.assertEqual(self.yearstr(-1234), '-1234')
340
self.assertEqual(self.yearstr(-123456), '-123456')
343
def test_mktime(self):
345
for t in (-2, -1, 0, 1):
347
tt = time.localtime(t)
348
except (OverflowError, ValueError):
351
self.assertEqual(time.mktime(tt), t)
352
# It may not be possible to reliably make mktime return error
353
# on all platfom. This will make sure that no other exception
354
# than OverflowError is raised for an extreme value.
356
time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
357
except OverflowError:
360
class TestAsctimeAccept2dYear(_TestAsctimeYear, _Test2dYear):
363
class TestStrftimeAccept2dYear(_TestStrftimeYear, _Test2dYear):
366
class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
369
class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
372
class Test2dyearBool(_TestAsctimeYear, _Test2dYear):
375
class Test4dyearBool(_TestAsctimeYear, _Test4dYear):
378
class TestAccept2YearBad(_TestAsctimeYear, _BaseYearTest):
381
raise RuntimeError('boo')
383
def test_2dyear(self):
385
def test_invalid(self):
386
self.assertRaises(RuntimeError, self.yearstr, 200)
244
support.run_unittest(TimeTestCase, TestLocale)
390
support.run_unittest(
393
TestAsctimeAccept2dYear,
394
TestStrftimeAccept2dYear,
246
401
if __name__ == "__main__":