|
4 | 4 | # include <winsock2.h> // struct timeval
|
5 | 5 | #endif
|
6 | 6 |
|
| 7 | +#include <fenv.h> |
| 8 | + |
7 | 9 | #if defined(__APPLE__)
|
8 | 10 | # include <mach/mach_time.h> // mach_absolute_time(), mach_timebase_info()
|
9 | 11 |
|
@@ -294,11 +296,13 @@ pytime_double_to_denominator(double d, time_t *sec, long *numerator,
|
294 | 296 | }
|
295 | 297 | assert(0.0 <= floatpart && floatpart < denominator);
|
296 | 298 |
|
297 |
| - if (!_Py_InIntegralTypeRange(time_t, intpart)) { |
| 299 | + feclearexcept(FE_ALL_EXCEPT); |
| 300 | + long long second = llround(intpart); |
| 301 | + if (fetestexcept(FE_INVALID) || !_Py_InIntegralTypeRange(time_t, second)) { |
298 | 302 | pytime_time_t_overflow();
|
299 | 303 | return -1;
|
300 | 304 | }
|
301 |
| - *sec = (time_t)intpart; |
| 305 | + *sec = (time_t)second; |
302 | 306 | *numerator = (long)floatpart;
|
303 | 307 | assert(0 <= *numerator && *numerator < idenominator);
|
304 | 308 | return 0;
|
@@ -349,11 +353,13 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
|
349 | 353 | d = pytime_round(d, round);
|
350 | 354 | (void)modf(d, &intpart);
|
351 | 355 |
|
352 |
| - if (!_Py_InIntegralTypeRange(time_t, intpart)) { |
| 356 | + feclearexcept(FE_ALL_EXCEPT); |
| 357 | + long long second = llround(intpart); |
| 358 | + if (fetestexcept(FE_INVALID) || !_Py_InIntegralTypeRange(time_t, second)) { |
353 | 359 | pytime_time_t_overflow();
|
354 | 360 | return -1;
|
355 | 361 | }
|
356 |
| - *sec = (time_t)intpart; |
| 362 | + *sec = (time_t)second; |
357 | 363 | return 0;
|
358 | 364 | }
|
359 | 365 | else {
|
@@ -515,13 +521,14 @@ pytime_from_double(_PyTime_t *tp, double value, _PyTime_round_t round,
|
515 | 521 | d *= (double)unit_to_ns;
|
516 | 522 | d = pytime_round(d, round);
|
517 | 523 |
|
518 |
| - if (!_Py_InIntegralTypeRange(_PyTime_t, d)) { |
| 524 | + feclearexcept(FE_ALL_EXCEPT); |
| 525 | + long long ns = llround(d); |
| 526 | + if (fetestexcept(FE_ALL_EXCEPT)) { |
519 | 527 | pytime_overflow();
|
520 | 528 | return -1;
|
521 | 529 | }
|
522 |
| - _PyTime_t ns = (_PyTime_t)d; |
523 | 530 |
|
524 |
| - *tp = pytime_from_nanoseconds(ns); |
| 531 | + *tp = pytime_from_nanoseconds((_PyTime_t)ns); |
525 | 532 | return 0;
|
526 | 533 | }
|
527 | 534 |
|
|
0 commit comments