blob: 9e2e68d4ef48ea1f9437aecc0dbdb73878cd1618 [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
Florent Xicluna49ce0682011-11-01 12:56:14 +010073 def _bounds_checking(self, func):
Brett Cannond1080a32004-03-02 04:38:10 +000074 # Make sure that strftime() checks the bounds of the various parts
Florent Xicluna49ce0682011-11-01 12:56:14 +010075 # 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
Florent Xicluna49ce0682011-11-01 12:56:14 +010080 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
81 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000082 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000084 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000085 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086 # Check day of month [1, 31] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +010087 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
88 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000089 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000091 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000092 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093 # Check hour [0, 23]
Florent Xicluna49ce0682011-11-01 12:56:14 +010094 func((1900, 1, 1, 0, 0, 0, 0, 1, -1))
95 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000096 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000097 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000098 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +000099 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100 # Check minute [0, 59]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100101 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000102 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000103 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000104 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000105 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000107 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000108 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
109 # C99 only requires allowing for one leap second, but Python's docs say
110 # allow two leap seconds (0..61)
Florent Xicluna49ce0682011-11-01 12:56:14 +0100111 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
112 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000113 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000114 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
115 # No check for upper-bound day of week;
116 # value forced into range by a ``% 7`` calculation.
117 # Start check at -2 since gettmarg() increments value before taking
118 # modulo.
Florent Xicluna49ce0682011-11-01 12:56:14 +0100119 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
120 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000121 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000122 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123 # Check day of the year [1, 366] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100124 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
125 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000126 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000128 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000129 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000130
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000131 def test_strftime_bounding_check(self):
132 self._bounds_checking(lambda tup: time.strftime('', tup))
133
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000134 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400135 # Make sure that using all zeros uses the proper default
136 # values. No test for daylight savings since strftime() does
137 # not change output based on its value and no test for year
138 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000140 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400141 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000142 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000144 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000145 # Should be able to go round-trip from strftime to strptime without
146 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000147 tt = time.gmtime(self.t)
148 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
149 'j', 'm', 'M', 'p', 'S',
150 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151 format = '%' + directive
152 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000153 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000154 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000155 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000156 self.fail("conversion specifier %r failed with '%s' input." %
157 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000158
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000159 def test_strptime_bytes(self):
160 # Make sure only strings are accepted as arguments to strptime.
161 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
162 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
163
Fred Drakebc561982001-05-22 17:02:02 +0000164 def test_asctime(self):
165 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000166
167 # Max year is only limited by the size of C int.
Antoine Pitrou1ec121d2011-01-04 22:54:30 +0000168 sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000169 bigyear = (1 << 8 * sizeof_int - 1) - 1
170 asc = time.asctime((bigyear, 6, 1) + (0,)*6)
171 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
172 self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
Fred Drakebc561982001-05-22 17:02:02 +0000173 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000174 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000175 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000176
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000177 def test_asctime_bounding_check(self):
178 self._bounds_checking(time.asctime)
179
Georg Brandle10608c2011-01-02 22:33:43 +0000180 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000181 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
182 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
183 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
184 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000185 for year in [-100, 100, 1000, 2000, 10000]:
186 try:
187 testval = time.mktime((year, 1, 10) + (0,)*6)
188 except (ValueError, OverflowError):
189 # If mktime fails, ctime will fail too. This may happen
190 # on some platforms.
191 pass
192 else:
193 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000194
R. David Murray6ecf76e2010-12-14 01:22:50 +0000195 @unittest.skipIf(not hasattr(time, "tzset"),
196 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000197 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000198
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000199 from os import environ
200
Tim Peters0eadaac2003-04-24 16:02:54 +0000201 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000202 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000203 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000204
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000205 # These formats are correct for 2002, and possibly future years
206 # This format is the 'standard' as documented at:
207 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
208 # They are also documented in the tzset(3) man page on most Unix
209 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000210 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000211 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
212 utc='UTC+0'
213
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000214 org_TZ = environ.get('TZ',None)
215 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000216 # Make sure we can switch to UTC time and results are correct
217 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000218 # Note that altzone is undefined in UTC, as there is no DST
219 environ['TZ'] = eastern
220 time.tzset()
221 environ['TZ'] = utc
222 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000223 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000224 time.gmtime(xmas2002), time.localtime(xmas2002)
225 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000226 self.assertEqual(time.daylight, 0)
227 self.assertEqual(time.timezone, 0)
228 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000229
230 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000231 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000232 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000233 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
234 self.assertEqual(time.tzname, ('EST', 'EDT'))
235 self.assertEqual(len(time.tzname), 2)
236 self.assertEqual(time.daylight, 1)
237 self.assertEqual(time.timezone, 18000)
238 self.assertEqual(time.altzone, 14400)
239 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
240 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000241
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000242 # Now go to the southern hemisphere.
243 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000244 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000245 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
246 self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
247 self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
248 self.assertEqual(len(time.tzname), 2)
249 self.assertEqual(time.daylight, 1)
250 self.assertEqual(time.timezone, -36000)
251 self.assertEqual(time.altzone, -39600)
252 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000253
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000254 finally:
255 # Repair TZ environment variable in case any other tests
256 # rely on it.
257 if org_TZ is not None:
258 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000259 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000260 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000261 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000262
Tim Peters1b6f7a92004-06-20 02:50:16 +0000263 def test_insane_timestamps(self):
264 # It's possible that some platform maps time_t to double,
265 # and that this test will fail there. This test should
266 # exempt such platforms (provided they return reasonable
267 # results!).
268 for func in time.ctime, time.gmtime, time.localtime:
269 for unreasonable in -1e200, 1e200:
270 self.assertRaises(ValueError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000271
Fred Drakef901abd2004-08-03 17:58:55 +0000272 def test_ctime_without_arg(self):
273 # Not sure how to check the values, since the clock could tick
274 # at any time. Make sure these are at least accepted and
275 # don't raise errors.
276 time.ctime()
277 time.ctime(None)
278
279 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280 gt0 = time.gmtime()
281 gt1 = time.gmtime(None)
282 t0 = time.mktime(gt0)
283 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000284 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000285
286 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 lt0 = time.localtime()
288 lt1 = time.localtime(None)
289 t0 = time.mktime(lt0)
290 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000291 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000292
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000293class TestLocale(unittest.TestCase):
294 def setUp(self):
295 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000296
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000297 def tearDown(self):
298 locale.setlocale(locale.LC_ALL, self.oldloc)
299
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000300 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000301 try:
302 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
303 except locale.Error:
304 # skip this test
305 return
306 # This should not cause an exception
307 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
308
Victor Stinner73ea29c2011-01-08 01:56:31 +0000309
310class _BaseYearTest(unittest.TestCase):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000311 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000312 raise NotImplementedError()
313
314class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100315 _format = '%d'
316
Victor Stinner73ea29c2011-01-08 01:56:31 +0000317 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000318 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000319
Victor Stinner73ea29c2011-01-08 01:56:31 +0000320 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000321 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000322 self.assertEqual(self.yearstr(12345), '12345')
323 self.assertEqual(self.yearstr(123456789), '123456789')
324
325class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100326
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
Victor Stinner73ea29c2011-01-08 01:56:31 +0000337 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100338 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
349class _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)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000361
362 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000363 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000364 try:
Victor Stinner73691322011-01-08 02:00:24 +0000365 text = self.yearstr(12345)
366 except ValueError:
Victor Stinneraf5aee52011-01-08 02:46:33 +0000367 # strftime() is limited to [1; 9999] with Visual Studio
Victor Stinner301f1212011-01-08 03:06:52 +0000368 return
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100369 self.assertEqual(text, '12345')
370 self.assertEqual(self.yearstr(123456789), '123456789')
Victor Stinner73ea29c2011-01-08 01:56:31 +0000371
Victor Stinner301f1212011-01-08 03:06:52 +0000372 def test_negative(self):
373 try:
374 text = self.yearstr(-1)
375 except ValueError:
376 # strftime() is limited to [1; 9999] with Visual Studio
377 return
Florent Xicluna49ce0682011-11-01 12:56:14 +0100378 self.assertEqual(text, self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000379
380 self.assertEqual(self.yearstr(-1234), '-1234')
381 self.assertEqual(self.yearstr(-123456), '-123456')
382
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000383
384 def test_mktime(self):
385 # Issue #1726687
386 for t in (-2, -1, 0, 1):
387 try:
388 tt = time.localtime(t)
389 except (OverflowError, ValueError):
390 pass
Alexander Belopolskya6892412011-01-11 02:22:16 +0000391 else:
392 self.assertEqual(time.mktime(tt), t)
Alexander Belopolsky31c5dd62011-01-11 01:35:22 +0000393 # It may not be possible to reliably make mktime return error
394 # on all platfom. This will make sure that no other exception
395 # than OverflowError is raised for an extreme value.
396 try:
397 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
398 except OverflowError:
399 pass
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000400
Victor Stinner73ea29c2011-01-08 01:56:31 +0000401class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
402 pass
403
404class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000405 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000406
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000407
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000408def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000409 support.run_unittest(
410 TimeTestCase,
411 TestLocale,
Victor Stinner73ea29c2011-01-08 01:56:31 +0000412 TestAsctime4dyear,
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400413 TestStrftime4dyear)
Fred Drake2e2be372001-09-20 21:33:42 +0000414
415if __name__ == "__main__":
416 test_main()