blob: 73c53b3f6c6592f0398c0170a4becf304b4aef37 [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
Victor Stinnere0be4232011-10-25 13:06:09 +020023 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
24 'need time.clock_gettime()')
25 def test_clock_realtime(self):
26 time.clock_gettime(time.CLOCK_REALTIME)
27
28 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
29 'need time.clock_gettime()')
30 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
31 'need time.CLOCK_MONOTONIC')
32 def test_clock_monotonic(self):
33 a = time.clock_gettime(time.CLOCK_MONOTONIC)
34 b = time.clock_gettime(time.CLOCK_MONOTONIC)
35 self.assertLessEqual(a, b)
36
37 @unittest.skipUnless(hasattr(time, 'clock_getres'),
38 'need time.clock_getres()')
39 def test_clock_getres(self):
40 res = time.clock_getres(time.CLOCK_REALTIME)
41 self.assertGreater(res, 0.0)
42 self.assertLessEqual(res, 1.0)
43
Fred Drakebc561982001-05-22 17:02:02 +000044 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000045 self.assertEqual(time.ctime(self.t),
46 time.asctime(time.localtime(self.t)))
47 self.assertEqual(int(time.mktime(time.localtime(self.t))),
48 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000049
50 def test_sleep(self):
Victor Stinner7f53a502011-07-05 22:00:25 +020051 self.assertRaises(ValueError, time.sleep, -2)
52 self.assertRaises(ValueError, time.sleep, -1)
Fred Drakebc561982001-05-22 17:02:02 +000053 time.sleep(1.2)
54
55 def test_strftime(self):
56 tt = time.gmtime(self.t)
57 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
58 'j', 'm', 'M', 'p', 'S',
59 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
60 format = ' %' + directive
61 try:
62 time.strftime(format, tt)
63 except ValueError:
64 self.fail('conversion specifier: %r failed.' % format)
65
Senthil Kumaran8f377a32011-04-06 12:54:06 +080066 # Issue #10762: Guard against invalid/non-supported format string
67 # so that Python don't crash (Windows crashes when the format string
68 # input to [w]strftime is not kosher.
69 if sys.platform.startswith('win'):
70 with self.assertRaises(ValueError):
71 time.strftime('%f')
72
Alexander Belopolsky38e29962010-10-01 14:18:49 +000073 def _bounds_checking(self, func=time.strftime):
Brett Cannond1080a32004-03-02 04:38:10 +000074 # Make sure that strftime() checks the bounds of the various parts
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075 #of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000076
Victor Stinner73ea29c2011-01-08 01:56:31 +000077 # The year field is tested by other test cases above
78
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079 # Check month [1, 12] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000080 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000082 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000083 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084 # Check day of month [1, 31] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +000085 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000087 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000088 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 # Check hour [0, 23]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000090 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000091 (1900, 1, 1, -1, 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, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094 # Check minute [0, 59]
Alexander Belopolsky38e29962010-10-01 14:18:49 +000095 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000096 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000097 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000098 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000100 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000101 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
102 # C99 only requires allowing for one leap second, but Python's docs say
103 # allow two leap seconds (0..61)
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000104 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000105 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
106 # No check for upper-bound day of week;
107 # value forced into range by a ``% 7`` calculation.
108 # Start check at -2 since gettmarg() increments value before taking
109 # modulo.
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000110 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000111 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112 # Check day of the year [1, 366] + zero support
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000113 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000114 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000115 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000116 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000117
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000118 def test_strftime_bounding_check(self):
119 self._bounds_checking(lambda tup: time.strftime('', tup))
120
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400122 # Make sure that using all zeros uses the proper default
123 # values. No test for daylight savings since strftime() does
124 # not change output based on its value and no test for year
125 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000126 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000127 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400128 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000129 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000130
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000131 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132 # Should be able to go round-trip from strftime to strptime without
133 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000134 tt = time.gmtime(self.t)
135 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
136 'j', 'm', 'M', 'p', 'S',
137 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000138 format = '%' + directive
139 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000140 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000142 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143 self.fail("conversion specifier %r failed with '%s' input." %
144 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000145
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000146 def test_strptime_bytes(self):
147 # Make sure only strings are accepted as arguments to strptime.
148 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
149 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
150
Fred Drakebc561982001-05-22 17:02:02 +0000151 def test_asctime(self):
152 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000153
154 # Max year is only limited by the size of C int.
Antoine Pitrou1ec121d2011-01-04 22:54:30 +0000155 sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000156 bigyear = (1 << 8 * sizeof_int - 1) - 1
157 asc = time.asctime((bigyear, 6, 1) + (0,)*6)
158 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
159 self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
Fred Drakebc561982001-05-22 17:02:02 +0000160 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000161 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000162 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000163
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000164 def test_asctime_bounding_check(self):
165 self._bounds_checking(time.asctime)
166
Georg Brandle10608c2011-01-02 22:33:43 +0000167 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000168 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
169 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
170 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
171 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000172 for year in [-100, 100, 1000, 2000, 10000]:
173 try:
174 testval = time.mktime((year, 1, 10) + (0,)*6)
175 except (ValueError, OverflowError):
176 # If mktime fails, ctime will fail too. This may happen
177 # on some platforms.
178 pass
179 else:
180 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000181
R. David Murray6ecf76e2010-12-14 01:22:50 +0000182 @unittest.skipIf(not hasattr(time, "tzset"),
183 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000184 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000185
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000186 from os import environ
187
Tim Peters0eadaac2003-04-24 16:02:54 +0000188 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000189 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000190 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000191
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000192 # These formats are correct for 2002, and possibly future years
193 # This format is the 'standard' as documented at:
194 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
195 # They are also documented in the tzset(3) man page on most Unix
196 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000197 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000198 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
199 utc='UTC+0'
200
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000201 org_TZ = environ.get('TZ',None)
202 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000203 # Make sure we can switch to UTC time and results are correct
204 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000205 # Note that altzone is undefined in UTC, as there is no DST
206 environ['TZ'] = eastern
207 time.tzset()
208 environ['TZ'] = utc
209 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000210 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000211 time.gmtime(xmas2002), time.localtime(xmas2002)
212 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000213 self.assertEqual(time.daylight, 0)
214 self.assertEqual(time.timezone, 0)
215 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000216
217 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000218 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000219 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000220 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
221 self.assertEqual(time.tzname, ('EST', 'EDT'))
222 self.assertEqual(len(time.tzname), 2)
223 self.assertEqual(time.daylight, 1)
224 self.assertEqual(time.timezone, 18000)
225 self.assertEqual(time.altzone, 14400)
226 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
227 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000228
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000229 # Now go to the southern hemisphere.
230 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000231 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000232 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
233 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
234 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
235 self.assertEqual(len(time.tzname), 2)
236 self.assertEqual(time.daylight, 1)
237 self.assertEqual(time.timezone, -36000)
238 self.assertEqual(time.altzone, -39600)
239 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000240
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000241 finally:
242 # Repair TZ environment variable in case any other tests
243 # rely on it.
244 if org_TZ is not None:
245 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000246 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000247 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000248 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000249
Tim Peters1b6f7a92004-06-20 02:50:16 +0000250 def test_insane_timestamps(self):
251 # It's possible that some platform maps time_t to double,
252 # and that this test will fail there. This test should
253 # exempt such platforms (provided they return reasonable
254 # results!).
255 for func in time.ctime, time.gmtime, time.localtime:
256 for unreasonable in -1e200, 1e200:
257 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000258
Fred Drakef901abd2004-08-03 17:58:55 +0000259 def test_ctime_without_arg(self):
260 # Not sure how to check the values, since the clock could tick
261 # at any time. Make sure these are at least accepted and
262 # don't raise errors.
263 time.ctime()
264 time.ctime(None)
265
266 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267 gt0 = time.gmtime()
268 gt1 = time.gmtime(None)
269 t0 = time.mktime(gt0)
270 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000271 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000272
273 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000274 lt0 = time.localtime()
275 lt1 = time.localtime(None)
276 t0 = time.mktime(lt0)
277 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000278 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000279
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000280class TestLocale(unittest.TestCase):
281 def setUp(self):
282 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000283
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000284 def tearDown(self):
285 locale.setlocale(locale.LC_ALL, self.oldloc)
286
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000287 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000288 try:
289 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
290 except locale.Error:
291 # skip this test
292 return
293 # This should not cause an exception
294 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
295
Victor Stinner73ea29c2011-01-08 01:56:31 +0000296
297class _BaseYearTest(unittest.TestCase):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000298 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000299 raise NotImplementedError()
300
301class _TestAsctimeYear:
302 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000303 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000304
Victor Stinner73ea29c2011-01-08 01:56:31 +0000305 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000306 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000307 self.assertEqual(self.yearstr(12345), '12345')
308 self.assertEqual(self.yearstr(123456789), '123456789')
309
310class _TestStrftimeYear:
311 def yearstr(self, y):
312 return time.strftime('%Y', (y,) + (0,) * 8).split()[-1]
313
314 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000315 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000316 try:
Victor Stinner73691322011-01-08 02:00:24 +0000317 text = self.yearstr(12345)
318 except ValueError:
Victor Stinneraf5aee52011-01-08 02:46:33 +0000319 # strftime() is limited to [1; 9999] with Visual Studio
Victor Stinner301f1212011-01-08 03:06:52 +0000320 return
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100321 self.assertEqual(text, '12345')
322 self.assertEqual(self.yearstr(123456789), '123456789')
Victor Stinner73ea29c2011-01-08 01:56:31 +0000323
Victor Stinner73ea29c2011-01-08 01:56:31 +0000324class _Test4dYear(_BaseYearTest):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000325
326 def test_year(self):
Victor Stinneraf5aee52011-01-08 02:46:33 +0000327 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'))
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000332 self.assertEqual(self.yearstr(9999), '9999')
333
Victor Stinner301f1212011-01-08 03:06:52 +0000334 def test_negative(self):
335 try:
336 text = self.yearstr(-1)
337 except ValueError:
338 # strftime() is limited to [1; 9999] with Visual Studio
339 return
340 self.assertIn(text, ('-1', '-001'))
341
342 self.assertEqual(self.yearstr(-1234), '-1234')
343 self.assertEqual(self.yearstr(-123456), '-123456')
344
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000345
346 def test_mktime(self):
347 # Issue #1726687
348 for t in (-2, -1, 0, 1):
349 try:
350 tt = time.localtime(t)
351 except (OverflowError, ValueError):
352 pass
Alexander Belopolskya6892412011-01-11 02:22:16 +0000353 else:
354 self.assertEqual(time.mktime(tt), t)
Alexander Belopolsky31c5dd62011-01-11 01:35:22 +0000355 # It may not be possible to reliably make mktime return error
356 # on all platfom. This will make sure that no other exception
357 # than OverflowError is raised for an extreme value.
358 try:
359 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
360 except OverflowError:
361 pass
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000362
Victor Stinner73ea29c2011-01-08 01:56:31 +0000363class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
364 pass
365
366class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000367 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000368
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000369
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000370def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000371 support.run_unittest(
372 TimeTestCase,
373 TestLocale,
Victor Stinner73ea29c2011-01-08 01:56:31 +0000374 TestAsctime4dyear,
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400375 TestStrftime4dyear)
Fred Drake2e2be372001-09-20 21:33:42 +0000376
377if __name__ == "__main__":
378 test_main()