1 | n/a | #include "Python.h" |
---|
2 | n/a | #ifdef MS_WINDOWS |
---|
3 | n/a | #include <windows.h> |
---|
4 | n/a | #endif |
---|
5 | n/a | |
---|
6 | n/a | #if defined(__APPLE__) |
---|
7 | n/a | #include <mach/mach_time.h> /* mach_absolute_time(), mach_timebase_info() */ |
---|
8 | n/a | #endif |
---|
9 | n/a | |
---|
10 | n/a | #define _PyTime_check_mul_overflow(a, b) \ |
---|
11 | n/a | (assert(b > 0), \ |
---|
12 | n/a | (_PyTime_t)(a) < _PyTime_MIN / (_PyTime_t)(b) \ |
---|
13 | n/a | || _PyTime_MAX / (_PyTime_t)(b) < (_PyTime_t)(a)) |
---|
14 | n/a | |
---|
15 | n/a | /* To millisecond (10^-3) */ |
---|
16 | n/a | #define SEC_TO_MS 1000 |
---|
17 | n/a | |
---|
18 | n/a | /* To microseconds (10^-6) */ |
---|
19 | n/a | #define MS_TO_US 1000 |
---|
20 | n/a | #define SEC_TO_US (SEC_TO_MS * MS_TO_US) |
---|
21 | n/a | |
---|
22 | n/a | /* To nanoseconds (10^-9) */ |
---|
23 | n/a | #define US_TO_NS 1000 |
---|
24 | n/a | #define MS_TO_NS (MS_TO_US * US_TO_NS) |
---|
25 | n/a | #define SEC_TO_NS (SEC_TO_MS * MS_TO_NS) |
---|
26 | n/a | |
---|
27 | n/a | /* Conversion from nanoseconds */ |
---|
28 | n/a | #define NS_TO_MS (1000 * 1000) |
---|
29 | n/a | #define NS_TO_US (1000) |
---|
30 | n/a | |
---|
31 | n/a | static void |
---|
32 | n/a | error_time_t_overflow(void) |
---|
33 | n/a | { |
---|
34 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
35 | n/a | "timestamp out of range for platform time_t"); |
---|
36 | n/a | } |
---|
37 | n/a | |
---|
38 | n/a | time_t |
---|
39 | n/a | _PyLong_AsTime_t(PyObject *obj) |
---|
40 | n/a | { |
---|
41 | n/a | #if SIZEOF_TIME_T == SIZEOF_LONG_LONG |
---|
42 | n/a | long long val; |
---|
43 | n/a | val = PyLong_AsLongLong(obj); |
---|
44 | n/a | #else |
---|
45 | n/a | long val; |
---|
46 | n/a | Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long)); |
---|
47 | n/a | val = PyLong_AsLong(obj); |
---|
48 | n/a | #endif |
---|
49 | n/a | if (val == -1 && PyErr_Occurred()) { |
---|
50 | n/a | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
---|
51 | n/a | error_time_t_overflow(); |
---|
52 | n/a | return -1; |
---|
53 | n/a | } |
---|
54 | n/a | return (time_t)val; |
---|
55 | n/a | } |
---|
56 | n/a | |
---|
57 | n/a | PyObject * |
---|
58 | n/a | _PyLong_FromTime_t(time_t t) |
---|
59 | n/a | { |
---|
60 | n/a | #if SIZEOF_TIME_T == SIZEOF_LONG_LONG |
---|
61 | n/a | return PyLong_FromLongLong((long long)t); |
---|
62 | n/a | #else |
---|
63 | n/a | Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long)); |
---|
64 | n/a | return PyLong_FromLong((long)t); |
---|
65 | n/a | #endif |
---|
66 | n/a | } |
---|
67 | n/a | |
---|
68 | n/a | /* Round to nearest with ties going to nearest even integer |
---|
69 | n/a | (_PyTime_ROUND_HALF_EVEN) */ |
---|
70 | n/a | static double |
---|
71 | n/a | _PyTime_RoundHalfEven(double x) |
---|
72 | n/a | { |
---|
73 | n/a | double rounded = round(x); |
---|
74 | n/a | if (fabs(x-rounded) == 0.5) |
---|
75 | n/a | /* halfway case: round to even */ |
---|
76 | n/a | rounded = 2.0*round(x/2.0); |
---|
77 | n/a | return rounded; |
---|
78 | n/a | } |
---|
79 | n/a | |
---|
80 | n/a | static double |
---|
81 | n/a | _PyTime_Round(double x, _PyTime_round_t round) |
---|
82 | n/a | { |
---|
83 | n/a | /* volatile avoids optimization changing how numbers are rounded */ |
---|
84 | n/a | volatile double d; |
---|
85 | n/a | |
---|
86 | n/a | d = x; |
---|
87 | n/a | if (round == _PyTime_ROUND_HALF_EVEN) |
---|
88 | n/a | d = _PyTime_RoundHalfEven(d); |
---|
89 | n/a | else if (round == _PyTime_ROUND_CEILING) |
---|
90 | n/a | d = ceil(d); |
---|
91 | n/a | else |
---|
92 | n/a | d = floor(d); |
---|
93 | n/a | return d; |
---|
94 | n/a | } |
---|
95 | n/a | |
---|
96 | n/a | static int |
---|
97 | n/a | _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator, |
---|
98 | n/a | double denominator, _PyTime_round_t round) |
---|
99 | n/a | { |
---|
100 | n/a | double intpart, err; |
---|
101 | n/a | /* volatile avoids optimization changing how numbers are rounded */ |
---|
102 | n/a | volatile double floatpart; |
---|
103 | n/a | |
---|
104 | n/a | floatpart = modf(d, &intpart); |
---|
105 | n/a | |
---|
106 | n/a | floatpart *= denominator; |
---|
107 | n/a | floatpart = _PyTime_Round(floatpart, round); |
---|
108 | n/a | if (floatpart >= denominator) { |
---|
109 | n/a | floatpart -= denominator; |
---|
110 | n/a | intpart += 1.0; |
---|
111 | n/a | } |
---|
112 | n/a | else if (floatpart < 0) { |
---|
113 | n/a | floatpart += denominator; |
---|
114 | n/a | intpart -= 1.0; |
---|
115 | n/a | } |
---|
116 | n/a | assert(0.0 <= floatpart && floatpart < denominator); |
---|
117 | n/a | |
---|
118 | n/a | *sec = (time_t)intpart; |
---|
119 | n/a | *numerator = (long)floatpart; |
---|
120 | n/a | |
---|
121 | n/a | err = intpart - (double)*sec; |
---|
122 | n/a | if (err <= -1.0 || err >= 1.0) { |
---|
123 | n/a | error_time_t_overflow(); |
---|
124 | n/a | return -1; |
---|
125 | n/a | } |
---|
126 | n/a | return 0; |
---|
127 | n/a | } |
---|
128 | n/a | |
---|
129 | n/a | static int |
---|
130 | n/a | _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, |
---|
131 | n/a | double denominator, _PyTime_round_t round) |
---|
132 | n/a | { |
---|
133 | n/a | assert(denominator <= (double)LONG_MAX); |
---|
134 | n/a | |
---|
135 | n/a | if (PyFloat_Check(obj)) { |
---|
136 | n/a | double d = PyFloat_AsDouble(obj); |
---|
137 | n/a | return _PyTime_DoubleToDenominator(d, sec, numerator, |
---|
138 | n/a | denominator, round); |
---|
139 | n/a | } |
---|
140 | n/a | else { |
---|
141 | n/a | *sec = _PyLong_AsTime_t(obj); |
---|
142 | n/a | *numerator = 0; |
---|
143 | n/a | if (*sec == (time_t)-1 && PyErr_Occurred()) |
---|
144 | n/a | return -1; |
---|
145 | n/a | return 0; |
---|
146 | n/a | } |
---|
147 | n/a | } |
---|
148 | n/a | |
---|
149 | n/a | int |
---|
150 | n/a | _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) |
---|
151 | n/a | { |
---|
152 | n/a | if (PyFloat_Check(obj)) { |
---|
153 | n/a | double intpart, err; |
---|
154 | n/a | /* volatile avoids optimization changing how numbers are rounded */ |
---|
155 | n/a | volatile double d; |
---|
156 | n/a | |
---|
157 | n/a | d = PyFloat_AsDouble(obj); |
---|
158 | n/a | d = _PyTime_Round(d, round); |
---|
159 | n/a | (void)modf(d, &intpart); |
---|
160 | n/a | |
---|
161 | n/a | *sec = (time_t)intpart; |
---|
162 | n/a | err = intpart - (double)*sec; |
---|
163 | n/a | if (err <= -1.0 || err >= 1.0) { |
---|
164 | n/a | error_time_t_overflow(); |
---|
165 | n/a | return -1; |
---|
166 | n/a | } |
---|
167 | n/a | return 0; |
---|
168 | n/a | } |
---|
169 | n/a | else { |
---|
170 | n/a | *sec = _PyLong_AsTime_t(obj); |
---|
171 | n/a | if (*sec == (time_t)-1 && PyErr_Occurred()) |
---|
172 | n/a | return -1; |
---|
173 | n/a | return 0; |
---|
174 | n/a | } |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | int |
---|
178 | n/a | _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec, |
---|
179 | n/a | _PyTime_round_t round) |
---|
180 | n/a | { |
---|
181 | n/a | int res; |
---|
182 | n/a | res = _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round); |
---|
183 | n/a | assert(0 <= *nsec && *nsec < SEC_TO_NS); |
---|
184 | n/a | return res; |
---|
185 | n/a | } |
---|
186 | n/a | |
---|
187 | n/a | int |
---|
188 | n/a | _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec, |
---|
189 | n/a | _PyTime_round_t round) |
---|
190 | n/a | { |
---|
191 | n/a | int res; |
---|
192 | n/a | res = _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round); |
---|
193 | n/a | assert(0 <= *usec && *usec < SEC_TO_US); |
---|
194 | n/a | return res; |
---|
195 | n/a | } |
---|
196 | n/a | |
---|
197 | n/a | static void |
---|
198 | n/a | _PyTime_overflow(void) |
---|
199 | n/a | { |
---|
200 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
201 | n/a | "timestamp too large to convert to C _PyTime_t"); |
---|
202 | n/a | } |
---|
203 | n/a | |
---|
204 | n/a | _PyTime_t |
---|
205 | n/a | _PyTime_FromSeconds(int seconds) |
---|
206 | n/a | { |
---|
207 | n/a | _PyTime_t t; |
---|
208 | n/a | t = (_PyTime_t)seconds; |
---|
209 | n/a | /* ensure that integer overflow cannot happen, int type should have 32 |
---|
210 | n/a | bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30 |
---|
211 | n/a | bits). */ |
---|
212 | n/a | Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS); |
---|
213 | n/a | Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS); |
---|
214 | n/a | assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS) |
---|
215 | n/a | || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS)); |
---|
216 | n/a | t *= SEC_TO_NS; |
---|
217 | n/a | return t; |
---|
218 | n/a | } |
---|
219 | n/a | |
---|
220 | n/a | _PyTime_t |
---|
221 | n/a | _PyTime_FromNanoseconds(long long ns) |
---|
222 | n/a | { |
---|
223 | n/a | _PyTime_t t; |
---|
224 | n/a | Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t)); |
---|
225 | n/a | t = Py_SAFE_DOWNCAST(ns, long long, _PyTime_t); |
---|
226 | n/a | return t; |
---|
227 | n/a | } |
---|
228 | n/a | |
---|
229 | n/a | #ifdef HAVE_CLOCK_GETTIME |
---|
230 | n/a | static int |
---|
231 | n/a | _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise) |
---|
232 | n/a | { |
---|
233 | n/a | _PyTime_t t; |
---|
234 | n/a | int res = 0; |
---|
235 | n/a | |
---|
236 | n/a | Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t)); |
---|
237 | n/a | t = (_PyTime_t)ts->tv_sec; |
---|
238 | n/a | |
---|
239 | n/a | if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) { |
---|
240 | n/a | if (raise) |
---|
241 | n/a | _PyTime_overflow(); |
---|
242 | n/a | res = -1; |
---|
243 | n/a | } |
---|
244 | n/a | t = t * SEC_TO_NS; |
---|
245 | n/a | |
---|
246 | n/a | t += ts->tv_nsec; |
---|
247 | n/a | |
---|
248 | n/a | *tp = t; |
---|
249 | n/a | return res; |
---|
250 | n/a | } |
---|
251 | n/a | #elif !defined(MS_WINDOWS) |
---|
252 | n/a | static int |
---|
253 | n/a | _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise) |
---|
254 | n/a | { |
---|
255 | n/a | _PyTime_t t; |
---|
256 | n/a | int res = 0; |
---|
257 | n/a | |
---|
258 | n/a | Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t)); |
---|
259 | n/a | t = (_PyTime_t)tv->tv_sec; |
---|
260 | n/a | |
---|
261 | n/a | if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) { |
---|
262 | n/a | if (raise) |
---|
263 | n/a | _PyTime_overflow(); |
---|
264 | n/a | res = -1; |
---|
265 | n/a | } |
---|
266 | n/a | t = t * SEC_TO_NS; |
---|
267 | n/a | |
---|
268 | n/a | t += (_PyTime_t)tv->tv_usec * US_TO_NS; |
---|
269 | n/a | |
---|
270 | n/a | *tp = t; |
---|
271 | n/a | return res; |
---|
272 | n/a | } |
---|
273 | n/a | #endif |
---|
274 | n/a | |
---|
275 | n/a | static int |
---|
276 | n/a | _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round, |
---|
277 | n/a | long unit_to_ns) |
---|
278 | n/a | { |
---|
279 | n/a | double err; |
---|
280 | n/a | /* volatile avoids optimization changing how numbers are rounded */ |
---|
281 | n/a | volatile double d; |
---|
282 | n/a | |
---|
283 | n/a | /* convert to a number of nanoseconds */ |
---|
284 | n/a | d = value; |
---|
285 | n/a | d *= (double)unit_to_ns; |
---|
286 | n/a | d = _PyTime_Round(d, round); |
---|
287 | n/a | |
---|
288 | n/a | *t = (_PyTime_t)d; |
---|
289 | n/a | err = d - (double)*t; |
---|
290 | n/a | if (fabs(err) >= 1.0) { |
---|
291 | n/a | _PyTime_overflow(); |
---|
292 | n/a | return -1; |
---|
293 | n/a | } |
---|
294 | n/a | return 0; |
---|
295 | n/a | } |
---|
296 | n/a | |
---|
297 | n/a | static int |
---|
298 | n/a | _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, |
---|
299 | n/a | long unit_to_ns) |
---|
300 | n/a | { |
---|
301 | n/a | if (PyFloat_Check(obj)) { |
---|
302 | n/a | double d; |
---|
303 | n/a | d = PyFloat_AsDouble(obj); |
---|
304 | n/a | return _PyTime_FromFloatObject(t, d, round, unit_to_ns); |
---|
305 | n/a | } |
---|
306 | n/a | else { |
---|
307 | n/a | long long sec; |
---|
308 | n/a | Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t)); |
---|
309 | n/a | |
---|
310 | n/a | sec = PyLong_AsLongLong(obj); |
---|
311 | n/a | if (sec == -1 && PyErr_Occurred()) { |
---|
312 | n/a | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
---|
313 | n/a | _PyTime_overflow(); |
---|
314 | n/a | return -1; |
---|
315 | n/a | } |
---|
316 | n/a | |
---|
317 | n/a | if (_PyTime_check_mul_overflow(sec, unit_to_ns)) { |
---|
318 | n/a | _PyTime_overflow(); |
---|
319 | n/a | return -1; |
---|
320 | n/a | } |
---|
321 | n/a | *t = sec * unit_to_ns; |
---|
322 | n/a | return 0; |
---|
323 | n/a | } |
---|
324 | n/a | } |
---|
325 | n/a | |
---|
326 | n/a | int |
---|
327 | n/a | _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) |
---|
328 | n/a | { |
---|
329 | n/a | return _PyTime_FromObject(t, obj, round, SEC_TO_NS); |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | int |
---|
333 | n/a | _PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) |
---|
334 | n/a | { |
---|
335 | n/a | return _PyTime_FromObject(t, obj, round, MS_TO_NS); |
---|
336 | n/a | } |
---|
337 | n/a | |
---|
338 | n/a | double |
---|
339 | n/a | _PyTime_AsSecondsDouble(_PyTime_t t) |
---|
340 | n/a | { |
---|
341 | n/a | /* volatile avoids optimization changing how numbers are rounded */ |
---|
342 | n/a | volatile double d; |
---|
343 | n/a | |
---|
344 | n/a | if (t % SEC_TO_NS == 0) { |
---|
345 | n/a | _PyTime_t secs; |
---|
346 | n/a | /* Divide using integers to avoid rounding issues on the integer part. |
---|
347 | n/a | 1e-9 cannot be stored exactly in IEEE 64-bit. */ |
---|
348 | n/a | secs = t / SEC_TO_NS; |
---|
349 | n/a | d = (double)secs; |
---|
350 | n/a | } |
---|
351 | n/a | else { |
---|
352 | n/a | d = (double)t; |
---|
353 | n/a | d /= 1e9; |
---|
354 | n/a | } |
---|
355 | n/a | return d; |
---|
356 | n/a | } |
---|
357 | n/a | |
---|
358 | n/a | PyObject * |
---|
359 | n/a | _PyTime_AsNanosecondsObject(_PyTime_t t) |
---|
360 | n/a | { |
---|
361 | n/a | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t)); |
---|
362 | n/a | return PyLong_FromLongLong((long long)t); |
---|
363 | n/a | } |
---|
364 | n/a | |
---|
365 | n/a | static _PyTime_t |
---|
366 | n/a | _PyTime_Divide(const _PyTime_t t, const _PyTime_t k, |
---|
367 | n/a | const _PyTime_round_t round) |
---|
368 | n/a | { |
---|
369 | n/a | assert(k > 1); |
---|
370 | n/a | if (round == _PyTime_ROUND_HALF_EVEN) { |
---|
371 | n/a | _PyTime_t x, r, abs_r; |
---|
372 | n/a | x = t / k; |
---|
373 | n/a | r = t % k; |
---|
374 | n/a | abs_r = Py_ABS(r); |
---|
375 | n/a | if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) { |
---|
376 | n/a | if (t >= 0) |
---|
377 | n/a | x++; |
---|
378 | n/a | else |
---|
379 | n/a | x--; |
---|
380 | n/a | } |
---|
381 | n/a | return x; |
---|
382 | n/a | } |
---|
383 | n/a | else if (round == _PyTime_ROUND_CEILING) { |
---|
384 | n/a | if (t >= 0) |
---|
385 | n/a | return (t + k - 1) / k; |
---|
386 | n/a | else |
---|
387 | n/a | return t / k; |
---|
388 | n/a | } |
---|
389 | n/a | else { |
---|
390 | n/a | if (t >= 0) |
---|
391 | n/a | return t / k; |
---|
392 | n/a | else |
---|
393 | n/a | return (t - (k - 1)) / k; |
---|
394 | n/a | } |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | _PyTime_t |
---|
398 | n/a | _PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round) |
---|
399 | n/a | { |
---|
400 | n/a | return _PyTime_Divide(t, NS_TO_MS, round); |
---|
401 | n/a | } |
---|
402 | n/a | |
---|
403 | n/a | _PyTime_t |
---|
404 | n/a | _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round) |
---|
405 | n/a | { |
---|
406 | n/a | return _PyTime_Divide(t, NS_TO_US, round); |
---|
407 | n/a | } |
---|
408 | n/a | |
---|
409 | n/a | static int |
---|
410 | n/a | _PyTime_AsTimeval_impl(_PyTime_t t, _PyTime_t *p_secs, int *p_us, |
---|
411 | n/a | _PyTime_round_t round) |
---|
412 | n/a | { |
---|
413 | n/a | _PyTime_t secs, ns; |
---|
414 | n/a | int usec; |
---|
415 | n/a | int res = 0; |
---|
416 | n/a | |
---|
417 | n/a | secs = t / SEC_TO_NS; |
---|
418 | n/a | ns = t % SEC_TO_NS; |
---|
419 | n/a | |
---|
420 | n/a | usec = (int)_PyTime_Divide(ns, US_TO_NS, round); |
---|
421 | n/a | if (usec < 0) { |
---|
422 | n/a | usec += SEC_TO_US; |
---|
423 | n/a | if (secs != _PyTime_MIN) |
---|
424 | n/a | secs -= 1; |
---|
425 | n/a | else |
---|
426 | n/a | res = -1; |
---|
427 | n/a | } |
---|
428 | n/a | else if (usec >= SEC_TO_US) { |
---|
429 | n/a | usec -= SEC_TO_US; |
---|
430 | n/a | if (secs != _PyTime_MAX) |
---|
431 | n/a | secs += 1; |
---|
432 | n/a | else |
---|
433 | n/a | res = -1; |
---|
434 | n/a | } |
---|
435 | n/a | assert(0 <= usec && usec < SEC_TO_US); |
---|
436 | n/a | |
---|
437 | n/a | *p_secs = secs; |
---|
438 | n/a | *p_us = usec; |
---|
439 | n/a | |
---|
440 | n/a | return res; |
---|
441 | n/a | } |
---|
442 | n/a | |
---|
443 | n/a | static int |
---|
444 | n/a | _PyTime_AsTimevalStruct_impl(_PyTime_t t, struct timeval *tv, |
---|
445 | n/a | _PyTime_round_t round, int raise) |
---|
446 | n/a | { |
---|
447 | n/a | _PyTime_t secs, secs2; |
---|
448 | n/a | int us; |
---|
449 | n/a | int res; |
---|
450 | n/a | |
---|
451 | n/a | res = _PyTime_AsTimeval_impl(t, &secs, &us, round); |
---|
452 | n/a | |
---|
453 | n/a | #ifdef MS_WINDOWS |
---|
454 | n/a | tv->tv_sec = (long)secs; |
---|
455 | n/a | #else |
---|
456 | n/a | tv->tv_sec = secs; |
---|
457 | n/a | #endif |
---|
458 | n/a | tv->tv_usec = us; |
---|
459 | n/a | |
---|
460 | n/a | secs2 = (_PyTime_t)tv->tv_sec; |
---|
461 | n/a | if (res < 0 || secs2 != secs) { |
---|
462 | n/a | if (raise) |
---|
463 | n/a | error_time_t_overflow(); |
---|
464 | n/a | return -1; |
---|
465 | n/a | } |
---|
466 | n/a | return 0; |
---|
467 | n/a | } |
---|
468 | n/a | |
---|
469 | n/a | int |
---|
470 | n/a | _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round) |
---|
471 | n/a | { |
---|
472 | n/a | return _PyTime_AsTimevalStruct_impl(t, tv, round, 1); |
---|
473 | n/a | } |
---|
474 | n/a | |
---|
475 | n/a | int |
---|
476 | n/a | _PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round) |
---|
477 | n/a | { |
---|
478 | n/a | return _PyTime_AsTimevalStruct_impl(t, tv, round, 0); |
---|
479 | n/a | } |
---|
480 | n/a | |
---|
481 | n/a | int |
---|
482 | n/a | _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us, |
---|
483 | n/a | _PyTime_round_t round) |
---|
484 | n/a | { |
---|
485 | n/a | _PyTime_t secs; |
---|
486 | n/a | int res; |
---|
487 | n/a | |
---|
488 | n/a | res = _PyTime_AsTimeval_impl(t, &secs, us, round); |
---|
489 | n/a | |
---|
490 | n/a | *p_secs = secs; |
---|
491 | n/a | |
---|
492 | n/a | if (res < 0 || (_PyTime_t)*p_secs != secs) { |
---|
493 | n/a | error_time_t_overflow(); |
---|
494 | n/a | return -1; |
---|
495 | n/a | } |
---|
496 | n/a | return 0; |
---|
497 | n/a | } |
---|
498 | n/a | |
---|
499 | n/a | |
---|
500 | n/a | #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE) |
---|
501 | n/a | int |
---|
502 | n/a | _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts) |
---|
503 | n/a | { |
---|
504 | n/a | _PyTime_t secs, nsec; |
---|
505 | n/a | |
---|
506 | n/a | secs = t / SEC_TO_NS; |
---|
507 | n/a | nsec = t % SEC_TO_NS; |
---|
508 | n/a | if (nsec < 0) { |
---|
509 | n/a | nsec += SEC_TO_NS; |
---|
510 | n/a | secs -= 1; |
---|
511 | n/a | } |
---|
512 | n/a | ts->tv_sec = (time_t)secs; |
---|
513 | n/a | assert(0 <= nsec && nsec < SEC_TO_NS); |
---|
514 | n/a | ts->tv_nsec = nsec; |
---|
515 | n/a | |
---|
516 | n/a | if ((_PyTime_t)ts->tv_sec != secs) { |
---|
517 | n/a | error_time_t_overflow(); |
---|
518 | n/a | return -1; |
---|
519 | n/a | } |
---|
520 | n/a | return 0; |
---|
521 | n/a | } |
---|
522 | n/a | #endif |
---|
523 | n/a | |
---|
524 | n/a | static int |
---|
525 | n/a | pygettimeofday(_PyTime_t *tp, _Py_clock_info_t *info, int raise) |
---|
526 | n/a | { |
---|
527 | n/a | #ifdef MS_WINDOWS |
---|
528 | n/a | FILETIME system_time; |
---|
529 | n/a | ULARGE_INTEGER large; |
---|
530 | n/a | |
---|
531 | n/a | assert(info == NULL || raise); |
---|
532 | n/a | |
---|
533 | n/a | GetSystemTimeAsFileTime(&system_time); |
---|
534 | n/a | large.u.LowPart = system_time.dwLowDateTime; |
---|
535 | n/a | large.u.HighPart = system_time.dwHighDateTime; |
---|
536 | n/a | /* 11,644,473,600,000,000,000: number of nanoseconds between |
---|
537 | n/a | the 1st january 1601 and the 1st january 1970 (369 years + 89 leap |
---|
538 | n/a | days). */ |
---|
539 | n/a | *tp = large.QuadPart * 100 - 11644473600000000000; |
---|
540 | n/a | if (info) { |
---|
541 | n/a | DWORD timeAdjustment, timeIncrement; |
---|
542 | n/a | BOOL isTimeAdjustmentDisabled, ok; |
---|
543 | n/a | |
---|
544 | n/a | info->implementation = "GetSystemTimeAsFileTime()"; |
---|
545 | n/a | info->monotonic = 0; |
---|
546 | n/a | ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
---|
547 | n/a | &isTimeAdjustmentDisabled); |
---|
548 | n/a | if (!ok) { |
---|
549 | n/a | PyErr_SetFromWindowsErr(0); |
---|
550 | n/a | return -1; |
---|
551 | n/a | } |
---|
552 | n/a | info->resolution = timeIncrement * 1e-7; |
---|
553 | n/a | info->adjustable = 1; |
---|
554 | n/a | } |
---|
555 | n/a | |
---|
556 | n/a | #else /* MS_WINDOWS */ |
---|
557 | n/a | int err; |
---|
558 | n/a | #ifdef HAVE_CLOCK_GETTIME |
---|
559 | n/a | struct timespec ts; |
---|
560 | n/a | #else |
---|
561 | n/a | struct timeval tv; |
---|
562 | n/a | #endif |
---|
563 | n/a | |
---|
564 | n/a | assert(info == NULL || raise); |
---|
565 | n/a | |
---|
566 | n/a | #ifdef HAVE_CLOCK_GETTIME |
---|
567 | n/a | err = clock_gettime(CLOCK_REALTIME, &ts); |
---|
568 | n/a | if (err) { |
---|
569 | n/a | if (raise) |
---|
570 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
571 | n/a | return -1; |
---|
572 | n/a | } |
---|
573 | n/a | if (_PyTime_FromTimespec(tp, &ts, raise) < 0) |
---|
574 | n/a | return -1; |
---|
575 | n/a | |
---|
576 | n/a | if (info) { |
---|
577 | n/a | struct timespec res; |
---|
578 | n/a | info->implementation = "clock_gettime(CLOCK_REALTIME)"; |
---|
579 | n/a | info->monotonic = 0; |
---|
580 | n/a | info->adjustable = 1; |
---|
581 | n/a | if (clock_getres(CLOCK_REALTIME, &res) == 0) |
---|
582 | n/a | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
---|
583 | n/a | else |
---|
584 | n/a | info->resolution = 1e-9; |
---|
585 | n/a | } |
---|
586 | n/a | #else /* HAVE_CLOCK_GETTIME */ |
---|
587 | n/a | |
---|
588 | n/a | /* test gettimeofday() */ |
---|
589 | n/a | #ifdef GETTIMEOFDAY_NO_TZ |
---|
590 | n/a | err = gettimeofday(&tv); |
---|
591 | n/a | #else |
---|
592 | n/a | err = gettimeofday(&tv, (struct timezone *)NULL); |
---|
593 | n/a | #endif |
---|
594 | n/a | if (err) { |
---|
595 | n/a | if (raise) |
---|
596 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
597 | n/a | return -1; |
---|
598 | n/a | } |
---|
599 | n/a | if (_PyTime_FromTimeval(tp, &tv, raise) < 0) |
---|
600 | n/a | return -1; |
---|
601 | n/a | |
---|
602 | n/a | if (info) { |
---|
603 | n/a | info->implementation = "gettimeofday()"; |
---|
604 | n/a | info->resolution = 1e-6; |
---|
605 | n/a | info->monotonic = 0; |
---|
606 | n/a | info->adjustable = 1; |
---|
607 | n/a | } |
---|
608 | n/a | #endif /* !HAVE_CLOCK_GETTIME */ |
---|
609 | n/a | #endif /* !MS_WINDOWS */ |
---|
610 | n/a | return 0; |
---|
611 | n/a | } |
---|
612 | n/a | |
---|
613 | n/a | _PyTime_t |
---|
614 | n/a | _PyTime_GetSystemClock(void) |
---|
615 | n/a | { |
---|
616 | n/a | _PyTime_t t; |
---|
617 | n/a | if (pygettimeofday(&t, NULL, 0) < 0) { |
---|
618 | n/a | /* should not happen, _PyTime_Init() checked the clock at startup */ |
---|
619 | n/a | assert(0); |
---|
620 | n/a | |
---|
621 | n/a | /* use a fixed value instead of a random value from the stack */ |
---|
622 | n/a | t = 0; |
---|
623 | n/a | } |
---|
624 | n/a | return t; |
---|
625 | n/a | } |
---|
626 | n/a | |
---|
627 | n/a | int |
---|
628 | n/a | _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info) |
---|
629 | n/a | { |
---|
630 | n/a | return pygettimeofday(t, info, 1); |
---|
631 | n/a | } |
---|
632 | n/a | |
---|
633 | n/a | static int |
---|
634 | n/a | pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise) |
---|
635 | n/a | { |
---|
636 | n/a | #if defined(MS_WINDOWS) |
---|
637 | n/a | ULONGLONG ticks; |
---|
638 | n/a | _PyTime_t t; |
---|
639 | n/a | |
---|
640 | n/a | assert(info == NULL || raise); |
---|
641 | n/a | |
---|
642 | n/a | ticks = GetTickCount64(); |
---|
643 | n/a | Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t)); |
---|
644 | n/a | t = (_PyTime_t)ticks; |
---|
645 | n/a | |
---|
646 | n/a | if (_PyTime_check_mul_overflow(t, MS_TO_NS)) { |
---|
647 | n/a | if (raise) { |
---|
648 | n/a | _PyTime_overflow(); |
---|
649 | n/a | return -1; |
---|
650 | n/a | } |
---|
651 | n/a | /* Hello, time traveler! */ |
---|
652 | n/a | assert(0); |
---|
653 | n/a | } |
---|
654 | n/a | *tp = t * MS_TO_NS; |
---|
655 | n/a | |
---|
656 | n/a | if (info) { |
---|
657 | n/a | DWORD timeAdjustment, timeIncrement; |
---|
658 | n/a | BOOL isTimeAdjustmentDisabled, ok; |
---|
659 | n/a | info->implementation = "GetTickCount64()"; |
---|
660 | n/a | info->monotonic = 1; |
---|
661 | n/a | ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement, |
---|
662 | n/a | &isTimeAdjustmentDisabled); |
---|
663 | n/a | if (!ok) { |
---|
664 | n/a | PyErr_SetFromWindowsErr(0); |
---|
665 | n/a | return -1; |
---|
666 | n/a | } |
---|
667 | n/a | info->resolution = timeIncrement * 1e-7; |
---|
668 | n/a | info->adjustable = 0; |
---|
669 | n/a | } |
---|
670 | n/a | |
---|
671 | n/a | #elif defined(__APPLE__) |
---|
672 | n/a | static mach_timebase_info_data_t timebase; |
---|
673 | n/a | uint64_t time; |
---|
674 | n/a | |
---|
675 | n/a | if (timebase.denom == 0) { |
---|
676 | n/a | /* According to the Technical Q&A QA1398, mach_timebase_info() cannot |
---|
677 | n/a | fail: https://developer.apple.com/library/mac/#qa/qa1398/ */ |
---|
678 | n/a | (void)mach_timebase_info(&timebase); |
---|
679 | n/a | } |
---|
680 | n/a | |
---|
681 | n/a | time = mach_absolute_time(); |
---|
682 | n/a | |
---|
683 | n/a | /* apply timebase factor */ |
---|
684 | n/a | time *= timebase.numer; |
---|
685 | n/a | time /= timebase.denom; |
---|
686 | n/a | |
---|
687 | n/a | *tp = time; |
---|
688 | n/a | |
---|
689 | n/a | if (info) { |
---|
690 | n/a | info->implementation = "mach_absolute_time()"; |
---|
691 | n/a | info->resolution = (double)timebase.numer / timebase.denom * 1e-9; |
---|
692 | n/a | info->monotonic = 1; |
---|
693 | n/a | info->adjustable = 0; |
---|
694 | n/a | } |
---|
695 | n/a | |
---|
696 | n/a | #else |
---|
697 | n/a | struct timespec ts; |
---|
698 | n/a | #ifdef CLOCK_HIGHRES |
---|
699 | n/a | const clockid_t clk_id = CLOCK_HIGHRES; |
---|
700 | n/a | const char *implementation = "clock_gettime(CLOCK_HIGHRES)"; |
---|
701 | n/a | #else |
---|
702 | n/a | const clockid_t clk_id = CLOCK_MONOTONIC; |
---|
703 | n/a | const char *implementation = "clock_gettime(CLOCK_MONOTONIC)"; |
---|
704 | n/a | #endif |
---|
705 | n/a | |
---|
706 | n/a | assert(info == NULL || raise); |
---|
707 | n/a | |
---|
708 | n/a | if (clock_gettime(clk_id, &ts) != 0) { |
---|
709 | n/a | if (raise) { |
---|
710 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
711 | n/a | return -1; |
---|
712 | n/a | } |
---|
713 | n/a | return -1; |
---|
714 | n/a | } |
---|
715 | n/a | |
---|
716 | n/a | if (info) { |
---|
717 | n/a | struct timespec res; |
---|
718 | n/a | info->monotonic = 1; |
---|
719 | n/a | info->implementation = implementation; |
---|
720 | n/a | info->adjustable = 0; |
---|
721 | n/a | if (clock_getres(clk_id, &res) != 0) { |
---|
722 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
723 | n/a | return -1; |
---|
724 | n/a | } |
---|
725 | n/a | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
---|
726 | n/a | } |
---|
727 | n/a | if (_PyTime_FromTimespec(tp, &ts, raise) < 0) |
---|
728 | n/a | return -1; |
---|
729 | n/a | #endif |
---|
730 | n/a | return 0; |
---|
731 | n/a | } |
---|
732 | n/a | |
---|
733 | n/a | _PyTime_t |
---|
734 | n/a | _PyTime_GetMonotonicClock(void) |
---|
735 | n/a | { |
---|
736 | n/a | _PyTime_t t; |
---|
737 | n/a | if (pymonotonic(&t, NULL, 0) < 0) { |
---|
738 | n/a | /* should not happen, _PyTime_Init() checked that monotonic clock at |
---|
739 | n/a | startup */ |
---|
740 | n/a | assert(0); |
---|
741 | n/a | |
---|
742 | n/a | /* use a fixed value instead of a random value from the stack */ |
---|
743 | n/a | t = 0; |
---|
744 | n/a | } |
---|
745 | n/a | return t; |
---|
746 | n/a | } |
---|
747 | n/a | |
---|
748 | n/a | int |
---|
749 | n/a | _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
---|
750 | n/a | { |
---|
751 | n/a | return pymonotonic(tp, info, 1); |
---|
752 | n/a | } |
---|
753 | n/a | |
---|
754 | n/a | int |
---|
755 | n/a | _PyTime_Init(void) |
---|
756 | n/a | { |
---|
757 | n/a | _PyTime_t t; |
---|
758 | n/a | |
---|
759 | n/a | /* ensure that the system clock works */ |
---|
760 | n/a | if (_PyTime_GetSystemClockWithInfo(&t, NULL) < 0) |
---|
761 | n/a | return -1; |
---|
762 | n/a | |
---|
763 | n/a | /* ensure that the operating system provides a monotonic clock */ |
---|
764 | n/a | if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) |
---|
765 | n/a | return -1; |
---|
766 | n/a | |
---|
767 | n/a | return 0; |
---|
768 | n/a | } |
---|
769 | n/a | |
---|
770 | n/a | int |
---|
771 | n/a | _PyTime_localtime(time_t t, struct tm *tm) |
---|
772 | n/a | { |
---|
773 | n/a | #ifdef MS_WINDOWS |
---|
774 | n/a | int error; |
---|
775 | n/a | |
---|
776 | n/a | error = localtime_s(tm, &t); |
---|
777 | n/a | if (error != 0) { |
---|
778 | n/a | errno = error; |
---|
779 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
780 | n/a | return -1; |
---|
781 | n/a | } |
---|
782 | n/a | return 0; |
---|
783 | n/a | #else /* !MS_WINDOWS */ |
---|
784 | n/a | if (localtime_r(&t, tm) == NULL) { |
---|
785 | n/a | #ifdef EINVAL |
---|
786 | n/a | if (errno == 0) |
---|
787 | n/a | errno = EINVAL; |
---|
788 | n/a | #endif |
---|
789 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
790 | n/a | return -1; |
---|
791 | n/a | } |
---|
792 | n/a | return 0; |
---|
793 | n/a | #endif /* MS_WINDOWS */ |
---|
794 | n/a | } |
---|
795 | n/a | |
---|
796 | n/a | int |
---|
797 | n/a | _PyTime_gmtime(time_t t, struct tm *tm) |
---|
798 | n/a | { |
---|
799 | n/a | #ifdef MS_WINDOWS |
---|
800 | n/a | int error; |
---|
801 | n/a | |
---|
802 | n/a | error = gmtime_s(tm, &t); |
---|
803 | n/a | if (error != 0) { |
---|
804 | n/a | errno = error; |
---|
805 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
806 | n/a | return -1; |
---|
807 | n/a | } |
---|
808 | n/a | return 0; |
---|
809 | n/a | #else /* !MS_WINDOWS */ |
---|
810 | n/a | if (gmtime_r(&t, tm) == NULL) { |
---|
811 | n/a | #ifdef EINVAL |
---|
812 | n/a | if (errno == 0) |
---|
813 | n/a | errno = EINVAL; |
---|
814 | n/a | #endif |
---|
815 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
816 | n/a | return -1; |
---|
817 | n/a | } |
---|
818 | n/a | return 0; |
---|
819 | n/a | #endif /* MS_WINDOWS */ |
---|
820 | n/a | } |
---|