1 | n/a | from test import support |
---|
2 | n/a | import decimal |
---|
3 | n/a | import enum |
---|
4 | n/a | import locale |
---|
5 | n/a | import math |
---|
6 | n/a | import platform |
---|
7 | n/a | import sys |
---|
8 | n/a | import sysconfig |
---|
9 | n/a | import time |
---|
10 | n/a | import unittest |
---|
11 | n/a | try: |
---|
12 | n/a | import threading |
---|
13 | n/a | except ImportError: |
---|
14 | n/a | threading = None |
---|
15 | n/a | try: |
---|
16 | n/a | import _testcapi |
---|
17 | n/a | except ImportError: |
---|
18 | n/a | _testcapi = None |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | # Max year is only limited by the size of C int. |
---|
22 | n/a | SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4 |
---|
23 | n/a | TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1 |
---|
24 | n/a | TIME_MINYEAR = -TIME_MAXYEAR - 1 |
---|
25 | n/a | |
---|
26 | n/a | SEC_TO_US = 10 ** 6 |
---|
27 | n/a | US_TO_NS = 10 ** 3 |
---|
28 | n/a | MS_TO_NS = 10 ** 6 |
---|
29 | n/a | SEC_TO_NS = 10 ** 9 |
---|
30 | n/a | NS_TO_SEC = 10 ** 9 |
---|
31 | n/a | |
---|
32 | n/a | class _PyTime(enum.IntEnum): |
---|
33 | n/a | # Round towards minus infinity (-inf) |
---|
34 | n/a | ROUND_FLOOR = 0 |
---|
35 | n/a | # Round towards infinity (+inf) |
---|
36 | n/a | ROUND_CEILING = 1 |
---|
37 | n/a | # Round to nearest with ties going to nearest even integer |
---|
38 | n/a | ROUND_HALF_EVEN = 2 |
---|
39 | n/a | |
---|
40 | n/a | # Rounding modes supported by PyTime |
---|
41 | n/a | ROUNDING_MODES = ( |
---|
42 | n/a | # (PyTime rounding method, decimal rounding method) |
---|
43 | n/a | (_PyTime.ROUND_FLOOR, decimal.ROUND_FLOOR), |
---|
44 | n/a | (_PyTime.ROUND_CEILING, decimal.ROUND_CEILING), |
---|
45 | n/a | (_PyTime.ROUND_HALF_EVEN, decimal.ROUND_HALF_EVEN), |
---|
46 | n/a | ) |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | class TimeTestCase(unittest.TestCase): |
---|
50 | n/a | |
---|
51 | n/a | def setUp(self): |
---|
52 | n/a | self.t = time.time() |
---|
53 | n/a | |
---|
54 | n/a | def test_data_attributes(self): |
---|
55 | n/a | time.altzone |
---|
56 | n/a | time.daylight |
---|
57 | n/a | time.timezone |
---|
58 | n/a | time.tzname |
---|
59 | n/a | |
---|
60 | n/a | def test_time(self): |
---|
61 | n/a | time.time() |
---|
62 | n/a | info = time.get_clock_info('time') |
---|
63 | n/a | self.assertFalse(info.monotonic) |
---|
64 | n/a | self.assertTrue(info.adjustable) |
---|
65 | n/a | |
---|
66 | n/a | def test_clock(self): |
---|
67 | n/a | time.clock() |
---|
68 | n/a | |
---|
69 | n/a | info = time.get_clock_info('clock') |
---|
70 | n/a | self.assertTrue(info.monotonic) |
---|
71 | n/a | self.assertFalse(info.adjustable) |
---|
72 | n/a | |
---|
73 | n/a | @unittest.skipUnless(hasattr(time, 'clock_gettime'), |
---|
74 | n/a | 'need time.clock_gettime()') |
---|
75 | n/a | def test_clock_realtime(self): |
---|
76 | n/a | time.clock_gettime(time.CLOCK_REALTIME) |
---|
77 | n/a | |
---|
78 | n/a | @unittest.skipUnless(hasattr(time, 'clock_gettime'), |
---|
79 | n/a | 'need time.clock_gettime()') |
---|
80 | n/a | @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'), |
---|
81 | n/a | 'need time.CLOCK_MONOTONIC') |
---|
82 | n/a | def test_clock_monotonic(self): |
---|
83 | n/a | a = time.clock_gettime(time.CLOCK_MONOTONIC) |
---|
84 | n/a | b = time.clock_gettime(time.CLOCK_MONOTONIC) |
---|
85 | n/a | self.assertLessEqual(a, b) |
---|
86 | n/a | |
---|
87 | n/a | @unittest.skipUnless(hasattr(time, 'clock_getres'), |
---|
88 | n/a | 'need time.clock_getres()') |
---|
89 | n/a | def test_clock_getres(self): |
---|
90 | n/a | res = time.clock_getres(time.CLOCK_REALTIME) |
---|
91 | n/a | self.assertGreater(res, 0.0) |
---|
92 | n/a | self.assertLessEqual(res, 1.0) |
---|
93 | n/a | |
---|
94 | n/a | @unittest.skipUnless(hasattr(time, 'clock_settime'), |
---|
95 | n/a | 'need time.clock_settime()') |
---|
96 | n/a | def test_clock_settime(self): |
---|
97 | n/a | t = time.clock_gettime(time.CLOCK_REALTIME) |
---|
98 | n/a | try: |
---|
99 | n/a | time.clock_settime(time.CLOCK_REALTIME, t) |
---|
100 | n/a | except PermissionError: |
---|
101 | n/a | pass |
---|
102 | n/a | |
---|
103 | n/a | if hasattr(time, 'CLOCK_MONOTONIC'): |
---|
104 | n/a | self.assertRaises(OSError, |
---|
105 | n/a | time.clock_settime, time.CLOCK_MONOTONIC, 0) |
---|
106 | n/a | |
---|
107 | n/a | def test_conversions(self): |
---|
108 | n/a | self.assertEqual(time.ctime(self.t), |
---|
109 | n/a | time.asctime(time.localtime(self.t))) |
---|
110 | n/a | self.assertEqual(int(time.mktime(time.localtime(self.t))), |
---|
111 | n/a | int(self.t)) |
---|
112 | n/a | |
---|
113 | n/a | def test_sleep(self): |
---|
114 | n/a | self.assertRaises(ValueError, time.sleep, -2) |
---|
115 | n/a | self.assertRaises(ValueError, time.sleep, -1) |
---|
116 | n/a | time.sleep(1.2) |
---|
117 | n/a | |
---|
118 | n/a | def test_strftime(self): |
---|
119 | n/a | tt = time.gmtime(self.t) |
---|
120 | n/a | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
---|
121 | n/a | 'j', 'm', 'M', 'p', 'S', |
---|
122 | n/a | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
---|
123 | n/a | format = ' %' + directive |
---|
124 | n/a | try: |
---|
125 | n/a | time.strftime(format, tt) |
---|
126 | n/a | except ValueError: |
---|
127 | n/a | self.fail('conversion specifier: %r failed.' % format) |
---|
128 | n/a | |
---|
129 | n/a | def _bounds_checking(self, func): |
---|
130 | n/a | # Make sure that strftime() checks the bounds of the various parts |
---|
131 | n/a | # of the time tuple (0 is valid for *all* values). |
---|
132 | n/a | |
---|
133 | n/a | # The year field is tested by other test cases above |
---|
134 | n/a | |
---|
135 | n/a | # Check month [1, 12] + zero support |
---|
136 | n/a | func((1900, 0, 1, 0, 0, 0, 0, 1, -1)) |
---|
137 | n/a | func((1900, 12, 1, 0, 0, 0, 0, 1, -1)) |
---|
138 | n/a | self.assertRaises(ValueError, func, |
---|
139 | n/a | (1900, -1, 1, 0, 0, 0, 0, 1, -1)) |
---|
140 | n/a | self.assertRaises(ValueError, func, |
---|
141 | n/a | (1900, 13, 1, 0, 0, 0, 0, 1, -1)) |
---|
142 | n/a | # Check day of month [1, 31] + zero support |
---|
143 | n/a | func((1900, 1, 0, 0, 0, 0, 0, 1, -1)) |
---|
144 | n/a | func((1900, 1, 31, 0, 0, 0, 0, 1, -1)) |
---|
145 | n/a | self.assertRaises(ValueError, func, |
---|
146 | n/a | (1900, 1, -1, 0, 0, 0, 0, 1, -1)) |
---|
147 | n/a | self.assertRaises(ValueError, func, |
---|
148 | n/a | (1900, 1, 32, 0, 0, 0, 0, 1, -1)) |
---|
149 | n/a | # Check hour [0, 23] |
---|
150 | n/a | func((1900, 1, 1, 23, 0, 0, 0, 1, -1)) |
---|
151 | n/a | self.assertRaises(ValueError, func, |
---|
152 | n/a | (1900, 1, 1, -1, 0, 0, 0, 1, -1)) |
---|
153 | n/a | self.assertRaises(ValueError, func, |
---|
154 | n/a | (1900, 1, 1, 24, 0, 0, 0, 1, -1)) |
---|
155 | n/a | # Check minute [0, 59] |
---|
156 | n/a | func((1900, 1, 1, 0, 59, 0, 0, 1, -1)) |
---|
157 | n/a | self.assertRaises(ValueError, func, |
---|
158 | n/a | (1900, 1, 1, 0, -1, 0, 0, 1, -1)) |
---|
159 | n/a | self.assertRaises(ValueError, func, |
---|
160 | n/a | (1900, 1, 1, 0, 60, 0, 0, 1, -1)) |
---|
161 | n/a | # Check second [0, 61] |
---|
162 | n/a | self.assertRaises(ValueError, func, |
---|
163 | n/a | (1900, 1, 1, 0, 0, -1, 0, 1, -1)) |
---|
164 | n/a | # C99 only requires allowing for one leap second, but Python's docs say |
---|
165 | n/a | # allow two leap seconds (0..61) |
---|
166 | n/a | func((1900, 1, 1, 0, 0, 60, 0, 1, -1)) |
---|
167 | n/a | func((1900, 1, 1, 0, 0, 61, 0, 1, -1)) |
---|
168 | n/a | self.assertRaises(ValueError, func, |
---|
169 | n/a | (1900, 1, 1, 0, 0, 62, 0, 1, -1)) |
---|
170 | n/a | # No check for upper-bound day of week; |
---|
171 | n/a | # value forced into range by a ``% 7`` calculation. |
---|
172 | n/a | # Start check at -2 since gettmarg() increments value before taking |
---|
173 | n/a | # modulo. |
---|
174 | n/a | self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)), |
---|
175 | n/a | func((1900, 1, 1, 0, 0, 0, +6, 1, -1))) |
---|
176 | n/a | self.assertRaises(ValueError, func, |
---|
177 | n/a | (1900, 1, 1, 0, 0, 0, -2, 1, -1)) |
---|
178 | n/a | # Check day of the year [1, 366] + zero support |
---|
179 | n/a | func((1900, 1, 1, 0, 0, 0, 0, 0, -1)) |
---|
180 | n/a | func((1900, 1, 1, 0, 0, 0, 0, 366, -1)) |
---|
181 | n/a | self.assertRaises(ValueError, func, |
---|
182 | n/a | (1900, 1, 1, 0, 0, 0, 0, -1, -1)) |
---|
183 | n/a | self.assertRaises(ValueError, func, |
---|
184 | n/a | (1900, 1, 1, 0, 0, 0, 0, 367, -1)) |
---|
185 | n/a | |
---|
186 | n/a | def test_strftime_bounding_check(self): |
---|
187 | n/a | self._bounds_checking(lambda tup: time.strftime('', tup)) |
---|
188 | n/a | |
---|
189 | n/a | def test_strftime_format_check(self): |
---|
190 | n/a | # Test that strftime does not crash on invalid format strings |
---|
191 | n/a | # that may trigger a buffer overread. When not triggered, |
---|
192 | n/a | # strftime may succeed or raise ValueError depending on |
---|
193 | n/a | # the platform. |
---|
194 | n/a | for x in [ '', 'A', '%A', '%AA' ]: |
---|
195 | n/a | for y in range(0x0, 0x10): |
---|
196 | n/a | for z in [ '%', 'A%', 'AA%', '%A%', 'A%A%', '%#' ]: |
---|
197 | n/a | try: |
---|
198 | n/a | time.strftime(x * y + z) |
---|
199 | n/a | except ValueError: |
---|
200 | n/a | pass |
---|
201 | n/a | |
---|
202 | n/a | def test_default_values_for_zero(self): |
---|
203 | n/a | # Make sure that using all zeros uses the proper default |
---|
204 | n/a | # values. No test for daylight savings since strftime() does |
---|
205 | n/a | # not change output based on its value and no test for year |
---|
206 | n/a | # because systems vary in their support for year 0. |
---|
207 | n/a | expected = "2000 01 01 00 00 00 1 001" |
---|
208 | n/a | with support.check_warnings(): |
---|
209 | n/a | result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8) |
---|
210 | n/a | self.assertEqual(expected, result) |
---|
211 | n/a | |
---|
212 | n/a | def test_strptime(self): |
---|
213 | n/a | # Should be able to go round-trip from strftime to strptime without |
---|
214 | n/a | # raising an exception. |
---|
215 | n/a | tt = time.gmtime(self.t) |
---|
216 | n/a | for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', |
---|
217 | n/a | 'j', 'm', 'M', 'p', 'S', |
---|
218 | n/a | 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): |
---|
219 | n/a | format = '%' + directive |
---|
220 | n/a | strf_output = time.strftime(format, tt) |
---|
221 | n/a | try: |
---|
222 | n/a | time.strptime(strf_output, format) |
---|
223 | n/a | except ValueError: |
---|
224 | n/a | self.fail("conversion specifier %r failed with '%s' input." % |
---|
225 | n/a | (format, strf_output)) |
---|
226 | n/a | |
---|
227 | n/a | def test_strptime_bytes(self): |
---|
228 | n/a | # Make sure only strings are accepted as arguments to strptime. |
---|
229 | n/a | self.assertRaises(TypeError, time.strptime, b'2009', "%Y") |
---|
230 | n/a | self.assertRaises(TypeError, time.strptime, '2009', b'%Y') |
---|
231 | n/a | |
---|
232 | n/a | def test_strptime_exception_context(self): |
---|
233 | n/a | # check that this doesn't chain exceptions needlessly (see #17572) |
---|
234 | n/a | with self.assertRaises(ValueError) as e: |
---|
235 | n/a | time.strptime('', '%D') |
---|
236 | n/a | self.assertIs(e.exception.__suppress_context__, True) |
---|
237 | n/a | # additional check for IndexError branch (issue #19545) |
---|
238 | n/a | with self.assertRaises(ValueError) as e: |
---|
239 | n/a | time.strptime('19', '%Y %') |
---|
240 | n/a | self.assertIs(e.exception.__suppress_context__, True) |
---|
241 | n/a | |
---|
242 | n/a | def test_asctime(self): |
---|
243 | n/a | time.asctime(time.gmtime(self.t)) |
---|
244 | n/a | |
---|
245 | n/a | # Max year is only limited by the size of C int. |
---|
246 | n/a | for bigyear in TIME_MAXYEAR, TIME_MINYEAR: |
---|
247 | n/a | asc = time.asctime((bigyear, 6, 1) + (0,) * 6) |
---|
248 | n/a | self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) |
---|
249 | n/a | self.assertRaises(OverflowError, time.asctime, |
---|
250 | n/a | (TIME_MAXYEAR + 1,) + (0,) * 8) |
---|
251 | n/a | self.assertRaises(OverflowError, time.asctime, |
---|
252 | n/a | (TIME_MINYEAR - 1,) + (0,) * 8) |
---|
253 | n/a | self.assertRaises(TypeError, time.asctime, 0) |
---|
254 | n/a | self.assertRaises(TypeError, time.asctime, ()) |
---|
255 | n/a | self.assertRaises(TypeError, time.asctime, (0,) * 10) |
---|
256 | n/a | |
---|
257 | n/a | def test_asctime_bounding_check(self): |
---|
258 | n/a | self._bounds_checking(time.asctime) |
---|
259 | n/a | |
---|
260 | n/a | def test_ctime(self): |
---|
261 | n/a | t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1)) |
---|
262 | n/a | self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973') |
---|
263 | n/a | t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1)) |
---|
264 | n/a | self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000') |
---|
265 | n/a | for year in [-100, 100, 1000, 2000, 2050, 10000]: |
---|
266 | n/a | try: |
---|
267 | n/a | testval = time.mktime((year, 1, 10) + (0,)*6) |
---|
268 | n/a | except (ValueError, OverflowError): |
---|
269 | n/a | # If mktime fails, ctime will fail too. This may happen |
---|
270 | n/a | # on some platforms. |
---|
271 | n/a | pass |
---|
272 | n/a | else: |
---|
273 | n/a | self.assertEqual(time.ctime(testval)[20:], str(year)) |
---|
274 | n/a | |
---|
275 | n/a | @unittest.skipUnless(hasattr(time, "tzset"), |
---|
276 | n/a | "time module has no attribute tzset") |
---|
277 | n/a | def test_tzset(self): |
---|
278 | n/a | |
---|
279 | n/a | from os import environ |
---|
280 | n/a | |
---|
281 | n/a | # Epoch time of midnight Dec 25th 2002. Never DST in northern |
---|
282 | n/a | # hemisphere. |
---|
283 | n/a | xmas2002 = 1040774400.0 |
---|
284 | n/a | |
---|
285 | n/a | # These formats are correct for 2002, and possibly future years |
---|
286 | n/a | # This format is the 'standard' as documented at: |
---|
287 | n/a | # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html |
---|
288 | n/a | # They are also documented in the tzset(3) man page on most Unix |
---|
289 | n/a | # systems. |
---|
290 | n/a | eastern = 'EST+05EDT,M4.1.0,M10.5.0' |
---|
291 | n/a | victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0' |
---|
292 | n/a | utc='UTC+0' |
---|
293 | n/a | |
---|
294 | n/a | org_TZ = environ.get('TZ',None) |
---|
295 | n/a | try: |
---|
296 | n/a | # Make sure we can switch to UTC time and results are correct |
---|
297 | n/a | # Note that unknown timezones default to UTC. |
---|
298 | n/a | # Note that altzone is undefined in UTC, as there is no DST |
---|
299 | n/a | environ['TZ'] = eastern |
---|
300 | n/a | time.tzset() |
---|
301 | n/a | environ['TZ'] = utc |
---|
302 | n/a | time.tzset() |
---|
303 | n/a | self.assertEqual( |
---|
304 | n/a | time.gmtime(xmas2002), time.localtime(xmas2002) |
---|
305 | n/a | ) |
---|
306 | n/a | self.assertEqual(time.daylight, 0) |
---|
307 | n/a | self.assertEqual(time.timezone, 0) |
---|
308 | n/a | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
---|
309 | n/a | |
---|
310 | n/a | # Make sure we can switch to US/Eastern |
---|
311 | n/a | environ['TZ'] = eastern |
---|
312 | n/a | time.tzset() |
---|
313 | n/a | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
---|
314 | n/a | self.assertEqual(time.tzname, ('EST', 'EDT')) |
---|
315 | n/a | self.assertEqual(len(time.tzname), 2) |
---|
316 | n/a | self.assertEqual(time.daylight, 1) |
---|
317 | n/a | self.assertEqual(time.timezone, 18000) |
---|
318 | n/a | self.assertEqual(time.altzone, 14400) |
---|
319 | n/a | self.assertEqual(time.localtime(xmas2002).tm_isdst, 0) |
---|
320 | n/a | self.assertEqual(len(time.tzname), 2) |
---|
321 | n/a | |
---|
322 | n/a | # Now go to the southern hemisphere. |
---|
323 | n/a | environ['TZ'] = victoria |
---|
324 | n/a | time.tzset() |
---|
325 | n/a | self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) |
---|
326 | n/a | |
---|
327 | n/a | # Issue #11886: Australian Eastern Standard Time (UTC+10) is called |
---|
328 | n/a | # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST" |
---|
329 | n/a | # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone), |
---|
330 | n/a | # on some operating systems (e.g. FreeBSD), which is wrong. See for |
---|
331 | n/a | # example this bug: |
---|
332 | n/a | # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810 |
---|
333 | n/a | self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0]) |
---|
334 | n/a | self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1])) |
---|
335 | n/a | self.assertEqual(len(time.tzname), 2) |
---|
336 | n/a | self.assertEqual(time.daylight, 1) |
---|
337 | n/a | self.assertEqual(time.timezone, -36000) |
---|
338 | n/a | self.assertEqual(time.altzone, -39600) |
---|
339 | n/a | self.assertEqual(time.localtime(xmas2002).tm_isdst, 1) |
---|
340 | n/a | |
---|
341 | n/a | finally: |
---|
342 | n/a | # Repair TZ environment variable in case any other tests |
---|
343 | n/a | # rely on it. |
---|
344 | n/a | if org_TZ is not None: |
---|
345 | n/a | environ['TZ'] = org_TZ |
---|
346 | n/a | elif 'TZ' in environ: |
---|
347 | n/a | del environ['TZ'] |
---|
348 | n/a | time.tzset() |
---|
349 | n/a | |
---|
350 | n/a | def test_insane_timestamps(self): |
---|
351 | n/a | # It's possible that some platform maps time_t to double, |
---|
352 | n/a | # and that this test will fail there. This test should |
---|
353 | n/a | # exempt such platforms (provided they return reasonable |
---|
354 | n/a | # results!). |
---|
355 | n/a | for func in time.ctime, time.gmtime, time.localtime: |
---|
356 | n/a | for unreasonable in -1e200, 1e200: |
---|
357 | n/a | self.assertRaises(OverflowError, func, unreasonable) |
---|
358 | n/a | |
---|
359 | n/a | def test_ctime_without_arg(self): |
---|
360 | n/a | # Not sure how to check the values, since the clock could tick |
---|
361 | n/a | # at any time. Make sure these are at least accepted and |
---|
362 | n/a | # don't raise errors. |
---|
363 | n/a | time.ctime() |
---|
364 | n/a | time.ctime(None) |
---|
365 | n/a | |
---|
366 | n/a | def test_gmtime_without_arg(self): |
---|
367 | n/a | gt0 = time.gmtime() |
---|
368 | n/a | gt1 = time.gmtime(None) |
---|
369 | n/a | t0 = time.mktime(gt0) |
---|
370 | n/a | t1 = time.mktime(gt1) |
---|
371 | n/a | self.assertAlmostEqual(t1, t0, delta=0.2) |
---|
372 | n/a | |
---|
373 | n/a | def test_localtime_without_arg(self): |
---|
374 | n/a | lt0 = time.localtime() |
---|
375 | n/a | lt1 = time.localtime(None) |
---|
376 | n/a | t0 = time.mktime(lt0) |
---|
377 | n/a | t1 = time.mktime(lt1) |
---|
378 | n/a | self.assertAlmostEqual(t1, t0, delta=0.2) |
---|
379 | n/a | |
---|
380 | n/a | def test_mktime(self): |
---|
381 | n/a | # Issue #1726687 |
---|
382 | n/a | for t in (-2, -1, 0, 1): |
---|
383 | n/a | if sys.platform.startswith('aix') and t == -1: |
---|
384 | n/a | # Issue #11188, #19748: mktime() returns -1 on error. On Linux, |
---|
385 | n/a | # the tm_wday field is used as a sentinel () to detect if -1 is |
---|
386 | n/a | # really an error or a valid timestamp. On AIX, tm_wday is |
---|
387 | n/a | # unchanged even on success and so cannot be used as a |
---|
388 | n/a | # sentinel. |
---|
389 | n/a | continue |
---|
390 | n/a | try: |
---|
391 | n/a | tt = time.localtime(t) |
---|
392 | n/a | except (OverflowError, OSError): |
---|
393 | n/a | pass |
---|
394 | n/a | else: |
---|
395 | n/a | self.assertEqual(time.mktime(tt), t) |
---|
396 | n/a | |
---|
397 | n/a | # Issue #13309: passing extreme values to mktime() or localtime() |
---|
398 | n/a | # borks the glibc's internal timezone data. |
---|
399 | n/a | @unittest.skipUnless(platform.libc_ver()[0] != 'glibc', |
---|
400 | n/a | "disabled because of a bug in glibc. Issue #13309") |
---|
401 | n/a | def test_mktime_error(self): |
---|
402 | n/a | # It may not be possible to reliably make mktime return error |
---|
403 | n/a | # on all platfom. This will make sure that no other exception |
---|
404 | n/a | # than OverflowError is raised for an extreme value. |
---|
405 | n/a | tt = time.gmtime(self.t) |
---|
406 | n/a | tzname = time.strftime('%Z', tt) |
---|
407 | n/a | self.assertNotEqual(tzname, 'LMT') |
---|
408 | n/a | try: |
---|
409 | n/a | time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1)) |
---|
410 | n/a | except OverflowError: |
---|
411 | n/a | pass |
---|
412 | n/a | self.assertEqual(time.strftime('%Z', tt), tzname) |
---|
413 | n/a | |
---|
414 | n/a | @unittest.skipUnless(hasattr(time, 'monotonic'), |
---|
415 | n/a | 'need time.monotonic') |
---|
416 | n/a | def test_monotonic(self): |
---|
417 | n/a | # monotonic() should not go backward |
---|
418 | n/a | times = [time.monotonic() for n in range(100)] |
---|
419 | n/a | t1 = times[0] |
---|
420 | n/a | for t2 in times[1:]: |
---|
421 | n/a | self.assertGreaterEqual(t2, t1, "times=%s" % times) |
---|
422 | n/a | t1 = t2 |
---|
423 | n/a | |
---|
424 | n/a | # monotonic() includes time elapsed during a sleep |
---|
425 | n/a | t1 = time.monotonic() |
---|
426 | n/a | time.sleep(0.5) |
---|
427 | n/a | t2 = time.monotonic() |
---|
428 | n/a | dt = t2 - t1 |
---|
429 | n/a | self.assertGreater(t2, t1) |
---|
430 | n/a | # Issue #20101: On some Windows machines, dt may be slightly low |
---|
431 | n/a | self.assertTrue(0.45 <= dt <= 1.0, dt) |
---|
432 | n/a | |
---|
433 | n/a | # monotonic() is a monotonic but non adjustable clock |
---|
434 | n/a | info = time.get_clock_info('monotonic') |
---|
435 | n/a | self.assertTrue(info.monotonic) |
---|
436 | n/a | self.assertFalse(info.adjustable) |
---|
437 | n/a | |
---|
438 | n/a | def test_perf_counter(self): |
---|
439 | n/a | time.perf_counter() |
---|
440 | n/a | |
---|
441 | n/a | def test_process_time(self): |
---|
442 | n/a | # process_time() should not include time spend during a sleep |
---|
443 | n/a | start = time.process_time() |
---|
444 | n/a | time.sleep(0.100) |
---|
445 | n/a | stop = time.process_time() |
---|
446 | n/a | # use 20 ms because process_time() has usually a resolution of 15 ms |
---|
447 | n/a | # on Windows |
---|
448 | n/a | self.assertLess(stop - start, 0.020) |
---|
449 | n/a | |
---|
450 | n/a | info = time.get_clock_info('process_time') |
---|
451 | n/a | self.assertTrue(info.monotonic) |
---|
452 | n/a | self.assertFalse(info.adjustable) |
---|
453 | n/a | |
---|
454 | n/a | @unittest.skipUnless(hasattr(time, 'monotonic'), |
---|
455 | n/a | 'need time.monotonic') |
---|
456 | n/a | @unittest.skipUnless(hasattr(time, 'clock_settime'), |
---|
457 | n/a | 'need time.clock_settime') |
---|
458 | n/a | def test_monotonic_settime(self): |
---|
459 | n/a | t1 = time.monotonic() |
---|
460 | n/a | realtime = time.clock_gettime(time.CLOCK_REALTIME) |
---|
461 | n/a | # jump backward with an offset of 1 hour |
---|
462 | n/a | try: |
---|
463 | n/a | time.clock_settime(time.CLOCK_REALTIME, realtime - 3600) |
---|
464 | n/a | except PermissionError as err: |
---|
465 | n/a | self.skipTest(err) |
---|
466 | n/a | t2 = time.monotonic() |
---|
467 | n/a | time.clock_settime(time.CLOCK_REALTIME, realtime) |
---|
468 | n/a | # monotonic must not be affected by system clock updates |
---|
469 | n/a | self.assertGreaterEqual(t2, t1) |
---|
470 | n/a | |
---|
471 | n/a | def test_localtime_failure(self): |
---|
472 | n/a | # Issue #13847: check for localtime() failure |
---|
473 | n/a | invalid_time_t = None |
---|
474 | n/a | for time_t in (-1, 2**30, 2**33, 2**60): |
---|
475 | n/a | try: |
---|
476 | n/a | time.localtime(time_t) |
---|
477 | n/a | except OverflowError: |
---|
478 | n/a | self.skipTest("need 64-bit time_t") |
---|
479 | n/a | except OSError: |
---|
480 | n/a | invalid_time_t = time_t |
---|
481 | n/a | break |
---|
482 | n/a | if invalid_time_t is None: |
---|
483 | n/a | self.skipTest("unable to find an invalid time_t value") |
---|
484 | n/a | |
---|
485 | n/a | self.assertRaises(OSError, time.localtime, invalid_time_t) |
---|
486 | n/a | self.assertRaises(OSError, time.ctime, invalid_time_t) |
---|
487 | n/a | |
---|
488 | n/a | def test_get_clock_info(self): |
---|
489 | n/a | clocks = ['clock', 'perf_counter', 'process_time', 'time'] |
---|
490 | n/a | if hasattr(time, 'monotonic'): |
---|
491 | n/a | clocks.append('monotonic') |
---|
492 | n/a | |
---|
493 | n/a | for name in clocks: |
---|
494 | n/a | info = time.get_clock_info(name) |
---|
495 | n/a | #self.assertIsInstance(info, dict) |
---|
496 | n/a | self.assertIsInstance(info.implementation, str) |
---|
497 | n/a | self.assertNotEqual(info.implementation, '') |
---|
498 | n/a | self.assertIsInstance(info.monotonic, bool) |
---|
499 | n/a | self.assertIsInstance(info.resolution, float) |
---|
500 | n/a | # 0.0 < resolution <= 1.0 |
---|
501 | n/a | self.assertGreater(info.resolution, 0.0) |
---|
502 | n/a | self.assertLessEqual(info.resolution, 1.0) |
---|
503 | n/a | self.assertIsInstance(info.adjustable, bool) |
---|
504 | n/a | |
---|
505 | n/a | self.assertRaises(ValueError, time.get_clock_info, 'xxx') |
---|
506 | n/a | |
---|
507 | n/a | |
---|
508 | n/a | class TestLocale(unittest.TestCase): |
---|
509 | n/a | def setUp(self): |
---|
510 | n/a | self.oldloc = locale.setlocale(locale.LC_ALL) |
---|
511 | n/a | |
---|
512 | n/a | def tearDown(self): |
---|
513 | n/a | locale.setlocale(locale.LC_ALL, self.oldloc) |
---|
514 | n/a | |
---|
515 | n/a | def test_bug_3061(self): |
---|
516 | n/a | try: |
---|
517 | n/a | tmp = locale.setlocale(locale.LC_ALL, "fr_FR") |
---|
518 | n/a | except locale.Error: |
---|
519 | n/a | self.skipTest('could not set locale.LC_ALL to fr_FR') |
---|
520 | n/a | # This should not cause an exception |
---|
521 | n/a | time.strftime("%B", (2009,2,1,0,0,0,0,0,0)) |
---|
522 | n/a | |
---|
523 | n/a | |
---|
524 | n/a | class _TestAsctimeYear: |
---|
525 | n/a | _format = '%d' |
---|
526 | n/a | |
---|
527 | n/a | def yearstr(self, y): |
---|
528 | n/a | return time.asctime((y,) + (0,) * 8).split()[-1] |
---|
529 | n/a | |
---|
530 | n/a | def test_large_year(self): |
---|
531 | n/a | # Check that it doesn't crash for year > 9999 |
---|
532 | n/a | self.assertEqual(self.yearstr(12345), '12345') |
---|
533 | n/a | self.assertEqual(self.yearstr(123456789), '123456789') |
---|
534 | n/a | |
---|
535 | n/a | class _TestStrftimeYear: |
---|
536 | n/a | |
---|
537 | n/a | # Issue 13305: For years < 1000, the value is not always |
---|
538 | n/a | # padded to 4 digits across platforms. The C standard |
---|
539 | n/a | # assumes year >= 1900, so it does not specify the number |
---|
540 | n/a | # of digits. |
---|
541 | n/a | |
---|
542 | n/a | if time.strftime('%Y', (1,) + (0,) * 8) == '0001': |
---|
543 | n/a | _format = '%04d' |
---|
544 | n/a | else: |
---|
545 | n/a | _format = '%d' |
---|
546 | n/a | |
---|
547 | n/a | def yearstr(self, y): |
---|
548 | n/a | return time.strftime('%Y', (y,) + (0,) * 8) |
---|
549 | n/a | |
---|
550 | n/a | def test_4dyear(self): |
---|
551 | n/a | # Check that we can return the zero padded value. |
---|
552 | n/a | if self._format == '%04d': |
---|
553 | n/a | self.test_year('%04d') |
---|
554 | n/a | else: |
---|
555 | n/a | def year4d(y): |
---|
556 | n/a | return time.strftime('%4Y', (y,) + (0,) * 8) |
---|
557 | n/a | self.test_year('%04d', func=year4d) |
---|
558 | n/a | |
---|
559 | n/a | def skip_if_not_supported(y): |
---|
560 | n/a | msg = "strftime() is limited to [1; 9999] with Visual Studio" |
---|
561 | n/a | # Check that it doesn't crash for year > 9999 |
---|
562 | n/a | try: |
---|
563 | n/a | time.strftime('%Y', (y,) + (0,) * 8) |
---|
564 | n/a | except ValueError: |
---|
565 | n/a | cond = False |
---|
566 | n/a | else: |
---|
567 | n/a | cond = True |
---|
568 | n/a | return unittest.skipUnless(cond, msg) |
---|
569 | n/a | |
---|
570 | n/a | @skip_if_not_supported(10000) |
---|
571 | n/a | def test_large_year(self): |
---|
572 | n/a | return super().test_large_year() |
---|
573 | n/a | |
---|
574 | n/a | @skip_if_not_supported(0) |
---|
575 | n/a | def test_negative(self): |
---|
576 | n/a | return super().test_negative() |
---|
577 | n/a | |
---|
578 | n/a | del skip_if_not_supported |
---|
579 | n/a | |
---|
580 | n/a | |
---|
581 | n/a | class _Test4dYear: |
---|
582 | n/a | _format = '%d' |
---|
583 | n/a | |
---|
584 | n/a | def test_year(self, fmt=None, func=None): |
---|
585 | n/a | fmt = fmt or self._format |
---|
586 | n/a | func = func or self.yearstr |
---|
587 | n/a | self.assertEqual(func(1), fmt % 1) |
---|
588 | n/a | self.assertEqual(func(68), fmt % 68) |
---|
589 | n/a | self.assertEqual(func(69), fmt % 69) |
---|
590 | n/a | self.assertEqual(func(99), fmt % 99) |
---|
591 | n/a | self.assertEqual(func(999), fmt % 999) |
---|
592 | n/a | self.assertEqual(func(9999), fmt % 9999) |
---|
593 | n/a | |
---|
594 | n/a | def test_large_year(self): |
---|
595 | n/a | self.assertEqual(self.yearstr(12345), '12345') |
---|
596 | n/a | self.assertEqual(self.yearstr(123456789), '123456789') |
---|
597 | n/a | self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR)) |
---|
598 | n/a | self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1) |
---|
599 | n/a | |
---|
600 | n/a | def test_negative(self): |
---|
601 | n/a | self.assertEqual(self.yearstr(-1), self._format % -1) |
---|
602 | n/a | self.assertEqual(self.yearstr(-1234), '-1234') |
---|
603 | n/a | self.assertEqual(self.yearstr(-123456), '-123456') |
---|
604 | n/a | self.assertEqual(self.yearstr(-123456789), str(-123456789)) |
---|
605 | n/a | self.assertEqual(self.yearstr(-1234567890), str(-1234567890)) |
---|
606 | n/a | self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900)) |
---|
607 | n/a | # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900 |
---|
608 | n/a | # Skip the value test, but check that no error is raised |
---|
609 | n/a | self.yearstr(TIME_MINYEAR) |
---|
610 | n/a | # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR)) |
---|
611 | n/a | self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1) |
---|
612 | n/a | |
---|
613 | n/a | |
---|
614 | n/a | class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase): |
---|
615 | n/a | pass |
---|
616 | n/a | |
---|
617 | n/a | class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase): |
---|
618 | n/a | pass |
---|
619 | n/a | |
---|
620 | n/a | |
---|
621 | n/a | class TestPytime(unittest.TestCase): |
---|
622 | n/a | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
---|
623 | n/a | def test_localtime_timezone(self): |
---|
624 | n/a | |
---|
625 | n/a | # Get the localtime and examine it for the offset and zone. |
---|
626 | n/a | lt = time.localtime() |
---|
627 | n/a | self.assertTrue(hasattr(lt, "tm_gmtoff")) |
---|
628 | n/a | self.assertTrue(hasattr(lt, "tm_zone")) |
---|
629 | n/a | |
---|
630 | n/a | # See if the offset and zone are similar to the module |
---|
631 | n/a | # attributes. |
---|
632 | n/a | if lt.tm_gmtoff is None: |
---|
633 | n/a | self.assertTrue(not hasattr(time, "timezone")) |
---|
634 | n/a | else: |
---|
635 | n/a | self.assertEqual(lt.tm_gmtoff, -[time.timezone, time.altzone][lt.tm_isdst]) |
---|
636 | n/a | if lt.tm_zone is None: |
---|
637 | n/a | self.assertTrue(not hasattr(time, "tzname")) |
---|
638 | n/a | else: |
---|
639 | n/a | self.assertEqual(lt.tm_zone, time.tzname[lt.tm_isdst]) |
---|
640 | n/a | |
---|
641 | n/a | # Try and make UNIX times from the localtime and a 9-tuple |
---|
642 | n/a | # created from the localtime. Test to see that the times are |
---|
643 | n/a | # the same. |
---|
644 | n/a | t = time.mktime(lt); t9 = time.mktime(lt[:9]) |
---|
645 | n/a | self.assertEqual(t, t9) |
---|
646 | n/a | |
---|
647 | n/a | # Make localtimes from the UNIX times and compare them to |
---|
648 | n/a | # the original localtime, thus making a round trip. |
---|
649 | n/a | new_lt = time.localtime(t); new_lt9 = time.localtime(t9) |
---|
650 | n/a | self.assertEqual(new_lt, lt) |
---|
651 | n/a | self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff) |
---|
652 | n/a | self.assertEqual(new_lt.tm_zone, lt.tm_zone) |
---|
653 | n/a | self.assertEqual(new_lt9, lt) |
---|
654 | n/a | self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff) |
---|
655 | n/a | self.assertEqual(new_lt9.tm_zone, lt.tm_zone) |
---|
656 | n/a | |
---|
657 | n/a | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
---|
658 | n/a | def test_strptime_timezone(self): |
---|
659 | n/a | t = time.strptime("UTC", "%Z") |
---|
660 | n/a | self.assertEqual(t.tm_zone, 'UTC') |
---|
661 | n/a | t = time.strptime("+0500", "%z") |
---|
662 | n/a | self.assertEqual(t.tm_gmtoff, 5 * 3600) |
---|
663 | n/a | |
---|
664 | n/a | @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support") |
---|
665 | n/a | def test_short_times(self): |
---|
666 | n/a | |
---|
667 | n/a | import pickle |
---|
668 | n/a | |
---|
669 | n/a | # Load a short time structure using pickle. |
---|
670 | n/a | st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\nI1\ntp1\n(dp2\ntp3\nRp4\n." |
---|
671 | n/a | lt = pickle.loads(st) |
---|
672 | n/a | self.assertIs(lt.tm_gmtoff, None) |
---|
673 | n/a | self.assertIs(lt.tm_zone, None) |
---|
674 | n/a | |
---|
675 | n/a | |
---|
676 | n/a | @unittest.skipIf(_testcapi is None, 'need the _testcapi module') |
---|
677 | n/a | class CPyTimeTestCase: |
---|
678 | n/a | """ |
---|
679 | n/a | Base class to test the C _PyTime_t API. |
---|
680 | n/a | """ |
---|
681 | n/a | OVERFLOW_SECONDS = None |
---|
682 | n/a | |
---|
683 | n/a | def setUp(self): |
---|
684 | n/a | from _testcapi import SIZEOF_TIME_T |
---|
685 | n/a | bits = SIZEOF_TIME_T * 8 - 1 |
---|
686 | n/a | self.time_t_min = -2 ** bits |
---|
687 | n/a | self.time_t_max = 2 ** bits - 1 |
---|
688 | n/a | |
---|
689 | n/a | def time_t_filter(self, seconds): |
---|
690 | n/a | return (self.time_t_min <= seconds <= self.time_t_max) |
---|
691 | n/a | |
---|
692 | n/a | def _rounding_values(self, use_float): |
---|
693 | n/a | "Build timestamps used to test rounding." |
---|
694 | n/a | |
---|
695 | n/a | units = [1, US_TO_NS, MS_TO_NS, SEC_TO_NS] |
---|
696 | n/a | if use_float: |
---|
697 | n/a | # picoseconds are only tested to pytime_converter accepting floats |
---|
698 | n/a | units.append(1e-3) |
---|
699 | n/a | |
---|
700 | n/a | values = ( |
---|
701 | n/a | # small values |
---|
702 | n/a | 1, 2, 5, 7, 123, 456, 1234, |
---|
703 | n/a | # 10^k - 1 |
---|
704 | n/a | 9, |
---|
705 | n/a | 99, |
---|
706 | n/a | 999, |
---|
707 | n/a | 9999, |
---|
708 | n/a | 99999, |
---|
709 | n/a | 999999, |
---|
710 | n/a | # test half even rounding near 0.5, 1.5, 2.5, 3.5, 4.5 |
---|
711 | n/a | 499, 500, 501, |
---|
712 | n/a | 1499, 1500, 1501, |
---|
713 | n/a | 2500, |
---|
714 | n/a | 3500, |
---|
715 | n/a | 4500, |
---|
716 | n/a | ) |
---|
717 | n/a | |
---|
718 | n/a | ns_timestamps = [0] |
---|
719 | n/a | for unit in units: |
---|
720 | n/a | for value in values: |
---|
721 | n/a | ns = value * unit |
---|
722 | n/a | ns_timestamps.extend((-ns, ns)) |
---|
723 | n/a | for pow2 in (0, 5, 10, 15, 22, 23, 24, 30, 33): |
---|
724 | n/a | ns = (2 ** pow2) * SEC_TO_NS |
---|
725 | n/a | ns_timestamps.extend(( |
---|
726 | n/a | -ns-1, -ns, -ns+1, |
---|
727 | n/a | ns-1, ns, ns+1 |
---|
728 | n/a | )) |
---|
729 | n/a | for seconds in (_testcapi.INT_MIN, _testcapi.INT_MAX): |
---|
730 | n/a | ns_timestamps.append(seconds * SEC_TO_NS) |
---|
731 | n/a | if use_float: |
---|
732 | n/a | # numbers with an exact representation in IEEE 754 (base 2) |
---|
733 | n/a | for pow2 in (3, 7, 10, 15): |
---|
734 | n/a | ns = 2.0 ** (-pow2) |
---|
735 | n/a | ns_timestamps.extend((-ns, ns)) |
---|
736 | n/a | |
---|
737 | n/a | # seconds close to _PyTime_t type limit |
---|
738 | n/a | ns = (2 ** 63 // SEC_TO_NS) * SEC_TO_NS |
---|
739 | n/a | ns_timestamps.extend((-ns, ns)) |
---|
740 | n/a | |
---|
741 | n/a | return ns_timestamps |
---|
742 | n/a | |
---|
743 | n/a | def _check_rounding(self, pytime_converter, expected_func, |
---|
744 | n/a | use_float, unit_to_sec, value_filter=None): |
---|
745 | n/a | |
---|
746 | n/a | def convert_values(ns_timestamps): |
---|
747 | n/a | if use_float: |
---|
748 | n/a | unit_to_ns = SEC_TO_NS / float(unit_to_sec) |
---|
749 | n/a | values = [ns / unit_to_ns for ns in ns_timestamps] |
---|
750 | n/a | else: |
---|
751 | n/a | unit_to_ns = SEC_TO_NS // unit_to_sec |
---|
752 | n/a | values = [ns // unit_to_ns for ns in ns_timestamps] |
---|
753 | n/a | |
---|
754 | n/a | if value_filter: |
---|
755 | n/a | values = filter(value_filter, values) |
---|
756 | n/a | |
---|
757 | n/a | # remove duplicates and sort |
---|
758 | n/a | return sorted(set(values)) |
---|
759 | n/a | |
---|
760 | n/a | # test rounding |
---|
761 | n/a | ns_timestamps = self._rounding_values(use_float) |
---|
762 | n/a | valid_values = convert_values(ns_timestamps) |
---|
763 | n/a | for time_rnd, decimal_rnd in ROUNDING_MODES : |
---|
764 | n/a | context = decimal.getcontext() |
---|
765 | n/a | context.rounding = decimal_rnd |
---|
766 | n/a | |
---|
767 | n/a | for value in valid_values: |
---|
768 | n/a | debug_info = {'value': value, 'rounding': decimal_rnd} |
---|
769 | n/a | try: |
---|
770 | n/a | result = pytime_converter(value, time_rnd) |
---|
771 | n/a | expected = expected_func(value) |
---|
772 | n/a | except Exception as exc: |
---|
773 | n/a | self.fail("Error on timestamp conversion: %s" % debug_info) |
---|
774 | n/a | self.assertEqual(result, |
---|
775 | n/a | expected, |
---|
776 | n/a | debug_info) |
---|
777 | n/a | |
---|
778 | n/a | # test overflow |
---|
779 | n/a | ns = self.OVERFLOW_SECONDS * SEC_TO_NS |
---|
780 | n/a | ns_timestamps = (-ns, ns) |
---|
781 | n/a | overflow_values = convert_values(ns_timestamps) |
---|
782 | n/a | for time_rnd, _ in ROUNDING_MODES : |
---|
783 | n/a | for value in overflow_values: |
---|
784 | n/a | debug_info = {'value': value, 'rounding': time_rnd} |
---|
785 | n/a | with self.assertRaises(OverflowError, msg=debug_info): |
---|
786 | n/a | pytime_converter(value, time_rnd) |
---|
787 | n/a | |
---|
788 | n/a | def check_int_rounding(self, pytime_converter, expected_func, |
---|
789 | n/a | unit_to_sec=1, value_filter=None): |
---|
790 | n/a | self._check_rounding(pytime_converter, expected_func, |
---|
791 | n/a | False, unit_to_sec, value_filter) |
---|
792 | n/a | |
---|
793 | n/a | def check_float_rounding(self, pytime_converter, expected_func, |
---|
794 | n/a | unit_to_sec=1, value_filter=None): |
---|
795 | n/a | self._check_rounding(pytime_converter, expected_func, |
---|
796 | n/a | True, unit_to_sec, value_filter) |
---|
797 | n/a | |
---|
798 | n/a | def decimal_round(self, x): |
---|
799 | n/a | d = decimal.Decimal(x) |
---|
800 | n/a | d = d.quantize(1) |
---|
801 | n/a | return int(d) |
---|
802 | n/a | |
---|
803 | n/a | |
---|
804 | n/a | class TestCPyTime(CPyTimeTestCase, unittest.TestCase): |
---|
805 | n/a | """ |
---|
806 | n/a | Test the C _PyTime_t API. |
---|
807 | n/a | """ |
---|
808 | n/a | # _PyTime_t is a 64-bit signed integer |
---|
809 | n/a | OVERFLOW_SECONDS = math.ceil((2**63 + 1) / SEC_TO_NS) |
---|
810 | n/a | |
---|
811 | n/a | def test_FromSeconds(self): |
---|
812 | n/a | from _testcapi import PyTime_FromSeconds |
---|
813 | n/a | |
---|
814 | n/a | # PyTime_FromSeconds() expects a C int, reject values out of range |
---|
815 | n/a | def c_int_filter(secs): |
---|
816 | n/a | return (_testcapi.INT_MIN <= secs <= _testcapi.INT_MAX) |
---|
817 | n/a | |
---|
818 | n/a | self.check_int_rounding(lambda secs, rnd: PyTime_FromSeconds(secs), |
---|
819 | n/a | lambda secs: secs * SEC_TO_NS, |
---|
820 | n/a | value_filter=c_int_filter) |
---|
821 | n/a | |
---|
822 | n/a | def test_FromSecondsObject(self): |
---|
823 | n/a | from _testcapi import PyTime_FromSecondsObject |
---|
824 | n/a | |
---|
825 | n/a | self.check_int_rounding( |
---|
826 | n/a | PyTime_FromSecondsObject, |
---|
827 | n/a | lambda secs: secs * SEC_TO_NS) |
---|
828 | n/a | |
---|
829 | n/a | self.check_float_rounding( |
---|
830 | n/a | PyTime_FromSecondsObject, |
---|
831 | n/a | lambda ns: self.decimal_round(ns * SEC_TO_NS)) |
---|
832 | n/a | |
---|
833 | n/a | def test_AsSecondsDouble(self): |
---|
834 | n/a | from _testcapi import PyTime_AsSecondsDouble |
---|
835 | n/a | |
---|
836 | n/a | def float_converter(ns): |
---|
837 | n/a | if abs(ns) % SEC_TO_NS == 0: |
---|
838 | n/a | return float(ns // SEC_TO_NS) |
---|
839 | n/a | else: |
---|
840 | n/a | return float(ns) / SEC_TO_NS |
---|
841 | n/a | |
---|
842 | n/a | self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns), |
---|
843 | n/a | float_converter, |
---|
844 | n/a | NS_TO_SEC) |
---|
845 | n/a | |
---|
846 | n/a | def create_decimal_converter(self, denominator): |
---|
847 | n/a | denom = decimal.Decimal(denominator) |
---|
848 | n/a | |
---|
849 | n/a | def converter(value): |
---|
850 | n/a | d = decimal.Decimal(value) / denom |
---|
851 | n/a | return self.decimal_round(d) |
---|
852 | n/a | |
---|
853 | n/a | return converter |
---|
854 | n/a | |
---|
855 | n/a | def test_AsTimeval(self): |
---|
856 | n/a | from _testcapi import PyTime_AsTimeval |
---|
857 | n/a | |
---|
858 | n/a | us_converter = self.create_decimal_converter(US_TO_NS) |
---|
859 | n/a | |
---|
860 | n/a | def timeval_converter(ns): |
---|
861 | n/a | us = us_converter(ns) |
---|
862 | n/a | return divmod(us, SEC_TO_US) |
---|
863 | n/a | |
---|
864 | n/a | if sys.platform == 'win32': |
---|
865 | n/a | from _testcapi import LONG_MIN, LONG_MAX |
---|
866 | n/a | |
---|
867 | n/a | # On Windows, timeval.tv_sec type is a C long |
---|
868 | n/a | def seconds_filter(secs): |
---|
869 | n/a | return LONG_MIN <= secs <= LONG_MAX |
---|
870 | n/a | else: |
---|
871 | n/a | seconds_filter = self.time_t_filter |
---|
872 | n/a | |
---|
873 | n/a | self.check_int_rounding(PyTime_AsTimeval, |
---|
874 | n/a | timeval_converter, |
---|
875 | n/a | NS_TO_SEC, |
---|
876 | n/a | value_filter=seconds_filter) |
---|
877 | n/a | |
---|
878 | n/a | @unittest.skipUnless(hasattr(_testcapi, 'PyTime_AsTimespec'), |
---|
879 | n/a | 'need _testcapi.PyTime_AsTimespec') |
---|
880 | n/a | def test_AsTimespec(self): |
---|
881 | n/a | from _testcapi import PyTime_AsTimespec |
---|
882 | n/a | |
---|
883 | n/a | def timespec_converter(ns): |
---|
884 | n/a | return divmod(ns, SEC_TO_NS) |
---|
885 | n/a | |
---|
886 | n/a | self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns), |
---|
887 | n/a | timespec_converter, |
---|
888 | n/a | NS_TO_SEC, |
---|
889 | n/a | value_filter=self.time_t_filter) |
---|
890 | n/a | |
---|
891 | n/a | def test_AsMilliseconds(self): |
---|
892 | n/a | from _testcapi import PyTime_AsMilliseconds |
---|
893 | n/a | |
---|
894 | n/a | self.check_int_rounding(PyTime_AsMilliseconds, |
---|
895 | n/a | self.create_decimal_converter(MS_TO_NS), |
---|
896 | n/a | NS_TO_SEC) |
---|
897 | n/a | |
---|
898 | n/a | def test_AsMicroseconds(self): |
---|
899 | n/a | from _testcapi import PyTime_AsMicroseconds |
---|
900 | n/a | |
---|
901 | n/a | self.check_int_rounding(PyTime_AsMicroseconds, |
---|
902 | n/a | self.create_decimal_converter(US_TO_NS), |
---|
903 | n/a | NS_TO_SEC) |
---|
904 | n/a | |
---|
905 | n/a | |
---|
906 | n/a | class TestOldPyTime(CPyTimeTestCase, unittest.TestCase): |
---|
907 | n/a | """ |
---|
908 | n/a | Test the old C _PyTime_t API: _PyTime_ObjectToXXX() functions. |
---|
909 | n/a | """ |
---|
910 | n/a | |
---|
911 | n/a | # time_t is a 32-bit or 64-bit signed integer |
---|
912 | n/a | OVERFLOW_SECONDS = 2 ** 64 |
---|
913 | n/a | |
---|
914 | n/a | def test_object_to_time_t(self): |
---|
915 | n/a | from _testcapi import pytime_object_to_time_t |
---|
916 | n/a | |
---|
917 | n/a | self.check_int_rounding(pytime_object_to_time_t, |
---|
918 | n/a | lambda secs: secs, |
---|
919 | n/a | value_filter=self.time_t_filter) |
---|
920 | n/a | |
---|
921 | n/a | self.check_float_rounding(pytime_object_to_time_t, |
---|
922 | n/a | self.decimal_round, |
---|
923 | n/a | value_filter=self.time_t_filter) |
---|
924 | n/a | |
---|
925 | n/a | def create_converter(self, sec_to_unit): |
---|
926 | n/a | def converter(secs): |
---|
927 | n/a | floatpart, intpart = math.modf(secs) |
---|
928 | n/a | intpart = int(intpart) |
---|
929 | n/a | floatpart *= sec_to_unit |
---|
930 | n/a | floatpart = self.decimal_round(floatpart) |
---|
931 | n/a | if floatpart < 0: |
---|
932 | n/a | floatpart += sec_to_unit |
---|
933 | n/a | intpart -= 1 |
---|
934 | n/a | elif floatpart >= sec_to_unit: |
---|
935 | n/a | floatpart -= sec_to_unit |
---|
936 | n/a | intpart += 1 |
---|
937 | n/a | return (intpart, floatpart) |
---|
938 | n/a | return converter |
---|
939 | n/a | |
---|
940 | n/a | def test_object_to_timeval(self): |
---|
941 | n/a | from _testcapi import pytime_object_to_timeval |
---|
942 | n/a | |
---|
943 | n/a | self.check_int_rounding(pytime_object_to_timeval, |
---|
944 | n/a | lambda secs: (secs, 0), |
---|
945 | n/a | value_filter=self.time_t_filter) |
---|
946 | n/a | |
---|
947 | n/a | self.check_float_rounding(pytime_object_to_timeval, |
---|
948 | n/a | self.create_converter(SEC_TO_US), |
---|
949 | n/a | value_filter=self.time_t_filter) |
---|
950 | n/a | |
---|
951 | n/a | def test_object_to_timespec(self): |
---|
952 | n/a | from _testcapi import pytime_object_to_timespec |
---|
953 | n/a | |
---|
954 | n/a | self.check_int_rounding(pytime_object_to_timespec, |
---|
955 | n/a | lambda secs: (secs, 0), |
---|
956 | n/a | value_filter=self.time_t_filter) |
---|
957 | n/a | |
---|
958 | n/a | self.check_float_rounding(pytime_object_to_timespec, |
---|
959 | n/a | self.create_converter(SEC_TO_NS), |
---|
960 | n/a | value_filter=self.time_t_filter) |
---|
961 | n/a | |
---|
962 | n/a | |
---|
963 | n/a | if __name__ == "__main__": |
---|
964 | n/a | unittest.main() |
---|