Branch data Line data Source code
1 : : #include "Python.h"
2 : : #ifdef MS_WINDOWS
3 : : # include <winsock2.h> // struct timeval
4 : : #endif
5 : :
6 : : #if defined(__APPLE__)
7 : : # include <mach/mach_time.h> // mach_absolute_time(), mach_timebase_info()
8 : :
9 : : #if defined(__APPLE__) && defined(__has_builtin)
10 : : # if __has_builtin(__builtin_available)
11 : : # define HAVE_CLOCK_GETTIME_RUNTIME __builtin_available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
12 : : # endif
13 : : #endif
14 : : #endif
15 : :
16 : : /* To millisecond (10^-3) */
17 : : #define SEC_TO_MS 1000
18 : :
19 : : /* To microseconds (10^-6) */
20 : : #define MS_TO_US 1000
21 : : #define SEC_TO_US (SEC_TO_MS * MS_TO_US)
22 : :
23 : : /* To nanoseconds (10^-9) */
24 : : #define US_TO_NS 1000
25 : : #define MS_TO_NS (MS_TO_US * US_TO_NS)
26 : : #define SEC_TO_NS (SEC_TO_MS * MS_TO_NS)
27 : :
28 : : /* Conversion from nanoseconds */
29 : : #define NS_TO_MS (1000 * 1000)
30 : : #define NS_TO_US (1000)
31 : : #define NS_TO_100NS (100)
32 : :
33 : : #if SIZEOF_TIME_T == SIZEOF_LONG_LONG
34 : : # define PY_TIME_T_MAX LLONG_MAX
35 : : # define PY_TIME_T_MIN LLONG_MIN
36 : : #elif SIZEOF_TIME_T == SIZEOF_LONG
37 : : # define PY_TIME_T_MAX LONG_MAX
38 : : # define PY_TIME_T_MIN LONG_MIN
39 : : #else
40 : : # error "unsupported time_t size"
41 : : #endif
42 : :
43 : : #if PY_TIME_T_MAX + PY_TIME_T_MIN != -1
44 : : # error "time_t is not a two's complement integer type"
45 : : #endif
46 : :
47 : : #if _PyTime_MIN + _PyTime_MAX != -1
48 : : # error "_PyTime_t is not a two's complement integer type"
49 : : #endif
50 : :
51 : :
52 : : static void
53 : 0 : pytime_time_t_overflow(void)
54 : : {
55 : 0 : PyErr_SetString(PyExc_OverflowError,
56 : : "timestamp out of range for platform time_t");
57 : 0 : }
58 : :
59 : :
60 : : static void
61 : 0 : pytime_overflow(void)
62 : : {
63 : 0 : PyErr_SetString(PyExc_OverflowError,
64 : : "timestamp too large to convert to C _PyTime_t");
65 : 0 : }
66 : :
67 : :
68 : : static inline _PyTime_t
69 : 34731 : pytime_from_nanoseconds(_PyTime_t t)
70 : : {
71 : : // _PyTime_t is a number of nanoseconds
72 : 34731 : return t;
73 : : }
74 : :
75 : :
76 : : static inline _PyTime_t
77 : 18736 : pytime_as_nanoseconds(_PyTime_t t)
78 : : {
79 : : // _PyTime_t is a number of nanoseconds: see pytime_from_nanoseconds()
80 : 18736 : return t;
81 : : }
82 : :
83 : :
84 : : // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
85 : : static inline int
86 : 32119 : pytime_add(_PyTime_t *t1, _PyTime_t t2)
87 : : {
88 [ + + - + ]: 32119 : if (t2 > 0 && *t1 > _PyTime_MAX - t2) {
89 : 0 : *t1 = _PyTime_MAX;
90 : 0 : return -1;
91 : : }
92 [ + + - + ]: 32119 : else if (t2 < 0 && *t1 < _PyTime_MIN - t2) {
93 : 0 : *t1 = _PyTime_MIN;
94 : 0 : return -1;
95 : : }
96 : : else {
97 : 32119 : *t1 += t2;
98 : 32119 : return 0;
99 : : }
100 : : }
101 : :
102 : :
103 : : _PyTime_t
104 : 16014 : _PyTime_Add(_PyTime_t t1, _PyTime_t t2)
105 : : {
106 : 16014 : (void)pytime_add(&t1, t2);
107 : 16014 : return t1;
108 : : }
109 : :
110 : :
111 : : static inline int
112 : 31816 : pytime_mul_check_overflow(_PyTime_t a, _PyTime_t b)
113 : : {
114 [ + - ]: 31816 : if (b != 0) {
115 : : assert(b > 0);
116 [ + - - + ]: 31816 : return ((a < _PyTime_MIN / b) || (_PyTime_MAX / b < a));
117 : : }
118 : : else {
119 : 0 : return 0;
120 : : }
121 : : }
122 : :
123 : :
124 : : // Compute t * k. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
125 : : static inline int
126 : 31816 : pytime_mul(_PyTime_t *t, _PyTime_t k)
127 : : {
128 : : assert(k >= 0);
129 [ - + ]: 31816 : if (pytime_mul_check_overflow(*t, k)) {
130 [ # # ]: 0 : *t = (*t >= 0) ? _PyTime_MAX : _PyTime_MIN;
131 : 0 : return -1;
132 : : }
133 : : else {
134 : 31816 : *t *= k;
135 : 31816 : return 0;
136 : : }
137 : : }
138 : :
139 : :
140 : : // Compute t * k. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow.
141 : : static inline _PyTime_t
142 : 15711 : _PyTime_Mul(_PyTime_t t, _PyTime_t k)
143 : : {
144 : 15711 : (void)pytime_mul(&t, k);
145 : 15711 : return t;
146 : : }
147 : :
148 : :
149 : :
150 : :
151 : : _PyTime_t
152 : 0 : _PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
153 : : {
154 : : /* Compute (ticks * mul / div) in two parts to reduce the risk of integer
155 : : overflow: compute the integer part, and then the remaining part.
156 : :
157 : : (ticks * mul) / div == (ticks / div) * mul + (ticks % div) * mul / div
158 : : */
159 : : _PyTime_t intpart, remaining;
160 : 0 : intpart = ticks / div;
161 : 0 : ticks %= div;
162 : 0 : remaining = _PyTime_Mul(ticks, mul) / div;
163 : : // intpart * mul + remaining
164 : 0 : return _PyTime_Add(_PyTime_Mul(intpart, mul), remaining);
165 : : }
166 : :
167 : :
168 : : time_t
169 : 0 : _PyLong_AsTime_t(PyObject *obj)
170 : : {
171 : : #if SIZEOF_TIME_T == SIZEOF_LONG_LONG
172 : 0 : long long val = PyLong_AsLongLong(obj);
173 : : #elif SIZEOF_TIME_T <= SIZEOF_LONG
174 : : long val = PyLong_AsLong(obj);
175 : : #else
176 : : # error "unsupported time_t size"
177 : : #endif
178 [ # # # # ]: 0 : if (val == -1 && PyErr_Occurred()) {
179 [ # # ]: 0 : if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
180 : 0 : pytime_time_t_overflow();
181 : : }
182 : 0 : return -1;
183 : : }
184 : 0 : return (time_t)val;
185 : : }
186 : :
187 : :
188 : : PyObject *
189 : 8349 : _PyLong_FromTime_t(time_t t)
190 : : {
191 : : #if SIZEOF_TIME_T == SIZEOF_LONG_LONG
192 : 8349 : return PyLong_FromLongLong((long long)t);
193 : : #elif SIZEOF_TIME_T <= SIZEOF_LONG
194 : : return PyLong_FromLong((long)t);
195 : : #else
196 : : # error "unsupported time_t size"
197 : : #endif
198 : : }
199 : :
200 : :
201 : : // Convert _PyTime_t to time_t.
202 : : // Return 0 on success. Return -1 and clamp the value on overflow.
203 : : static int
204 : 16014 : _PyTime_AsTime_t(_PyTime_t t, time_t *t2)
205 : : {
206 : : #if SIZEOF_TIME_T < _SIZEOF_PYTIME_T
207 : : if ((_PyTime_t)PY_TIME_T_MAX < t) {
208 : : *t2 = PY_TIME_T_MAX;
209 : : return -1;
210 : : }
211 : : if (t < (_PyTime_t)PY_TIME_T_MIN) {
212 : : *t2 = PY_TIME_T_MIN;
213 : : return -1;
214 : : }
215 : : #endif
216 : 16014 : *t2 = (time_t)t;
217 : 16014 : return 0;
218 : : }
219 : :
220 : :
221 : : #ifdef MS_WINDOWS
222 : : // Convert _PyTime_t to long.
223 : : // Return 0 on success. Return -1 and clamp the value on overflow.
224 : : static int
225 : : _PyTime_AsLong(_PyTime_t t, long *t2)
226 : : {
227 : : #if SIZEOF_LONG < _SIZEOF_PYTIME_T
228 : : if ((_PyTime_t)LONG_MAX < t) {
229 : : *t2 = LONG_MAX;
230 : : return -1;
231 : : }
232 : : if (t < (_PyTime_t)LONG_MIN) {
233 : : *t2 = LONG_MIN;
234 : : return -1;
235 : : }
236 : : #endif
237 : : *t2 = (long)t;
238 : : return 0;
239 : : }
240 : : #endif
241 : :
242 : :
243 : : /* Round to nearest with ties going to nearest even integer
244 : : (_PyTime_ROUND_HALF_EVEN) */
245 : : static double
246 : 0 : pytime_round_half_even(double x)
247 : : {
248 : 0 : double rounded = round(x);
249 [ # # ]: 0 : if (fabs(x-rounded) == 0.5) {
250 : : /* halfway case: round to even */
251 : 0 : rounded = 2.0 * round(x / 2.0);
252 : : }
253 : 0 : return rounded;
254 : : }
255 : :
256 : :
257 : : static double
258 : 1 : pytime_round(double x, _PyTime_round_t round)
259 : : {
260 : : /* volatile avoids optimization changing how numbers are rounded */
261 : : volatile double d;
262 : :
263 : 1 : d = x;
264 [ - + ]: 1 : if (round == _PyTime_ROUND_HALF_EVEN) {
265 : 0 : d = pytime_round_half_even(d);
266 : : }
267 [ - + ]: 1 : else if (round == _PyTime_ROUND_CEILING) {
268 : 0 : d = ceil(d);
269 : : }
270 [ - + ]: 1 : else if (round == _PyTime_ROUND_FLOOR) {
271 : 0 : d = floor(d);
272 : : }
273 : : else {
274 : : assert(round == _PyTime_ROUND_UP);
275 [ + - ]: 1 : d = (d >= 0.0) ? ceil(d) : floor(d);
276 : : }
277 : 1 : return d;
278 : : }
279 : :
280 : :
281 : : static int
282 : 0 : pytime_double_to_denominator(double d, time_t *sec, long *numerator,
283 : : long idenominator, _PyTime_round_t round)
284 : : {
285 : 0 : double denominator = (double)idenominator;
286 : : double intpart;
287 : : /* volatile avoids optimization changing how numbers are rounded */
288 : : volatile double floatpart;
289 : :
290 : 0 : floatpart = modf(d, &intpart);
291 : :
292 : 0 : floatpart *= denominator;
293 : 0 : floatpart = pytime_round(floatpart, round);
294 [ # # ]: 0 : if (floatpart >= denominator) {
295 : 0 : floatpart -= denominator;
296 : 0 : intpart += 1.0;
297 : : }
298 [ # # ]: 0 : else if (floatpart < 0) {
299 : 0 : floatpart += denominator;
300 : 0 : intpart -= 1.0;
301 : : }
302 : : assert(0.0 <= floatpart && floatpart < denominator);
303 : :
304 : : /*
305 : : Conversion of an out-of-range value to time_t gives undefined behaviour
306 : : (C99 ยง6.3.1.4p1), so we must guard against it. However, checking that
307 : : `intpart` is in range is delicate: the obvious expression `intpart <=
308 : : PY_TIME_T_MAX` will first convert the value `PY_TIME_T_MAX` to a double,
309 : : potentially changing its value and leading to us failing to catch some
310 : : UB-inducing values. The code below works correctly under the mild
311 : : assumption that time_t is a two's complement integer type with no trap
312 : : representation, and that `PY_TIME_T_MIN` is within the representable
313 : : range of a C double.
314 : :
315 : : Note: we want the `if` condition below to be true for NaNs; therefore,
316 : : resist any temptation to simplify by applying De Morgan's laws.
317 : : */
318 [ # # # # ]: 0 : if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
319 : 0 : pytime_time_t_overflow();
320 : 0 : return -1;
321 : : }
322 : 0 : *sec = (time_t)intpart;
323 : 0 : *numerator = (long)floatpart;
324 : : assert(0 <= *numerator && *numerator < idenominator);
325 : 0 : return 0;
326 : : }
327 : :
328 : :
329 : : static int
330 : 0 : pytime_object_to_denominator(PyObject *obj, time_t *sec, long *numerator,
331 : : long denominator, _PyTime_round_t round)
332 : : {
333 : : assert(denominator >= 1);
334 : :
335 [ # # ]: 0 : if (PyFloat_Check(obj)) {
336 : 0 : double d = PyFloat_AsDouble(obj);
337 [ # # ]: 0 : if (Py_IS_NAN(d)) {
338 : 0 : *numerator = 0;
339 : 0 : PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
340 : 0 : return -1;
341 : : }
342 : 0 : return pytime_double_to_denominator(d, sec, numerator,
343 : : denominator, round);
344 : : }
345 : : else {
346 : 0 : *sec = _PyLong_AsTime_t(obj);
347 : 0 : *numerator = 0;
348 [ # # # # ]: 0 : if (*sec == (time_t)-1 && PyErr_Occurred()) {
349 : 0 : return -1;
350 : : }
351 : 0 : return 0;
352 : : }
353 : : }
354 : :
355 : :
356 : : int
357 : 0 : _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
358 : : {
359 [ # # ]: 0 : if (PyFloat_Check(obj)) {
360 : : double intpart;
361 : : /* volatile avoids optimization changing how numbers are rounded */
362 : : volatile double d;
363 : :
364 : 0 : d = PyFloat_AsDouble(obj);
365 [ # # ]: 0 : if (Py_IS_NAN(d)) {
366 : 0 : PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
367 : 0 : return -1;
368 : : }
369 : :
370 : 0 : d = pytime_round(d, round);
371 : 0 : (void)modf(d, &intpart);
372 : :
373 : : /* See comments in pytime_double_to_denominator */
374 [ # # # # ]: 0 : if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
375 : 0 : pytime_time_t_overflow();
376 : 0 : return -1;
377 : : }
378 : 0 : *sec = (time_t)intpart;
379 : 0 : return 0;
380 : : }
381 : : else {
382 : 0 : *sec = _PyLong_AsTime_t(obj);
383 [ # # # # ]: 0 : if (*sec == (time_t)-1 && PyErr_Occurred()) {
384 : 0 : return -1;
385 : : }
386 : 0 : return 0;
387 : : }
388 : : }
389 : :
390 : :
391 : : int
392 : 0 : _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
393 : : _PyTime_round_t round)
394 : : {
395 : 0 : return pytime_object_to_denominator(obj, sec, nsec, SEC_TO_NS, round);
396 : : }
397 : :
398 : :
399 : : int
400 : 0 : _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
401 : : _PyTime_round_t round)
402 : : {
403 : 0 : return pytime_object_to_denominator(obj, sec, usec, SEC_TO_US, round);
404 : : }
405 : :
406 : :
407 : : _PyTime_t
408 : 2611 : _PyTime_FromSeconds(int seconds)
409 : : {
410 : : /* ensure that integer overflow cannot happen, int type should have 32
411 : : bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_NS takes 30
412 : : bits). */
413 : : static_assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS, "_PyTime_t overflow");
414 : : static_assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS, "_PyTime_t underflow");
415 : :
416 : 2611 : _PyTime_t t = (_PyTime_t)seconds;
417 : : assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
418 : : || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
419 : 2611 : t *= SEC_TO_NS;
420 : 2611 : return pytime_from_nanoseconds(t);
421 : : }
422 : :
423 : :
424 : : _PyTime_t
425 : 303 : _PyTime_FromNanoseconds(_PyTime_t ns)
426 : : {
427 : 303 : return pytime_from_nanoseconds(ns);
428 : : }
429 : :
430 : :
431 : : _PyTime_t
432 : 15711 : _PyTime_FromMicrosecondsClamp(_PyTime_t us)
433 : : {
434 : 15711 : _PyTime_t ns = _PyTime_Mul(us, US_TO_NS);
435 : 15711 : return pytime_from_nanoseconds(ns);
436 : : }
437 : :
438 : :
439 : : int
440 : 0 : _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
441 : : {
442 : :
443 [ # # ]: 0 : if (!PyLong_Check(obj)) {
444 : 0 : PyErr_Format(PyExc_TypeError, "expect int, got %s",
445 : 0 : Py_TYPE(obj)->tp_name);
446 : 0 : return -1;
447 : : }
448 : :
449 : : static_assert(sizeof(long long) == sizeof(_PyTime_t),
450 : : "_PyTime_t is not long long");
451 : 0 : long long nsec = PyLong_AsLongLong(obj);
452 [ # # # # ]: 0 : if (nsec == -1 && PyErr_Occurred()) {
453 [ # # ]: 0 : if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
454 : 0 : pytime_overflow();
455 : : }
456 : 0 : return -1;
457 : : }
458 : :
459 : 0 : _PyTime_t t = (_PyTime_t)nsec;
460 : 0 : *tp = pytime_from_nanoseconds(t);
461 : 0 : return 0;
462 : : }
463 : :
464 : :
465 : : #ifdef HAVE_CLOCK_GETTIME
466 : : static int
467 : 16105 : pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise_exc)
468 : : {
469 : : _PyTime_t t, tv_nsec;
470 : :
471 : : static_assert(sizeof(ts->tv_sec) <= sizeof(_PyTime_t),
472 : : "timespec.tv_sec is larger than _PyTime_t");
473 : 16105 : t = (_PyTime_t)ts->tv_sec;
474 : :
475 : 16105 : int res1 = pytime_mul(&t, SEC_TO_NS);
476 : :
477 : 16105 : tv_nsec = ts->tv_nsec;
478 : 16105 : int res2 = pytime_add(&t, tv_nsec);
479 : :
480 : 16105 : *tp = pytime_from_nanoseconds(t);
481 : :
482 [ + + + - : 16105 : if (raise_exc && (res1 < 0 || res2 < 0)) {
- + ]
483 : 0 : pytime_overflow();
484 : 0 : return -1;
485 : : }
486 : 16105 : return 0;
487 : : }
488 : :
489 : : int
490 : 0 : _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
491 : : {
492 : 0 : return pytime_fromtimespec(tp, ts, 1);
493 : : }
494 : : #endif
495 : :
496 : :
497 : : #ifndef MS_WINDOWS
498 : : static int
499 : 0 : pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise_exc)
500 : : {
501 : : static_assert(sizeof(tv->tv_sec) <= sizeof(_PyTime_t),
502 : : "timeval.tv_sec is larger than _PyTime_t");
503 : 0 : _PyTime_t t = (_PyTime_t)tv->tv_sec;
504 : :
505 : 0 : int res1 = pytime_mul(&t, SEC_TO_NS);
506 : :
507 : 0 : _PyTime_t usec = (_PyTime_t)tv->tv_usec * US_TO_NS;
508 : 0 : int res2 = pytime_add(&t, usec);
509 : :
510 : 0 : *tp = pytime_from_nanoseconds(t);
511 : :
512 [ # # # # : 0 : if (raise_exc && (res1 < 0 || res2 < 0)) {
# # ]
513 : 0 : pytime_overflow();
514 : 0 : return -1;
515 : : }
516 : 0 : return 0;
517 : : }
518 : :
519 : :
520 : : int
521 : 0 : _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv)
522 : : {
523 : 0 : return pytime_fromtimeval(tp, tv, 1);
524 : : }
525 : : #endif
526 : :
527 : :
528 : : static int
529 : 1 : pytime_from_double(_PyTime_t *tp, double value, _PyTime_round_t round,
530 : : long unit_to_ns)
531 : : {
532 : : /* volatile avoids optimization changing how numbers are rounded */
533 : : volatile double d;
534 : :
535 : : /* convert to a number of nanoseconds */
536 : 1 : d = value;
537 : 1 : d *= (double)unit_to_ns;
538 : 1 : d = pytime_round(d, round);
539 : :
540 : : /* See comments in pytime_double_to_denominator */
541 [ + - - + ]: 1 : if (!((double)_PyTime_MIN <= d && d < -(double)_PyTime_MIN)) {
542 : 0 : pytime_time_t_overflow();
543 : 0 : return -1;
544 : : }
545 : 1 : _PyTime_t ns = (_PyTime_t)d;
546 : :
547 : 1 : *tp = pytime_from_nanoseconds(ns);
548 : 1 : return 0;
549 : : }
550 : :
551 : :
552 : : static int
553 : 1 : pytime_from_object(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
554 : : long unit_to_ns)
555 : : {
556 [ + - ]: 1 : if (PyFloat_Check(obj)) {
557 : : double d;
558 : 1 : d = PyFloat_AsDouble(obj);
559 [ - + ]: 1 : if (Py_IS_NAN(d)) {
560 : 0 : PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
561 : 0 : return -1;
562 : : }
563 : 1 : return pytime_from_double(tp, d, round, unit_to_ns);
564 : : }
565 : : else {
566 : 0 : long long sec = PyLong_AsLongLong(obj);
567 [ # # # # ]: 0 : if (sec == -1 && PyErr_Occurred()) {
568 [ # # ]: 0 : if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
569 : 0 : pytime_overflow();
570 : : }
571 : 0 : return -1;
572 : : }
573 : :
574 : : static_assert(sizeof(long long) <= sizeof(_PyTime_t),
575 : : "_PyTime_t is smaller than long long");
576 : 0 : _PyTime_t ns = (_PyTime_t)sec;
577 [ # # ]: 0 : if (pytime_mul(&ns, unit_to_ns) < 0) {
578 : 0 : pytime_overflow();
579 : 0 : return -1;
580 : : }
581 : :
582 : 0 : *tp = pytime_from_nanoseconds(ns);
583 : 0 : return 0;
584 : : }
585 : : }
586 : :
587 : :
588 : : int
589 : 1 : _PyTime_FromSecondsObject(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
590 : : {
591 : 1 : return pytime_from_object(tp, obj, round, SEC_TO_NS);
592 : : }
593 : :
594 : :
595 : : int
596 : 0 : _PyTime_FromMillisecondsObject(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round)
597 : : {
598 : 0 : return pytime_from_object(tp, obj, round, MS_TO_NS);
599 : : }
600 : :
601 : :
602 : : double
603 : 116 : _PyTime_AsSecondsDouble(_PyTime_t t)
604 : : {
605 : : /* volatile avoids optimization changing how numbers are rounded */
606 : : volatile double d;
607 : :
608 : 116 : _PyTime_t ns = pytime_as_nanoseconds(t);
609 [ - + ]: 116 : if (ns % SEC_TO_NS == 0) {
610 : : /* Divide using integers to avoid rounding issues on the integer part.
611 : : 1e-9 cannot be stored exactly in IEEE 64-bit. */
612 : 0 : _PyTime_t secs = ns / SEC_TO_NS;
613 : 0 : d = (double)secs;
614 : : }
615 : : else {
616 : 116 : d = (double)ns;
617 : 116 : d /= 1e9;
618 : : }
619 : 116 : return d;
620 : : }
621 : :
622 : :
623 : : PyObject *
624 : 0 : _PyTime_AsNanosecondsObject(_PyTime_t t)
625 : : {
626 : 0 : _PyTime_t ns = pytime_as_nanoseconds(t);
627 : : static_assert(sizeof(long long) >= sizeof(_PyTime_t),
628 : : "_PyTime_t is larger than long long");
629 : 0 : return PyLong_FromLongLong((long long)ns);
630 : : }
631 : :
632 : :
633 : : static _PyTime_t
634 : 5 : pytime_divide_round_up(const _PyTime_t t, const _PyTime_t k)
635 : : {
636 : : assert(k > 1);
637 [ + - ]: 5 : if (t >= 0) {
638 : : // Don't use (t + k - 1) / k to avoid integer overflow
639 : : // if t is equal to _PyTime_MAX
640 : 5 : _PyTime_t q = t / k;
641 [ - + ]: 5 : if (t % k) {
642 : 0 : q += 1;
643 : : }
644 : 5 : return q;
645 : : }
646 : : else {
647 : : // Don't use (t - (k - 1)) / k to avoid integer overflow
648 : : // if t is equals to _PyTime_MIN.
649 : 0 : _PyTime_t q = t / k;
650 [ # # ]: 0 : if (t % k) {
651 : 0 : q -= 1;
652 : : }
653 : 0 : return q;
654 : : }
655 : : }
656 : :
657 : :
658 : : static _PyTime_t
659 : 2606 : pytime_divide(const _PyTime_t t, const _PyTime_t k,
660 : : const _PyTime_round_t round)
661 : : {
662 : : assert(k > 1);
663 [ - + ]: 2606 : if (round == _PyTime_ROUND_HALF_EVEN) {
664 : 0 : _PyTime_t x = t / k;
665 : 0 : _PyTime_t r = t % k;
666 : 0 : _PyTime_t abs_r = Py_ABS(r);
667 [ # # # # : 0 : if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) {
# # ]
668 [ # # ]: 0 : if (t >= 0) {
669 : 0 : x++;
670 : : }
671 : : else {
672 : 0 : x--;
673 : : }
674 : : }
675 : 0 : return x;
676 : : }
677 [ + + ]: 2606 : else if (round == _PyTime_ROUND_CEILING) {
678 [ + + ]: 2605 : if (t >= 0) {
679 : 4 : return pytime_divide_round_up(t, k);
680 : : }
681 : : else {
682 : 2601 : return t / k;
683 : : }
684 : : }
685 [ - + ]: 1 : else if (round == _PyTime_ROUND_FLOOR){
686 [ # # ]: 0 : if (t >= 0) {
687 : 0 : return t / k;
688 : : }
689 : : else {
690 : 0 : return pytime_divide_round_up(t, k);
691 : : }
692 : : }
693 : : else {
694 : : assert(round == _PyTime_ROUND_UP);
695 : 1 : return pytime_divide_round_up(t, k);
696 : : }
697 : : }
698 : :
699 : :
700 : : // Compute (t / k, t % k) in (pq, pr).
701 : : // Make sure that 0 <= pr < k.
702 : : // Return 0 on success.
703 : : // Return -1 on underflow and store (_PyTime_MIN, 0) in (pq, pr).
704 : : static int
705 : 16014 : pytime_divmod(const _PyTime_t t, const _PyTime_t k,
706 : : _PyTime_t *pq, _PyTime_t *pr)
707 : : {
708 : : assert(k > 1);
709 : 16014 : _PyTime_t q = t / k;
710 : 16014 : _PyTime_t r = t % k;
711 [ - + ]: 16014 : if (r < 0) {
712 [ # # ]: 0 : if (q == _PyTime_MIN) {
713 : 0 : *pq = _PyTime_MIN;
714 : 0 : *pr = 0;
715 : 0 : return -1;
716 : : }
717 : 0 : r += k;
718 : 0 : q -= 1;
719 : : }
720 : : assert(0 <= r && r < k);
721 : :
722 : 16014 : *pq = q;
723 : 16014 : *pr = r;
724 : 16014 : return 0;
725 : : }
726 : :
727 : :
728 : : _PyTime_t
729 : 0 : _PyTime_AsNanoseconds(_PyTime_t t)
730 : : {
731 : 0 : return pytime_as_nanoseconds(t);
732 : : }
733 : :
734 : :
735 : : #ifdef MS_WINDOWS
736 : : _PyTime_t
737 : : _PyTime_As100Nanoseconds(_PyTime_t t, _PyTime_round_t round)
738 : : {
739 : : _PyTime_t ns = pytime_as_nanoseconds(t);
740 : : return pytime_divide(ns, NS_TO_100NS, round);
741 : : }
742 : : #endif
743 : :
744 : :
745 : : _PyTime_t
746 : 2606 : _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
747 : : {
748 : 2606 : _PyTime_t ns = pytime_as_nanoseconds(t);
749 : 2606 : return pytime_divide(ns, NS_TO_US, round);
750 : : }
751 : :
752 : :
753 : : _PyTime_t
754 : 0 : _PyTime_AsMilliseconds(_PyTime_t t, _PyTime_round_t round)
755 : : {
756 : 0 : _PyTime_t ns = pytime_as_nanoseconds(t);
757 : 0 : return pytime_divide(ns, NS_TO_MS, round);
758 : : }
759 : :
760 : :
761 : : static int
762 : 0 : pytime_as_timeval(_PyTime_t t, _PyTime_t *ptv_sec, int *ptv_usec,
763 : : _PyTime_round_t round)
764 : : {
765 : 0 : _PyTime_t ns = pytime_as_nanoseconds(t);
766 : 0 : _PyTime_t us = pytime_divide(ns, US_TO_NS, round);
767 : :
768 : : _PyTime_t tv_sec, tv_usec;
769 : 0 : int res = pytime_divmod(us, SEC_TO_US, &tv_sec, &tv_usec);
770 : 0 : *ptv_sec = tv_sec;
771 : 0 : *ptv_usec = (int)tv_usec;
772 : 0 : return res;
773 : : }
774 : :
775 : :
776 : : static int
777 : 0 : pytime_as_timeval_struct(_PyTime_t t, struct timeval *tv,
778 : : _PyTime_round_t round, int raise_exc)
779 : : {
780 : : _PyTime_t tv_sec;
781 : : int tv_usec;
782 : 0 : int res = pytime_as_timeval(t, &tv_sec, &tv_usec, round);
783 : : int res2;
784 : : #ifdef MS_WINDOWS
785 : : // On Windows, timeval.tv_sec type is long
786 : : res2 = _PyTime_AsLong(tv_sec, &tv->tv_sec);
787 : : #else
788 : 0 : res2 = _PyTime_AsTime_t(tv_sec, &tv->tv_sec);
789 : : #endif
790 [ # # ]: 0 : if (res2 < 0) {
791 : 0 : tv_usec = 0;
792 : : }
793 : 0 : tv->tv_usec = tv_usec;
794 : :
795 [ # # # # : 0 : if (raise_exc && (res < 0 || res2 < 0)) {
# # ]
796 : 0 : pytime_time_t_overflow();
797 : 0 : return -1;
798 : : }
799 : 0 : return 0;
800 : : }
801 : :
802 : :
803 : : int
804 : 0 : _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
805 : : {
806 : 0 : return pytime_as_timeval_struct(t, tv, round, 1);
807 : : }
808 : :
809 : :
810 : : void
811 : 0 : _PyTime_AsTimeval_clamp(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
812 : : {
813 : 0 : (void)pytime_as_timeval_struct(t, tv, round, 0);
814 : 0 : }
815 : :
816 : :
817 : : int
818 : 0 : _PyTime_AsTimevalTime_t(_PyTime_t t, time_t *p_secs, int *us,
819 : : _PyTime_round_t round)
820 : : {
821 : : _PyTime_t secs;
822 [ # # ]: 0 : if (pytime_as_timeval(t, &secs, us, round) < 0) {
823 : 0 : pytime_time_t_overflow();
824 : 0 : return -1;
825 : : }
826 : :
827 [ # # ]: 0 : if (_PyTime_AsTime_t(secs, p_secs) < 0) {
828 : 0 : pytime_time_t_overflow();
829 : 0 : return -1;
830 : : }
831 : 0 : return 0;
832 : : }
833 : :
834 : :
835 : : #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
836 : : static int
837 : 16014 : pytime_as_timespec(_PyTime_t t, struct timespec *ts, int raise_exc)
838 : : {
839 : 16014 : _PyTime_t ns = pytime_as_nanoseconds(t);
840 : : _PyTime_t tv_sec, tv_nsec;
841 : 16014 : int res = pytime_divmod(ns, SEC_TO_NS, &tv_sec, &tv_nsec);
842 : :
843 : 16014 : int res2 = _PyTime_AsTime_t(tv_sec, &ts->tv_sec);
844 [ - + ]: 16014 : if (res2 < 0) {
845 : 0 : tv_nsec = 0;
846 : : }
847 : 16014 : ts->tv_nsec = tv_nsec;
848 : :
849 [ - + - - : 16014 : if (raise_exc && (res < 0 || res2 < 0)) {
- - ]
850 : 0 : pytime_time_t_overflow();
851 : 0 : return -1;
852 : : }
853 : 16014 : return 0;
854 : : }
855 : :
856 : : void
857 : 16014 : _PyTime_AsTimespec_clamp(_PyTime_t t, struct timespec *ts)
858 : : {
859 : 16014 : (void)pytime_as_timespec(t, ts, 0);
860 : 16014 : }
861 : :
862 : : int
863 : 0 : _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
864 : : {
865 : 0 : return pytime_as_timespec(t, ts, 1);
866 : : }
867 : : #endif
868 : :
869 : :
870 : : static int
871 : 3 : py_get_system_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
872 : : {
873 : : assert(info == NULL || raise_exc);
874 : :
875 : : #ifdef MS_WINDOWS
876 : : FILETIME system_time;
877 : : ULARGE_INTEGER large;
878 : :
879 : : GetSystemTimeAsFileTime(&system_time);
880 : : large.u.LowPart = system_time.dwLowDateTime;
881 : : large.u.HighPart = system_time.dwHighDateTime;
882 : : /* 11,644,473,600,000,000,000: number of nanoseconds between
883 : : the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
884 : : days). */
885 : : _PyTime_t ns = large.QuadPart * 100 - 11644473600000000000;
886 : : *tp = pytime_from_nanoseconds(ns);
887 : : if (info) {
888 : : DWORD timeAdjustment, timeIncrement;
889 : : BOOL isTimeAdjustmentDisabled, ok;
890 : :
891 : : info->implementation = "GetSystemTimeAsFileTime()";
892 : : info->monotonic = 0;
893 : : ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
894 : : &isTimeAdjustmentDisabled);
895 : : if (!ok) {
896 : : PyErr_SetFromWindowsErr(0);
897 : : return -1;
898 : : }
899 : : info->resolution = timeIncrement * 1e-7;
900 : : info->adjustable = 1;
901 : : }
902 : :
903 : : #else /* MS_WINDOWS */
904 : : int err;
905 : : #if defined(HAVE_CLOCK_GETTIME)
906 : : struct timespec ts;
907 : : #endif
908 : :
909 : : #if !defined(HAVE_CLOCK_GETTIME) || defined(__APPLE__)
910 : : struct timeval tv;
911 : : #endif
912 : :
913 : : #ifdef HAVE_CLOCK_GETTIME
914 : :
915 : : #ifdef HAVE_CLOCK_GETTIME_RUNTIME
916 : : if (HAVE_CLOCK_GETTIME_RUNTIME) {
917 : : #endif
918 : :
919 : 3 : err = clock_gettime(CLOCK_REALTIME, &ts);
920 [ - + ]: 3 : if (err) {
921 [ # # ]: 0 : if (raise_exc) {
922 : 0 : PyErr_SetFromErrno(PyExc_OSError);
923 : : }
924 : 0 : return -1;
925 : : }
926 [ - + ]: 3 : if (pytime_fromtimespec(tp, &ts, raise_exc) < 0) {
927 : 0 : return -1;
928 : : }
929 : :
930 [ - + ]: 3 : if (info) {
931 : : struct timespec res;
932 : 0 : info->implementation = "clock_gettime(CLOCK_REALTIME)";
933 : 0 : info->monotonic = 0;
934 : 0 : info->adjustable = 1;
935 [ # # ]: 0 : if (clock_getres(CLOCK_REALTIME, &res) == 0) {
936 : 0 : info->resolution = (double)res.tv_sec + (double)res.tv_nsec * 1e-9;
937 : : }
938 : : else {
939 : 0 : info->resolution = 1e-9;
940 : : }
941 : : }
942 : :
943 : : #ifdef HAVE_CLOCK_GETTIME_RUNTIME
944 : : }
945 : : else {
946 : : #endif
947 : :
948 : : #endif
949 : :
950 : : #if !defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_RUNTIME)
951 : :
952 : : /* test gettimeofday() */
953 : : err = gettimeofday(&tv, (struct timezone *)NULL);
954 : : if (err) {
955 : : if (raise_exc) {
956 : : PyErr_SetFromErrno(PyExc_OSError);
957 : : }
958 : : return -1;
959 : : }
960 : : if (pytime_fromtimeval(tp, &tv, raise_exc) < 0) {
961 : : return -1;
962 : : }
963 : :
964 : : if (info) {
965 : : info->implementation = "gettimeofday()";
966 : : info->resolution = 1e-6;
967 : : info->monotonic = 0;
968 : : info->adjustable = 1;
969 : : }
970 : :
971 : : #if defined(HAVE_CLOCK_GETTIME_RUNTIME) && defined(HAVE_CLOCK_GETTIME)
972 : : } /* end of availibity block */
973 : : #endif
974 : :
975 : : #endif /* !HAVE_CLOCK_GETTIME */
976 : : #endif /* !MS_WINDOWS */
977 : 3 : return 0;
978 : : }
979 : :
980 : :
981 : : _PyTime_t
982 : 0 : _PyTime_GetSystemClock(void)
983 : : {
984 : : _PyTime_t t;
985 [ # # ]: 0 : if (py_get_system_clock(&t, NULL, 0) < 0) {
986 : : // If clock_gettime(CLOCK_REALTIME) or gettimeofday() fails:
987 : : // silently ignore the failure and return 0.
988 : 0 : t = 0;
989 : : }
990 : 0 : return t;
991 : : }
992 : :
993 : :
994 : : int
995 : 3 : _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
996 : : {
997 : 3 : return py_get_system_clock(t, info, 1);
998 : : }
999 : :
1000 : :
1001 : : #ifdef __APPLE__
1002 : : static int
1003 : : py_mach_timebase_info(_PyTime_t *pnumer, _PyTime_t *pdenom, int raise)
1004 : : {
1005 : : static mach_timebase_info_data_t timebase;
1006 : : /* According to the Technical Q&A QA1398, mach_timebase_info() cannot
1007 : : fail: https://developer.apple.com/library/mac/#qa/qa1398/ */
1008 : : (void)mach_timebase_info(&timebase);
1009 : :
1010 : : /* Sanity check: should never occur in practice */
1011 : : if (timebase.numer < 1 || timebase.denom < 1) {
1012 : : if (raise) {
1013 : : PyErr_SetString(PyExc_RuntimeError,
1014 : : "invalid mach_timebase_info");
1015 : : }
1016 : : return -1;
1017 : : }
1018 : :
1019 : : /* Check that timebase.numer and timebase.denom can be casted to
1020 : : _PyTime_t. In practice, timebase uses uint32_t, so casting cannot
1021 : : overflow. At the end, only make sure that the type is uint32_t
1022 : : (_PyTime_t is 64-bit long). */
1023 : : static_assert(sizeof(timebase.numer) <= sizeof(_PyTime_t),
1024 : : "timebase.numer is larger than _PyTime_t");
1025 : : static_assert(sizeof(timebase.denom) <= sizeof(_PyTime_t),
1026 : : "timebase.denom is larger than _PyTime_t");
1027 : :
1028 : : /* Make sure that _PyTime_MulDiv(ticks, timebase_numer, timebase_denom)
1029 : : cannot overflow.
1030 : :
1031 : : Known time bases:
1032 : :
1033 : : * (1, 1) on Intel
1034 : : * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
1035 : :
1036 : : None of these time bases can overflow with 64-bit _PyTime_t, but
1037 : : check for overflow, just in case. */
1038 : : if ((_PyTime_t)timebase.numer > _PyTime_MAX / (_PyTime_t)timebase.denom) {
1039 : : if (raise) {
1040 : : PyErr_SetString(PyExc_OverflowError,
1041 : : "mach_timebase_info is too large");
1042 : : }
1043 : : return -1;
1044 : : }
1045 : :
1046 : : *pnumer = (_PyTime_t)timebase.numer;
1047 : : *pdenom = (_PyTime_t)timebase.denom;
1048 : : return 0;
1049 : : }
1050 : : #endif
1051 : :
1052 : :
1053 : : static int
1054 : 16102 : py_get_monotonic_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1055 : : {
1056 : : assert(info == NULL || raise_exc);
1057 : :
1058 : : #if defined(MS_WINDOWS)
1059 : : ULONGLONG ticks = GetTickCount64();
1060 : : static_assert(sizeof(ticks) <= sizeof(_PyTime_t),
1061 : : "ULONGLONG is larger than _PyTime_t");
1062 : : _PyTime_t t;
1063 : : if (ticks <= (ULONGLONG)_PyTime_MAX) {
1064 : : t = (_PyTime_t)ticks;
1065 : : }
1066 : : else {
1067 : : // GetTickCount64() maximum is larger than _PyTime_t maximum:
1068 : : // ULONGLONG is unsigned, whereas _PyTime_t is signed.
1069 : : t = _PyTime_MAX;
1070 : : }
1071 : :
1072 : : int res = pytime_mul(&t, MS_TO_NS);
1073 : : *tp = t;
1074 : :
1075 : : if (raise_exc && res < 0) {
1076 : : pytime_overflow();
1077 : : return -1;
1078 : : }
1079 : :
1080 : : if (info) {
1081 : : DWORD timeAdjustment, timeIncrement;
1082 : : BOOL isTimeAdjustmentDisabled, ok;
1083 : : info->implementation = "GetTickCount64()";
1084 : : info->monotonic = 1;
1085 : : ok = GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
1086 : : &isTimeAdjustmentDisabled);
1087 : : if (!ok) {
1088 : : PyErr_SetFromWindowsErr(0);
1089 : : return -1;
1090 : : }
1091 : : info->resolution = timeIncrement * 1e-7;
1092 : : info->adjustable = 0;
1093 : : }
1094 : :
1095 : : #elif defined(__APPLE__)
1096 : : static _PyTime_t timebase_numer = 0;
1097 : : static _PyTime_t timebase_denom = 0;
1098 : : if (timebase_denom == 0) {
1099 : : if (py_mach_timebase_info(&timebase_numer, &timebase_denom, raise_exc) < 0) {
1100 : : return -1;
1101 : : }
1102 : : }
1103 : :
1104 : : if (info) {
1105 : : info->implementation = "mach_absolute_time()";
1106 : : info->resolution = (double)timebase_numer / (double)timebase_denom * 1e-9;
1107 : : info->monotonic = 1;
1108 : : info->adjustable = 0;
1109 : : }
1110 : :
1111 : : uint64_t uticks = mach_absolute_time();
1112 : : // unsigned => signed
1113 : : assert(uticks <= (uint64_t)_PyTime_MAX);
1114 : : _PyTime_t ticks = (_PyTime_t)uticks;
1115 : :
1116 : : _PyTime_t ns = _PyTime_MulDiv(ticks, timebase_numer, timebase_denom);
1117 : : *tp = pytime_from_nanoseconds(ns);
1118 : :
1119 : : #elif defined(__hpux)
1120 : : hrtime_t time;
1121 : :
1122 : : time = gethrtime();
1123 : : if (time == -1) {
1124 : : if (raise_exc) {
1125 : : PyErr_SetFromErrno(PyExc_OSError);
1126 : : }
1127 : : return -1;
1128 : : }
1129 : :
1130 : : *tp = pytime_from_nanoseconds(time);
1131 : :
1132 : : if (info) {
1133 : : info->implementation = "gethrtime()";
1134 : : info->resolution = 1e-9;
1135 : : info->monotonic = 1;
1136 : : info->adjustable = 0;
1137 : : }
1138 : :
1139 : : #else
1140 : :
1141 : : #ifdef CLOCK_HIGHRES
1142 : : const clockid_t clk_id = CLOCK_HIGHRES;
1143 : : const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
1144 : : #else
1145 : 16102 : const clockid_t clk_id = CLOCK_MONOTONIC;
1146 : 16102 : const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
1147 : : #endif
1148 : :
1149 : : struct timespec ts;
1150 [ - + ]: 16102 : if (clock_gettime(clk_id, &ts) != 0) {
1151 [ # # ]: 0 : if (raise_exc) {
1152 : 0 : PyErr_SetFromErrno(PyExc_OSError);
1153 : 0 : return -1;
1154 : : }
1155 : 0 : return -1;
1156 : : }
1157 : :
1158 [ - + ]: 16102 : if (pytime_fromtimespec(tp, &ts, raise_exc) < 0) {
1159 : 0 : return -1;
1160 : : }
1161 : :
1162 [ - + ]: 16102 : if (info) {
1163 : 0 : info->monotonic = 1;
1164 : 0 : info->implementation = implementation;
1165 : 0 : info->adjustable = 0;
1166 : : struct timespec res;
1167 [ # # ]: 0 : if (clock_getres(clk_id, &res) != 0) {
1168 : 0 : PyErr_SetFromErrno(PyExc_OSError);
1169 : 0 : return -1;
1170 : : }
1171 : 0 : info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
1172 : : }
1173 : : #endif
1174 : 16102 : return 0;
1175 : : }
1176 : :
1177 : :
1178 : : _PyTime_t
1179 : 16014 : _PyTime_GetMonotonicClock(void)
1180 : : {
1181 : : _PyTime_t t;
1182 [ - + ]: 16014 : if (py_get_monotonic_clock(&t, NULL, 0) < 0) {
1183 : : // If mach_timebase_info(), clock_gettime() or gethrtime() fails:
1184 : : // silently ignore the failure and return 0.
1185 : 0 : t = 0;
1186 : : }
1187 : 16014 : return t;
1188 : : }
1189 : :
1190 : :
1191 : : int
1192 : 88 : _PyTime_GetMonotonicClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
1193 : : {
1194 : 88 : return py_get_monotonic_clock(tp, info, 1);
1195 : : }
1196 : :
1197 : :
1198 : : #ifdef MS_WINDOWS
1199 : : static int
1200 : : py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
1201 : : {
1202 : : LONGLONG frequency;
1203 : :
1204 : : LARGE_INTEGER freq;
1205 : : // Since Windows XP, the function cannot fail.
1206 : : (void)QueryPerformanceFrequency(&freq);
1207 : : frequency = freq.QuadPart;
1208 : :
1209 : : // Since Windows XP, frequency cannot be zero.
1210 : : assert(frequency >= 1);
1211 : :
1212 : : /* Make also sure that (ticks * SEC_TO_NS) cannot overflow in
1213 : : _PyTime_MulDiv(), with ticks < frequency.
1214 : :
1215 : : Known QueryPerformanceFrequency() values:
1216 : :
1217 : : * 10,000,000 (10 MHz): 100 ns resolution
1218 : : * 3,579,545 Hz (3.6 MHz): 279 ns resolution
1219 : :
1220 : : None of these frequencies can overflow with 64-bit _PyTime_t, but
1221 : : check for integer overflow just in case. */
1222 : : if (frequency > _PyTime_MAX / SEC_TO_NS) {
1223 : : if (raise) {
1224 : : PyErr_SetString(PyExc_OverflowError,
1225 : : "QueryPerformanceFrequency is too large");
1226 : : }
1227 : : return -1;
1228 : : }
1229 : :
1230 : : *pfrequency = frequency;
1231 : : return 0;
1232 : : }
1233 : :
1234 : :
1235 : : static int
1236 : : py_get_win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1237 : : {
1238 : : assert(info == NULL || raise_exc);
1239 : :
1240 : : static LONGLONG frequency = 0;
1241 : : if (frequency == 0) {
1242 : : if (py_win_perf_counter_frequency(&frequency, raise_exc) < 0) {
1243 : : return -1;
1244 : : }
1245 : : }
1246 : :
1247 : : if (info) {
1248 : : info->implementation = "QueryPerformanceCounter()";
1249 : : info->resolution = 1.0 / (double)frequency;
1250 : : info->monotonic = 1;
1251 : : info->adjustable = 0;
1252 : : }
1253 : :
1254 : : LARGE_INTEGER now;
1255 : : QueryPerformanceCounter(&now);
1256 : : LONGLONG ticksll = now.QuadPart;
1257 : :
1258 : : /* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
1259 : : both types are signed */
1260 : : _PyTime_t ticks;
1261 : : static_assert(sizeof(ticksll) <= sizeof(ticks),
1262 : : "LONGLONG is larger than _PyTime_t");
1263 : : ticks = (_PyTime_t)ticksll;
1264 : :
1265 : : _PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);
1266 : : *tp = pytime_from_nanoseconds(ns);
1267 : : return 0;
1268 : : }
1269 : : #endif // MS_WINDOWS
1270 : :
1271 : :
1272 : : int
1273 : 80 : _PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info)
1274 : : {
1275 : : #ifdef MS_WINDOWS
1276 : : return py_get_win_perf_counter(t, info, 1);
1277 : : #else
1278 : 80 : return _PyTime_GetMonotonicClockWithInfo(t, info);
1279 : : #endif
1280 : : }
1281 : :
1282 : :
1283 : : _PyTime_t
1284 : 0 : _PyTime_GetPerfCounter(void)
1285 : : {
1286 : : _PyTime_t t;
1287 : : int res;
1288 : : #ifdef MS_WINDOWS
1289 : : res = py_get_win_perf_counter(&t, NULL, 0);
1290 : : #else
1291 : 0 : res = py_get_monotonic_clock(&t, NULL, 0);
1292 : : #endif
1293 [ # # ]: 0 : if (res < 0) {
1294 : : // If py_win_perf_counter_frequency() or py_get_monotonic_clock()
1295 : : // fails: silently ignore the failure and return 0.
1296 : 0 : t = 0;
1297 : : }
1298 : 0 : return t;
1299 : : }
1300 : :
1301 : :
1302 : : int
1303 : 53 : _PyTime_localtime(time_t t, struct tm *tm)
1304 : : {
1305 : : #ifdef MS_WINDOWS
1306 : : int error;
1307 : :
1308 : : error = localtime_s(tm, &t);
1309 : : if (error != 0) {
1310 : : errno = error;
1311 : : PyErr_SetFromErrno(PyExc_OSError);
1312 : : return -1;
1313 : : }
1314 : : return 0;
1315 : : #else /* !MS_WINDOWS */
1316 : :
1317 : : #if defined(_AIX) && (SIZEOF_TIME_T < 8)
1318 : : /* bpo-34373: AIX does not return NULL if t is too small or too large */
1319 : : if (t < -2145916800 /* 1902-01-01 */
1320 : : || t > 2145916800 /* 2038-01-01 */) {
1321 : : errno = EINVAL;
1322 : : PyErr_SetString(PyExc_OverflowError,
1323 : : "localtime argument out of range");
1324 : : return -1;
1325 : : }
1326 : : #endif
1327 : :
1328 : 53 : errno = 0;
1329 [ - + ]: 53 : if (localtime_r(&t, tm) == NULL) {
1330 [ # # ]: 0 : if (errno == 0) {
1331 : 0 : errno = EINVAL;
1332 : : }
1333 : 0 : PyErr_SetFromErrno(PyExc_OSError);
1334 : 0 : return -1;
1335 : : }
1336 : 53 : return 0;
1337 : : #endif /* MS_WINDOWS */
1338 : : }
1339 : :
1340 : :
1341 : : int
1342 : 0 : _PyTime_gmtime(time_t t, struct tm *tm)
1343 : : {
1344 : : #ifdef MS_WINDOWS
1345 : : int error;
1346 : :
1347 : : error = gmtime_s(tm, &t);
1348 : : if (error != 0) {
1349 : : errno = error;
1350 : : PyErr_SetFromErrno(PyExc_OSError);
1351 : : return -1;
1352 : : }
1353 : : return 0;
1354 : : #else /* !MS_WINDOWS */
1355 [ # # ]: 0 : if (gmtime_r(&t, tm) == NULL) {
1356 : : #ifdef EINVAL
1357 [ # # ]: 0 : if (errno == 0) {
1358 : 0 : errno = EINVAL;
1359 : : }
1360 : : #endif
1361 : 0 : PyErr_SetFromErrno(PyExc_OSError);
1362 : 0 : return -1;
1363 : : }
1364 : 0 : return 0;
1365 : : #endif /* MS_WINDOWS */
1366 : : }
1367 : :
1368 : :
1369 : : _PyTime_t
1370 : 0 : _PyDeadline_Init(_PyTime_t timeout)
1371 : : {
1372 : 0 : _PyTime_t now = _PyTime_GetMonotonicClock();
1373 : 0 : return _PyTime_Add(now, timeout);
1374 : : }
1375 : :
1376 : :
1377 : : _PyTime_t
1378 : 0 : _PyDeadline_Get(_PyTime_t deadline)
1379 : : {
1380 : 0 : _PyTime_t now = _PyTime_GetMonotonicClock();
1381 : 0 : return deadline - now;
1382 : : }
|