blob: fb2489c45325a2db2db7ba550d48a5ef0c62e350 [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
Victor Stinner4195b5c2012-02-08 23:03:19 +01004import locale
5import sysconfig
6import sys
7import platform
Barry Warsawb0c22321996-12-06 23:30:07 +00008
Florent Xiclunabceb5282011-11-01 14:11:34 +01009# Max year is only limited by the size of C int.
10SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
11TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
12TIME_MINYEAR = -TIME_MAXYEAR - 1
13
14
Fred Drakebc561982001-05-22 17:02:02 +000015class TimeTestCase(unittest.TestCase):
Barry Warsawb0c22321996-12-06 23:30:07 +000016
Fred Drakebc561982001-05-22 17:02:02 +000017 def setUp(self):
18 self.t = time.time()
Barry Warsawb0c22321996-12-06 23:30:07 +000019
Fred Drakebc561982001-05-22 17:02:02 +000020 def test_data_attributes(self):
21 time.altzone
22 time.daylight
23 time.timezone
24 time.tzname
Barry Warsawb0c22321996-12-06 23:30:07 +000025
Fred Drakebc561982001-05-22 17:02:02 +000026 def test_clock(self):
27 time.clock()
Barry Warsawb0c22321996-12-06 23:30:07 +000028
Victor Stinnere0be4232011-10-25 13:06:09 +020029 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
30 'need time.clock_gettime()')
31 def test_clock_realtime(self):
32 time.clock_gettime(time.CLOCK_REALTIME)
33
34 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
35 'need time.clock_gettime()')
36 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
37 'need time.CLOCK_MONOTONIC')
38 def test_clock_monotonic(self):
39 a = time.clock_gettime(time.CLOCK_MONOTONIC)
40 b = time.clock_gettime(time.CLOCK_MONOTONIC)
41 self.assertLessEqual(a, b)
42
43 @unittest.skipUnless(hasattr(time, 'clock_getres'),
44 'need time.clock_getres()')
45 def test_clock_getres(self):
46 res = time.clock_getres(time.CLOCK_REALTIME)
47 self.assertGreater(res, 0.0)
48 self.assertLessEqual(res, 1.0)
49
Victor Stinner30d79472012-04-03 00:45:07 +020050 @unittest.skipUnless(hasattr(time, 'clock_settime'),
51 'need time.clock_settime()')
52 def test_clock_settime(self):
53 t = time.clock_gettime(time.CLOCK_REALTIME)
54 try:
55 time.clock_settime(time.CLOCK_REALTIME, t)
56 except PermissionError:
57 pass
58
59 self.assertRaises(OSError, time.clock_settime, time.CLOCK_MONOTONIC, 0)
60
Fred Drakebc561982001-05-22 17:02:02 +000061 def test_conversions(self):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +000062 self.assertEqual(time.ctime(self.t),
63 time.asctime(time.localtime(self.t)))
64 self.assertEqual(int(time.mktime(time.localtime(self.t))),
65 int(self.t))
Fred Drakebc561982001-05-22 17:02:02 +000066
67 def test_sleep(self):
Victor Stinner7f53a502011-07-05 22:00:25 +020068 self.assertRaises(ValueError, time.sleep, -2)
69 self.assertRaises(ValueError, time.sleep, -1)
Fred Drakebc561982001-05-22 17:02:02 +000070 time.sleep(1.2)
71
72 def test_strftime(self):
73 tt = time.gmtime(self.t)
74 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
75 'j', 'm', 'M', 'p', 'S',
76 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
77 format = ' %' + directive
78 try:
79 time.strftime(format, tt)
80 except ValueError:
81 self.fail('conversion specifier: %r failed.' % format)
82
Senthil Kumaran8f377a32011-04-06 12:54:06 +080083 # Issue #10762: Guard against invalid/non-supported format string
84 # so that Python don't crash (Windows crashes when the format string
85 # input to [w]strftime is not kosher.
86 if sys.platform.startswith('win'):
87 with self.assertRaises(ValueError):
88 time.strftime('%f')
89
Florent Xicluna49ce0682011-11-01 12:56:14 +010090 def _bounds_checking(self, func):
Brett Cannond1080a32004-03-02 04:38:10 +000091 # Make sure that strftime() checks the bounds of the various parts
Florent Xicluna49ce0682011-11-01 12:56:14 +010092 # of the time tuple (0 is valid for *all* values).
Brett Cannond1080a32004-03-02 04:38:10 +000093
Victor Stinner73ea29c2011-01-08 01:56:31 +000094 # The year field is tested by other test cases above
95
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096 # Check month [1, 12] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +010097 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
98 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +000099 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000101 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000102 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000103 # Check day of month [1, 31] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100104 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
105 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000106 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000107 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000108 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000109 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110 # Check hour [0, 23]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100111 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000112 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000113 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000114 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000115 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000116 # Check minute [0, 59]
Florent Xicluna49ce0682011-11-01 12:56:14 +0100117 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000118 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000119 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000120 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000121 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000122 # Check second [0, 61]
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000123 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000124 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
125 # C99 only requires allowing for one leap second, but Python's docs say
126 # allow two leap seconds (0..61)
Florent Xicluna49ce0682011-11-01 12:56:14 +0100127 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
128 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000129 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000130 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
131 # No check for upper-bound day of week;
132 # value forced into range by a ``% 7`` calculation.
133 # Start check at -2 since gettmarg() increments value before taking
134 # modulo.
Florent Xicluna49ce0682011-11-01 12:56:14 +0100135 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
136 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000137 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000138 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 # Check day of the year [1, 366] + zero support
Florent Xicluna49ce0682011-11-01 12:56:14 +0100140 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
141 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000142 self.assertRaises(ValueError, func,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000144 self.assertRaises(ValueError, func,
Brett Cannond1080a32004-03-02 04:38:10 +0000145 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
Brett Cannond1080a32004-03-02 04:38:10 +0000146
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000147 def test_strftime_bounding_check(self):
148 self._bounds_checking(lambda tup: time.strftime('', tup))
149
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150 def test_default_values_for_zero(self):
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400151 # Make sure that using all zeros uses the proper default
152 # values. No test for daylight savings since strftime() does
153 # not change output based on its value and no test for year
154 # because systems vary in their support for year 0.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000155 expected = "2000 01 01 00 00 00 1 001"
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000156 with support.check_warnings():
Alexander Belopolsky03163ac2011-05-02 12:20:52 -0400157 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000158 self.assertEqual(expected, result)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000159
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000160 def test_strptime(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000161 # Should be able to go round-trip from strftime to strptime without
162 # throwing an exception.
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000163 tt = time.gmtime(self.t)
164 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
165 'j', 'm', 'M', 'p', 'S',
166 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167 format = '%' + directive
168 strf_output = time.strftime(format, tt)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000169 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170 time.strptime(strf_output, format)
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000171 except ValueError:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000172 self.fail("conversion specifier %r failed with '%s' input." %
173 (format, strf_output))
Guido van Rossum00efe7e2002-07-19 17:04:46 +0000174
Brett Cannon7f6b4f82009-03-30 21:30:26 +0000175 def test_strptime_bytes(self):
176 # Make sure only strings are accepted as arguments to strptime.
177 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
178 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
179
Fred Drakebc561982001-05-22 17:02:02 +0000180 def test_asctime(self):
181 time.asctime(time.gmtime(self.t))
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000182
183 # Max year is only limited by the size of C int.
Florent Xiclunabceb5282011-11-01 14:11:34 +0100184 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
185 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
186 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
187 self.assertRaises(OverflowError, time.asctime,
188 (TIME_MAXYEAR + 1,) + (0,) * 8)
189 self.assertRaises(OverflowError, time.asctime,
190 (TIME_MINYEAR - 1,) + (0,) * 8)
Fred Drakebc561982001-05-22 17:02:02 +0000191 self.assertRaises(TypeError, time.asctime, 0)
Alexander Belopolskye2dc0822011-01-02 20:48:22 +0000192 self.assertRaises(TypeError, time.asctime, ())
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000193 self.assertRaises(TypeError, time.asctime, (0,) * 10)
Fred Drakebc561982001-05-22 17:02:02 +0000194
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000195 def test_asctime_bounding_check(self):
196 self._bounds_checking(time.asctime)
197
Georg Brandle10608c2011-01-02 22:33:43 +0000198 def test_ctime(self):
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000199 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
200 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
201 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
202 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000203 for year in [-100, 100, 1000, 2000, 10000]:
204 try:
205 testval = time.mktime((year, 1, 10) + (0,)*6)
206 except (ValueError, OverflowError):
207 # If mktime fails, ctime will fail too. This may happen
208 # on some platforms.
209 pass
210 else:
211 self.assertEqual(time.ctime(testval)[20:], str(year))
Georg Brandle10608c2011-01-02 22:33:43 +0000212
Florent Xiclunae54371e2011-11-11 18:59:30 +0100213 @unittest.skipUnless(hasattr(time, "tzset"),
214 "time module has no attribute tzset")
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000215 def test_tzset(self):
Guido van Rossumd2b738e2003-03-15 12:01:52 +0000216
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000217 from os import environ
218
Tim Peters0eadaac2003-04-24 16:02:54 +0000219 # Epoch time of midnight Dec 25th 2002. Never DST in northern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000220 # hemisphere.
Tim Peters0eadaac2003-04-24 16:02:54 +0000221 xmas2002 = 1040774400.0
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000222
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000223 # These formats are correct for 2002, and possibly future years
224 # This format is the 'standard' as documented at:
225 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
226 # They are also documented in the tzset(3) man page on most Unix
227 # systems.
Tim Peters0eadaac2003-04-24 16:02:54 +0000228 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000229 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
230 utc='UTC+0'
231
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000232 org_TZ = environ.get('TZ',None)
233 try:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000234 # Make sure we can switch to UTC time and results are correct
235 # Note that unknown timezones default to UTC.
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000236 # Note that altzone is undefined in UTC, as there is no DST
237 environ['TZ'] = eastern
238 time.tzset()
239 environ['TZ'] = utc
240 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000241 self.assertEqual(
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000242 time.gmtime(xmas2002), time.localtime(xmas2002)
243 )
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000244 self.assertEqual(time.daylight, 0)
245 self.assertEqual(time.timezone, 0)
246 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000247
248 # Make sure we can switch to US/Eastern
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000249 environ['TZ'] = eastern
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000250 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000251 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
252 self.assertEqual(time.tzname, ('EST', 'EDT'))
253 self.assertEqual(len(time.tzname), 2)
254 self.assertEqual(time.daylight, 1)
255 self.assertEqual(time.timezone, 18000)
256 self.assertEqual(time.altzone, 14400)
257 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
258 self.assertEqual(len(time.tzname), 2)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000259
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000260 # Now go to the southern hemisphere.
261 environ['TZ'] = victoria
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000262 time.tzset()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000263 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
Victor Stinner0cd47902011-12-08 00:32:51 +0100264
265 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100266 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST"
267 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone),
268 # on some operating systems (e.g. FreeBSD), which is wrong. See for
269 # example this bug:
270 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
Victor Stinner0cd47902011-12-08 00:32:51 +0100271 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
Victor Stinner10a6ddb2011-12-10 14:37:53 +0100272 self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1]))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000273 self.assertEqual(len(time.tzname), 2)
274 self.assertEqual(time.daylight, 1)
275 self.assertEqual(time.timezone, -36000)
276 self.assertEqual(time.altzone, -39600)
277 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000278
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000279 finally:
280 # Repair TZ environment variable in case any other tests
281 # rely on it.
282 if org_TZ is not None:
283 environ['TZ'] = org_TZ
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000284 elif 'TZ' in environ:
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000285 del environ['TZ']
Neal Norwitz7f2588c2003-04-11 15:35:53 +0000286 time.tzset()
Tim Peters0eadaac2003-04-24 16:02:54 +0000287
Tim Peters1b6f7a92004-06-20 02:50:16 +0000288 def test_insane_timestamps(self):
289 # It's possible that some platform maps time_t to double,
290 # and that this test will fail there. This test should
291 # exempt such platforms (provided they return reasonable
292 # results!).
293 for func in time.ctime, time.gmtime, time.localtime:
294 for unreasonable in -1e200, 1e200:
Victor Stinner5d272cc2012-03-13 13:35:55 +0100295 self.assertRaises(OverflowError, func, unreasonable)
Fred Drakebc561982001-05-22 17:02:02 +0000296
Fred Drakef901abd2004-08-03 17:58:55 +0000297 def test_ctime_without_arg(self):
298 # Not sure how to check the values, since the clock could tick
299 # at any time. Make sure these are at least accepted and
300 # don't raise errors.
301 time.ctime()
302 time.ctime(None)
303
304 def test_gmtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305 gt0 = time.gmtime()
306 gt1 = time.gmtime(None)
307 t0 = time.mktime(gt0)
308 t1 = time.mktime(gt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000309 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000310
311 def test_localtime_without_arg(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000312 lt0 = time.localtime()
313 lt1 = time.localtime(None)
314 t0 = time.mktime(lt0)
315 t1 = time.mktime(lt1)
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000316 self.assertAlmostEqual(t1, t0, delta=0.2)
Fred Drakef901abd2004-08-03 17:58:55 +0000317
Florent Xiclunae54371e2011-11-11 18:59:30 +0100318 def test_mktime(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100319 # Issue #1726687
320 for t in (-2, -1, 0, 1):
321 try:
322 tt = time.localtime(t)
Victor Stinner2cbae982012-01-27 00:50:33 +0100323 except (OverflowError, OSError):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100324 pass
325 else:
326 self.assertEqual(time.mktime(tt), t)
Florent Xiclunae54371e2011-11-11 18:59:30 +0100327
328 # Issue #13309: passing extreme values to mktime() or localtime()
329 # borks the glibc's internal timezone data.
330 @unittest.skipUnless(platform.libc_ver()[0] != 'glibc',
331 "disabled because of a bug in glibc. Issue #13309")
332 def test_mktime_error(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100333 # It may not be possible to reliably make mktime return error
334 # on all platfom. This will make sure that no other exception
335 # than OverflowError is raised for an extreme value.
Florent Xiclunae54371e2011-11-11 18:59:30 +0100336 tt = time.gmtime(self.t)
337 tzname = time.strftime('%Z', tt)
338 self.assertNotEqual(tzname, 'LMT')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100339 try:
340 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
341 except OverflowError:
342 pass
Florent Xiclunae54371e2011-11-11 18:59:30 +0100343 self.assertEqual(time.strftime('%Z', tt), tzname)
Florent Xiclunabceb5282011-11-01 14:11:34 +0100344
Victor Stinnerec919cc2012-03-15 00:58:32 +0100345 def test_steady(self):
346 t1 = time.steady()
Victor Stinner8b302012012-02-07 23:29:46 +0100347 time.sleep(0.1)
Victor Stinnerec919cc2012-03-15 00:58:32 +0100348 t2 = time.steady()
Victor Stinner2dd254d2012-01-20 02:24:18 +0100349 dt = t2 - t1
Victor Stinner8b302012012-02-07 23:29:46 +0100350 # may fail if the system clock was changed
351 self.assertGreater(t2, t1)
Victor Stinner2dd254d2012-01-20 02:24:18 +0100352 self.assertAlmostEqual(dt, 0.1, delta=0.2)
Antoine Pitrou391166f2012-01-18 22:35:21 +0100353
Victor Stinner071eca32012-03-15 01:17:09 +0100354 def test_steady_strict(self):
355 try:
356 t1 = time.steady(strict=True)
357 except OSError as err:
358 self.skipTest("the monotonic clock failed: %s" % err)
359 except NotImplementedError:
360 self.skipTest("no monotonic clock available")
361 t2 = time.steady(strict=True)
362 self.assertGreaterEqual(t2, t1)
363
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100364 def test_localtime_failure(self):
365 # Issue #13847: check for localtime() failure
Victor Stinner53d36452012-01-27 01:03:25 +0100366 invalid_time_t = None
367 for time_t in (-1, 2**30, 2**33, 2**60):
368 try:
369 time.localtime(time_t)
Victor Stinner5d272cc2012-03-13 13:35:55 +0100370 except OverflowError:
371 self.skipTest("need 64-bit time_t")
Victor Stinner53d36452012-01-27 01:03:25 +0100372 except OSError:
373 invalid_time_t = time_t
374 break
375 if invalid_time_t is None:
376 self.skipTest("unable to find an invalid time_t value")
377
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100378 self.assertRaises(OSError, time.localtime, invalid_time_t)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100379 self.assertRaises(OSError, time.ctime, invalid_time_t)
Victor Stinnerb94b2662012-01-18 01:50:21 +0100380
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000381class TestLocale(unittest.TestCase):
382 def setUp(self):
383 self.oldloc = locale.setlocale(locale.LC_ALL)
Fred Drake2e2be372001-09-20 21:33:42 +0000384
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000385 def tearDown(self):
386 locale.setlocale(locale.LC_ALL, self.oldloc)
387
Martin v. Löwisa6a9c4d2009-05-30 06:15:30 +0000388 def test_bug_3061(self):
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000389 try:
390 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
391 except locale.Error:
392 # skip this test
393 return
394 # This should not cause an exception
395 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
396
Victor Stinner73ea29c2011-01-08 01:56:31 +0000397
398class _BaseYearTest(unittest.TestCase):
Alexander Belopolskya6867252011-01-05 23:00:47 +0000399 def yearstr(self, y):
Victor Stinner73ea29c2011-01-08 01:56:31 +0000400 raise NotImplementedError()
401
402class _TestAsctimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100403 _format = '%d'
404
Victor Stinner73ea29c2011-01-08 01:56:31 +0000405 def yearstr(self, y):
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000406 return time.asctime((y,) + (0,) * 8).split()[-1]
Alexander Belopolskya6867252011-01-05 23:00:47 +0000407
Victor Stinner73ea29c2011-01-08 01:56:31 +0000408 def test_large_year(self):
Victor Stinner73691322011-01-08 02:00:24 +0000409 # Check that it doesn't crash for year > 9999
Victor Stinner73ea29c2011-01-08 01:56:31 +0000410 self.assertEqual(self.yearstr(12345), '12345')
411 self.assertEqual(self.yearstr(123456789), '123456789')
412
413class _TestStrftimeYear:
Florent Xicluna49ce0682011-11-01 12:56:14 +0100414
415 # Issue 13305: For years < 1000, the value is not always
416 # padded to 4 digits across platforms. The C standard
417 # assumes year >= 1900, so it does not specify the number
418 # of digits.
419
420 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
421 _format = '%04d'
422 else:
423 _format = '%d'
424
Victor Stinner73ea29c2011-01-08 01:56:31 +0000425 def yearstr(self, y):
Florent Xicluna49ce0682011-11-01 12:56:14 +0100426 return time.strftime('%Y', (y,) + (0,) * 8)
427
428 def test_4dyear(self):
429 # Check that we can return the zero padded value.
430 if self._format == '%04d':
431 self.test_year('%04d')
432 else:
433 def year4d(y):
434 return time.strftime('%4Y', (y,) + (0,) * 8)
435 self.test_year('%04d', func=year4d)
436
Florent Xiclunabceb5282011-11-01 14:11:34 +0100437 def skip_if_not_supported(y):
438 msg = "strftime() is limited to [1; 9999] with Visual Studio"
439 # Check that it doesn't crash for year > 9999
440 try:
441 time.strftime('%Y', (y,) + (0,) * 8)
442 except ValueError:
443 cond = False
444 else:
445 cond = True
446 return unittest.skipUnless(cond, msg)
447
448 @skip_if_not_supported(10000)
449 def test_large_year(self):
450 return super().test_large_year()
451
452 @skip_if_not_supported(0)
453 def test_negative(self):
454 return super().test_negative()
455
456 del skip_if_not_supported
457
458
Florent Xicluna49ce0682011-11-01 12:56:14 +0100459class _Test4dYear(_BaseYearTest):
460 _format = '%d'
461
462 def test_year(self, fmt=None, func=None):
463 fmt = fmt or self._format
464 func = func or self.yearstr
465 self.assertEqual(func(1), fmt % 1)
466 self.assertEqual(func(68), fmt % 68)
467 self.assertEqual(func(69), fmt % 69)
468 self.assertEqual(func(99), fmt % 99)
469 self.assertEqual(func(999), fmt % 999)
470 self.assertEqual(func(9999), fmt % 9999)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000471
472 def test_large_year(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100473 self.assertEqual(self.yearstr(12345), '12345')
Victor Stinner13ed2ea2011-03-21 02:11:01 +0100474 self.assertEqual(self.yearstr(123456789), '123456789')
Florent Xiclunabceb5282011-11-01 14:11:34 +0100475 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
476 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000477
Victor Stinner301f1212011-01-08 03:06:52 +0000478 def test_negative(self):
Florent Xiclunabceb5282011-11-01 14:11:34 +0100479 self.assertEqual(self.yearstr(-1), self._format % -1)
Victor Stinner301f1212011-01-08 03:06:52 +0000480 self.assertEqual(self.yearstr(-1234), '-1234')
481 self.assertEqual(self.yearstr(-123456), '-123456')
Florent Xiclunad1bd7f72011-11-01 23:42:05 +0100482 self.assertEqual(self.yearstr(-123456789), str(-123456789))
483 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
Florent Xicluna2fbc1852011-11-02 08:13:43 +0100484 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
485 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
486 # Skip the value test, but check that no error is raised
487 self.yearstr(TIME_MINYEAR)
Florent Xiclunae2a732e2011-11-02 01:28:17 +0100488 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
Florent Xiclunabceb5282011-11-01 14:11:34 +0100489 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
Victor Stinner301f1212011-01-08 03:06:52 +0000490
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000491
Victor Stinner73ea29c2011-01-08 01:56:31 +0000492class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
493 pass
494
495class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
Victor Stinner301f1212011-01-08 03:06:52 +0000496 pass
Victor Stinner73ea29c2011-01-08 01:56:31 +0000497
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000498
Victor Stinner643cd682012-03-02 22:54:03 +0100499class TestPytime(unittest.TestCase):
Victor Stinner5d272cc2012-03-13 13:35:55 +0100500 def setUp(self):
501 self.invalid_values = (
502 -(2 ** 100), 2 ** 100,
503 -(2.0 ** 100.0), 2.0 ** 100.0,
504 )
505
506 def test_time_t(self):
507 from _testcapi import pytime_object_to_time_t
508 for obj, time_t in (
509 (0, 0),
510 (-1, -1),
511 (-1.0, -1),
512 (-1.9, -1),
513 (1.0, 1),
514 (1.9, 1),
515 ):
516 self.assertEqual(pytime_object_to_time_t(obj), time_t)
517
518 for invalid in self.invalid_values:
519 self.assertRaises(OverflowError, pytime_object_to_time_t, invalid)
520
521 def test_timeval(self):
522 from _testcapi import pytime_object_to_timeval
523 for obj, timeval in (
524 (0, (0, 0)),
525 (-1, (-1, 0)),
526 (-1.0, (-1, 0)),
527 (1e-6, (0, 1)),
528 (-1e-6, (-1, 999999)),
529 (-1.2, (-2, 800000)),
530 (1.1234560, (1, 123456)),
531 (1.1234569, (1, 123456)),
532 (-1.1234560, (-2, 876544)),
533 (-1.1234561, (-2, 876543)),
534 ):
535 self.assertEqual(pytime_object_to_timeval(obj), timeval)
536
537 for invalid in self.invalid_values:
538 self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)
539
Victor Stinner643cd682012-03-02 22:54:03 +0100540 def test_timespec(self):
541 from _testcapi import pytime_object_to_timespec
542 for obj, timespec in (
543 (0, (0, 0)),
544 (-1, (-1, 0)),
545 (-1.0, (-1, 0)),
Victor Stinner5d272cc2012-03-13 13:35:55 +0100546 (1e-9, (0, 1)),
Victor Stinner643cd682012-03-02 22:54:03 +0100547 (-1e-9, (-1, 999999999)),
548 (-1.2, (-2, 800000000)),
Victor Stinner5d272cc2012-03-13 13:35:55 +0100549 (1.1234567890, (1, 123456789)),
550 (1.1234567899, (1, 123456789)),
551 (-1.1234567890, (-2, 876543211)),
552 (-1.1234567891, (-2, 876543210)),
Victor Stinner643cd682012-03-02 22:54:03 +0100553 ):
554 self.assertEqual(pytime_object_to_timespec(obj), timespec)
555
Victor Stinner5d272cc2012-03-13 13:35:55 +0100556 for invalid in self.invalid_values:
Victor Stinner643cd682012-03-02 22:54:03 +0100557 self.assertRaises(OverflowError, pytime_object_to_timespec, invalid)
558
559
560
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000561def test_main():
Victor Stinner73ea29c2011-01-08 01:56:31 +0000562 support.run_unittest(
563 TimeTestCase,
564 TestLocale,
Victor Stinner73ea29c2011-01-08 01:56:31 +0000565 TestAsctime4dyear,
Victor Stinner643cd682012-03-02 22:54:03 +0100566 TestStrftime4dyear,
567 TestPytime)
Fred Drake2e2be372001-09-20 21:33:42 +0000568
569if __name__ == "__main__":
570 test_main()