static _PyTime_t _PyTime_Divide(const _PyTime_t t, const _PyTime_t k, const _PyTime_round_t round) { assert(k > 1); if (round == _PyTime_ROUND_HALF_EVEN) { _PyTime_t x, r, abs_r; x = t / k; r = t % k; abs_r = Py_ABS(r); if (abs_r > k / 2 || (abs_r == k / 2 && (Py_ABS(x) & 1))) { if (t >= 0) x++; else x--; } return x; } else if (round == _PyTime_ROUND_CEILING) { if (t >= 0) return (t + k - 1) / k; else return t / k; } else { if (t >= 0) return t / k; else return (t - (k - 1)) / k; } }
static _PyTime_t _PyTime_Divide(_PyTime_t t, _PyTime_t k, _PyTime_round_t round) { assert(k > 1); if (round == _PyTime_ROUND_HALF_UP) { _PyTime_t x, r; x = t / k; r = t % k; if (Py_ABS(r) >= k / 2) { if (t >= 0) x++; else x--; } return x; } else if (round == _PyTime_ROUND_CEILING) { if (t >= 0) return (t + k - 1) / k; else return (t - (k - 1)) / k; } else return t / k; }
static int _PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round, int raise) { const long k = US_TO_NS; _PyTime_t secs, ns; int res = 0; int usec; secs = t / SEC_TO_NS; ns = t % SEC_TO_NS; if (ns < 0) { ns += SEC_TO_NS; secs -= 1; } #ifdef MS_WINDOWS /* On Windows, timeval.tv_sec is a long (32 bit), whereas time_t can be 64-bit. */ assert(sizeof(tv->tv_sec) == sizeof(long)); #if SIZEOF_TIME_T > SIZEOF_LONG if (secs > LONG_MAX) { secs = LONG_MAX; res = -1; } else if (secs < LONG_MIN) { secs = LONG_MIN; res = -1; } #endif tv->tv_sec = (long)secs; #else /* On OpenBSD 5.4, timeval.tv_sec is a long. Example: long is 64-bit, whereas time_t is 32-bit. */ tv->tv_sec = secs; if ((_PyTime_t)tv->tv_sec != secs) res = -1; #endif if (round == _PyTime_ROUND_HALF_UP) { _PyTime_t r; usec = (int)(ns / k); r = ns % k; if (Py_ABS(r) >= k / 2) { if (ns >= 0) usec++; else usec--; } } else if (round == _PyTime_ROUND_CEILING) usec = (int)((ns + k - 1) / k); else usec = (int)(ns / k); if (usec >= SEC_TO_US) { usec -= SEC_TO_US; tv->tv_sec += 1; } assert(0 <= usec && usec < SEC_TO_US); tv->tv_usec = usec; if (res && raise) _PyTime_overflow(); return res; }