blob: 8499af81577107554a4b100ecbe20ecf8fa871dc [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Barry Warsawb0c22321996-12-06 23:30:07 +00002import time
Fred Drakebc561982001-05-22 17:02:02 +00003import unittest
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +00004import locale
Alexander Belopolskyb9588b52011-01-04 16:34:30 +00005import sysconfig
Senthil Kumaran8f377a32011-04-06 12:54:06 +08006import sys
Alexander Belopolskyc64708a2011-01-07 19:59:19 +00007import warnings
Barry Warsawb0c22321996-12-06 23:30:07 +00008
Fred Drakebc561982001-05-22 17:02:02 +00009class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000010
Fred Drakebc561982001-05-22 17:02:02 +000011 def setUp(self):
12 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000013
Fred Drakebc561982001-05-22 17:02:02 +000014 def test_data_attributes(self):
15 time.altzone
16 time.daylight
17 time.timezone
18 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000019
Fred Drakebc561982001-05-22 17:02:02 +000020 def test_clock(self):
21 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000022
Fred Drakebc561982001-05-22 17:02:02 +000023 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000024 self.assertEqual(time.ctime(self.t),
25 time.asctime(time.localtime(self.t)))
26 self.assertEqual(int(time.mktime(time.localtime(self.t))),
27 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000028
29 def test_sleep(self):
30 time.sleep(1.2)
31
32 def test_strftime(self):
33 tt = time.gmtime(self.t)
34 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
35 'j', 'm', 'M', 'p', 'S',
36 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
37 format = ' %' + directive
38 try:
39 time.strftime(format, tt)
40 except ValueError:
41 self.fail('conversion specifier: %r failed.' % format)
42
Senthil Kumaran8f377a32011-04-06 12:54:06 +080043 # Issue #10762: Guard against invalid/non-supported format string
44 # so that Python don't crash (Windows crashes when the format string
45 # input to [w]strftime is not kosher.
46 if sys.platform.startswith('win'):
47 with self.assertRaises(ValueError):
48 time.strftime('%f')
49
Alexander Belopolsky38e29962010-10-01 14:18:49 +000050 def _bounds_checking(self, func=time.strftime):
Brett Cannond1080a32004-03-02 04:38:10 +000051 # Make sure that strftime() checks the bounds of the various parts
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000053
Victor Stinner73ea29c2011-01-08 01:56:31 +000054 # The year field is tested by other test cases above
55
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 # Check month [1, 12] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000057 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000059 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000060 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061 # Check day of month [1, 31] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000062 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000064 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000065 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 # Check hour [0, 23]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000067 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000068 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000069 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000070 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071 # Check minute [0, 59]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000072 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000073 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000074 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000075 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000077 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000078 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
79 # C99 only requires allowing for one leap second, but Python's docs say
80 # allow two leap seconds (0..61)
Alexander Belopolsky38e29962010-10-01 14:18:49 +000081 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000082 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
83 # No check for upper-bound day of week;
84 # value forced into range by a ``% 7`` calculation.
85 # Start check at -2 since gettmarg() increments value before taking
86 # modulo.
Alexander Belopolsky38e29962010-10-01 14:18:49 +000087 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000088 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 # Check day of the year [1, 366] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000090 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000092 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000093 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +000094
Alexander Belopolsky38e29962010-10-01 14:18:49 +000095 def test_strftime_bounding_check(self):
96 self._bounds_checking(lambda tup: time.strftime('', tup))
97
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098 def test_default_values_for_zero(self):
99 # Make sure that using all zeros uses the proper default values.
100 # No test for daylight savings since strftime() does not change output
101 # based on its value.
102 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000103 with support.check_warnings():
104 result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000105 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000107 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108 # Should be able to go round-trip from strftime to strptime without
109 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000110 tt = time.gmtime(self.t)
111 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
112 'j', 'm', 'M', 'p', 'S',
113 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 format = '%' + directive
115 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000116 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000118 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 self.fail("conversion specifier %r failed with '%s' input." %
120 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000121
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000122 def test_strptime_bytes(self):
123 # Make sure only strings are accepted as arguments to strptime.
124 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
125 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
126
Fred Drakebc561982001-05-22 17:02:02 +0000127 def test_asctime(self):
128 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000129
130 # Max year is only limited by the size of C int.
Antoine Pitrou1ec121d2011-01-04 22:54:30 +0000131 sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000132 bigyear = (1 << 8 * sizeof_int - 1) - 1
133 asc = time.asctime((bigyear, 6, 1) + (0,)*6)
134 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
135 self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
Fred Drakebc561982001-05-22 17:02:02 +0000136 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000137 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000138 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000139
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000140 def test_asctime_bounding_check(self):
141 self._bounds_checking(time.asctime)
142
Georg Brandle10608c2011-01-02 22:33:43 +0000143 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000144 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
145 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
146 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
147 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000148 for year in [-100, 100, 1000, 2000, 10000]:
149 try:
150 testval = time.mktime((year, 1, 10) + (0,)*6)
151 except (ValueError, OverflowError):
152 # If mktime fails, ctime will fail too. This may happen
153 # on some platforms.
154 pass
155 else:
156 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000157
R. David Murray6ecf76e2010-12-14 01:22:50 +0000158 @unittest.skipIf(not hasattr(time, "tzset"),
159 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000160 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000161
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000162 from os import environ
163
Tim Peters0eadaac2003-04-24 16:02:54 +0000164 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000165 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000166 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000167
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000168 # These formats are correct for 2002, and possibly future years
169 # This format is the 'standard' as documented at:
170 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
171 # They are also documented in the tzset(3) man page on most Unix
172 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000173 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000174 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
175 utc='UTC+0'
176
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000177 org_TZ = environ.get('TZ',None)
178 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000179 # Make sure we can switch to UTC time and results are correct
180 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000181 # Note that altzone is undefined in UTC, as there is no DST
182 environ['TZ'] = eastern
183 time.tzset()
184 environ['TZ'] = utc
185 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000186 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000187 time.gmtime(xmas2002), time.localtime(xmas2002)
188 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000189 self.assertEqual(time.daylight, 0)
190 self.assertEqual(time.timezone, 0)
191 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000192
193 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000194 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000195 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000196 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
197 self.assertEqual(time.tzname, ('EST', 'EDT'))
198 self.assertEqual(len(time.tzname), 2)
199 self.assertEqual(time.daylight, 1)
200 self.assertEqual(time.timezone, 18000)
201 self.assertEqual(time.altzone, 14400)
202 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
203 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000204
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000205 # Now go to the southern hemisphere.
206 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000207 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000208 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
209 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
210 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
211 self.assertEqual(len(time.tzname), 2)
212 self.assertEqual(time.daylight, 1)
213 self.assertEqual(time.timezone, -36000)
214 self.assertEqual(time.altzone, -39600)
215 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000216
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000217 finally:
218 # Repair TZ environment variable in case any other tests
219 # rely on it.
220 if org_TZ is not None:
221 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000222 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000223 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000224 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000225
Tim Peters1b6f7a92004-06-20 02:50:16 +0000226 def test_insane_timestamps(self):
227 # It's possible that some platform maps time_t to double,
228 # and that this test will fail there. This test should
229 # exempt such platforms (provided they return reasonable
230 # results!).
231 for func in time.ctime, time.gmtime, time.localtime:
232 for unreasonable in -1e200, 1e200:
233 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000234
Fred Drakef901abd2004-08-03 17:58:55 +0000235 def test_ctime_without_arg(self):
236 # Not sure how to check the values, since the clock could tick
237 # at any time. Make sure these are at least accepted and
238 # don't raise errors.
239 time.ctime()
240 time.ctime(None)
241
242 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243 gt0 = time.gmtime()
244 gt1 = time.gmtime(None)
245 t0 = time.mktime(gt0)
246 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000247 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000248
249 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250 lt0 = time.localtime()
251 lt1 = time.localtime(None)
252 t0 = time.mktime(lt0)
253 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000254 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000255
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000256class TestLocale(unittest.TestCase):
257 def setUp(self):
258 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000259
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000260 def tearDown(self):
261 locale.setlocale(locale.LC_ALL, self.oldloc)
262
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000263 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000264 try:
265 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
266 except locale.Error:
267 # skip this test
268 return
269 # This should not cause an exception
270 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
271
Victor Stinner73ea29c2011-01-08 01:56:31 +0000272
273class _BaseYearTest(unittest.TestCase):
274 accept2dyear = None
275
Alexander Belopolskya6867252011-01-05 23:00:47 +0000276 def setUp(self):
277 self.saved_accept2dyear = time.accept2dyear
278 time.accept2dyear = self.accept2dyear
279
280 def tearDown(self):
281 time.accept2dyear = self.saved_accept2dyear
282
283 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000284 raise NotImplementedError()
285
286class _TestAsctimeYear:
287 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000288 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000289
Victor Stinner73ea29c2011-01-08 01:56:31 +0000290 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000291 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000292 self.assertEqual(self.yearstr(12345), '12345')
293 self.assertEqual(self.yearstr(123456789), '123456789')
294
295class _TestStrftimeYear:
296 def yearstr(self, y):
297 return time.strftime('%Y', (y,) + (0,) * 8).split()[-1]
298
299 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000300 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000301 try:
Victor Stinner73691322011-01-08 02:00:24 +0000302 text = self.yearstr(12345)
303 except ValueError:
Victor Stinneraf5aee52011-01-08 02:46:33 +0000304 # strftime() is limited to [1; 9999] with Visual Studio
Victor Stinner301f1212011-01-08 03:06:52 +0000305 return
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100306 self.assertEqual(text, '12345')
307 self.assertEqual(self.yearstr(123456789), '123456789')
Victor Stinner73ea29c2011-01-08 01:56:31 +0000308
309class _Test2dYear(_BaseYearTest):
310 accept2dyear = 1
311
312 def test_year(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000313 with support.check_warnings():
314 self.assertEqual(self.yearstr(0), '2000')
315 self.assertEqual(self.yearstr(69), '1969')
316 self.assertEqual(self.yearstr(68), '2068')
317 self.assertEqual(self.yearstr(99), '1999')
Alexander Belopolskya6867252011-01-05 23:00:47 +0000318
319 def test_invalid(self):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000320 self.assertRaises(ValueError, self.yearstr, -1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000321 self.assertRaises(ValueError, self.yearstr, 100)
322 self.assertRaises(ValueError, self.yearstr, 999)
Alexander Belopolskya6867252011-01-05 23:00:47 +0000323
Victor Stinner73ea29c2011-01-08 01:56:31 +0000324class _Test4dYear(_BaseYearTest):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000325 accept2dyear = 0
Victor Stinner73ea29c2011-01-08 01:56:31 +0000326
327 def test_year(self):
Victor Stinneraf5aee52011-01-08 02:46:33 +0000328 self.assertIn(self.yearstr(1), ('1', '0001'))
329 self.assertIn(self.yearstr(68), ('68', '0068'))
330 self.assertIn(self.yearstr(69), ('69', '0069'))
331 self.assertIn(self.yearstr(99), ('99', '0099'))
332 self.assertIn(self.yearstr(999), ('999', '0999'))
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000333 self.assertEqual(self.yearstr(9999), '9999')
334
Victor Stinner301f1212011-01-08 03:06:52 +0000335 def test_negative(self):
336 try:
337 text = self.yearstr(-1)
338 except ValueError:
339 # strftime() is limited to [1; 9999] with Visual Studio
340 return
341 self.assertIn(text, ('-1', '-001'))
342
343 self.assertEqual(self.yearstr(-1234), '-1234')
344 self.assertEqual(self.yearstr(-123456), '-123456')
345
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000346
347 def test_mktime(self):
348 # Issue #1726687
349 for t in (-2, -1, 0, 1):
350 try:
351 tt = time.localtime(t)
352 except (OverflowError, ValueError):
353 pass
Alexander Belopolskya6892412011-01-11 02:22:16 +0000354 else:
355 self.assertEqual(time.mktime(tt), t)
Alexander Belopolsky31c5dd62011-01-11 01:35:22 +0000356 # It may not be possible to reliably make mktime return error
357 # on all platfom. This will make sure that no other exception
358 # than OverflowError is raised for an extreme value.
359 try:
360 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
361 except OverflowError:
362 pass
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000363
Victor Stinner73ea29c2011-01-08 01:56:31 +0000364class TestAsctimeAccept2dYear(_TestAsctimeYear, _Test2dYear):
365 pass
Alexander Belopolskya6867252011-01-05 23:00:47 +0000366
Victor Stinner73ea29c2011-01-08 01:56:31 +0000367class TestStrftimeAccept2dYear(_TestStrftimeYear, _Test2dYear):
368 pass
369
370class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
371 pass
372
373class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000374 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000375
376class Test2dyearBool(_TestAsctimeYear, _Test2dYear):
377 accept2dyear = True
378
379class Test4dyearBool(_TestAsctimeYear, _Test4dYear):
380 accept2dyear = False
381
382class TestAccept2YearBad(_TestAsctimeYear, _BaseYearTest):
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000383 class X:
384 def __bool__(self):
385 raise RuntimeError('boo')
386 accept2dyear = X()
387 def test_2dyear(self):
388 pass
389 def test_invalid(self):
390 self.assertRaises(RuntimeError, self.yearstr, 200)
391
392
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000393def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000394 support.run_unittest(
395 TimeTestCase,
396 TestLocale,
397 TestAsctimeAccept2dYear,
398 TestStrftimeAccept2dYear,
399 TestAsctime4dyear,
400 TestStrftime4dyear,
401 Test2dyearBool,
402 Test4dyearBool,
403 TestAccept2YearBad)
Fred Drake2e2be372001-09-20 21:33:42 +0000404
405if __name__ == "__main__":
406 test_main()