diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-03-29 22:44:06 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-03-29 22:44:06 (GMT) |
commit | f81f0f9c63c8ae306d550b5bb387abf30e60a668 (patch) | |
tree | f73f88019eba031edb39608f7d5b656906953a7d /Python | |
parent | 1bd18ba9a78c58b817564637f1937c2bc3920ecd (diff) | |
download | cpython-f81f0f9c63c8ae306d550b5bb387abf30e60a668.zip cpython-f81f0f9c63c8ae306d550b5bb387abf30e60a668.tar.gz cpython-f81f0f9c63c8ae306d550b5bb387abf30e60a668.tar.bz2 |
Issue #22117: Fix rounding and implement _PyTime_ROUND_FLOOR in:
- _PyTime_ObjectToTime_t()
- _PyTime_ObjectToTimespec()
- _PyTime_ObjectToTimeval()
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pytime.c | 44 |
1 files changed, 20 insertions, 24 deletions
diff --git a/Python/pytime.c b/Python/pytime.c index d9ff3c6..2bf6ba5 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -26,6 +26,14 @@ error_time_t_overflow(void) "timestamp out of range for platform time_t"); } +static int +_PyTime_RoundTowardsPosInf(int is_neg, _PyTime_round_t round) +{ + if (round == _PyTime_ROUND_FLOOR) + return 0; + return ((round == _PyTime_ROUND_UP) ^ is_neg); +} + time_t _PyLong_AsTime_t(PyObject *obj) { @@ -74,18 +82,16 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, } floatpart *= denominator; - if (round == _PyTime_ROUND_UP) { - if (intpart >= 0) { - floatpart = ceil(floatpart); - if (floatpart >= denominator) { - floatpart = 0.0; - intpart += 1.0; - } - } - else { - floatpart = floor(floatpart); + if (_PyTime_RoundTowardsPosInf(intpart < 0, round)) { + floatpart = ceil(floatpart); + if (floatpart >= denominator) { + floatpart = 0.0; + intpart += 1.0; } } + else { + floatpart = floor(floatpart); + } *sec = (time_t)intpart; err = intpart - (double)*sec; @@ -113,12 +119,10 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round) double d, intpart, err; d = PyFloat_AsDouble(obj); - if (round == _PyTime_ROUND_UP) { - if (d >= 0) - d = ceil(d); - else - d = floor(d); - } + if (_PyTime_RoundTowardsPosInf(d < 0, round)) + d = ceil(d); + else + d = floor(d); (void)modf(d, &intpart); *sec = (time_t)intpart; @@ -158,14 +162,6 @@ _PyTime_overflow(void) "timestamp too large to convert to C _PyTime_t"); } -int -_PyTime_RoundTowardsPosInf(int is_neg, _PyTime_round_t round) -{ - if (round == _PyTime_ROUND_FLOOR) - return 0; - return ((round == _PyTime_ROUND_UP) ^ is_neg); -} - _PyTime_t _PyTime_FromNanoseconds(PY_LONG_LONG ns) { |