Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec26f83

Browse files
committed
Issue #25155: Fix _PyTime_Divide() rounding
_PyTime_Divide() rounding was wrong: copy code from Python default which has now much better unit tests.
1 parent 02d6a25 commit ec26f83

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

Lib/test/test_time.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -946,14 +946,14 @@ def test_milliseconds(self):
946946
# nanoseconds
947947
(1, 0, FLOOR),
948948
(1, 1, CEILING),
949-
(-1, 0, FLOOR),
950-
(-1, -1, CEILING),
949+
(-1, -1, FLOOR),
950+
(-1, 0, CEILING),
951951

952952
# seconds + nanoseconds
953953
(1234 * MS_TO_NS + 1, 1234, FLOOR),
954954
(1234 * MS_TO_NS + 1, 1235, CEILING),
955-
(-1234 * MS_TO_NS - 1, -1234, FLOOR),
956-
(-1234 * MS_TO_NS - 1, -1235, CEILING),
955+
(-1234 * MS_TO_NS - 1, -1235, FLOOR),
956+
(-1234 * MS_TO_NS - 1, -1234, CEILING),
957957
):
958958
with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
959959
self.assertEqual(PyTime_AsMilliseconds(ns, rnd), ms)
@@ -983,14 +983,14 @@ def test_microseconds(self):
983983
# nanoseconds
984984
(1, 0, FLOOR),
985985
(1, 1, CEILING),
986-
(-1, 0, FLOOR),
987-
(-1, -1, CEILING),
986+
(-1, -1, FLOOR),
987+
(-1, 0, CEILING),
988988

989989
# seconds + nanoseconds
990990
(1234 * US_TO_NS + 1, 1234, FLOOR),
991991
(1234 * US_TO_NS + 1, 1235, CEILING),
992-
(-1234 * US_TO_NS - 1, -1234, FLOOR),
993-
(-1234 * US_TO_NS - 1, -1235, CEILING),
992+
(-1234 * US_TO_NS - 1, -1235, FLOOR),
993+
(-1234 * US_TO_NS - 1, -1234, CEILING),
994994
):
995995
with self.subTest(nanoseconds=ns, milliseconds=ms, round=rnd):
996996
self.assertEqual(PyTime_AsMicroseconds(ns, rnd), ms)

Python/pytime.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,22 @@ _PyTime_AsNanosecondsObject(_PyTime_t t)
305305
}
306306

307307
static _PyTime_t
308-
_PyTime_Divide(_PyTime_t t, _PyTime_t k, _PyTime_round_t round)
308+
_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
309+
const _PyTime_round_t round)
309310
{
310311
assert(k > 1);
311312
if (round == _PyTime_ROUND_CEILING) {
312313
if (t >= 0)
313314
return (t + k - 1) / k;
315+
else
316+
return t / k;
317+
}
318+
else {
319+
if (t >= 0)
320+
return t / k;
314321
else
315322
return (t - (k - 1)) / k;
316323
}
317-
else
318-
return t / k;
319324
}
320325

321326
_PyTime_t

0 commit comments

Comments
 (0)