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

Skip to content

Commit a695f83

Browse files
committed
Issue #22117: Remove _PyTime_ROUND_DOWN and _PyTime_ROUND_UP rounding methods
Use _PyTime_ROUND_FLOOR and _PyTime_ROUND_CEILING instead.
1 parent 869e177 commit a695f83

4 files changed

Lines changed: 20 additions & 110 deletions

File tree

Include/pytime.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,9 @@ typedef PY_INT64_T _PyTime_t;
2525
#endif
2626

2727
typedef enum {
28-
/* Round towards zero. */
29-
_PyTime_ROUND_DOWN=0,
30-
/* Round away from zero.
31-
For example, used for timeout to wait "at least" N seconds. */
32-
_PyTime_ROUND_UP,
3328
/* Round towards minus infinity (-inf).
3429
For example, used to read a clock. */
35-
_PyTime_ROUND_FLOOR,
30+
_PyTime_ROUND_FLOOR=0,
3631
/* Round towards infinity (+inf).
3732
For example, used for timeout to wait "at least" N seconds. */
3833
_PyTime_ROUND_CEILING

Lib/test/test_time.py

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,12 @@
2424
SEC_TO_NS = 10 ** 9
2525

2626
class _PyTime(enum.IntEnum):
27-
# Round towards zero
28-
ROUND_DOWN = 0
29-
# Round away from zero
30-
ROUND_UP = 1
3127
# Round towards minus infinity (-inf)
32-
ROUND_FLOOR = 2
28+
ROUND_FLOOR = 0
3329
# Round towards infinity (+inf)
34-
ROUND_CEILING = 3
30+
ROUND_CEILING = 1
3531

36-
ALL_ROUNDING_METHODS = (
37-
_PyTime.ROUND_UP,
38-
_PyTime.ROUND_DOWN,
39-
_PyTime.ROUND_FLOOR,
40-
_PyTime.ROUND_CEILING)
32+
ALL_ROUNDING_METHODS = (_PyTime.ROUND_FLOOR, _PyTime.ROUND_CEILING)
4133

4234

4335
class TimeTestCase(unittest.TestCase):
@@ -617,27 +609,13 @@ def setUp(self):
617609
def test_time_t(self):
618610
from _testcapi import pytime_object_to_time_t
619611
for obj, time_t, rnd in (
620-
# Round towards zero
621-
(0, 0, _PyTime.ROUND_DOWN),
622-
(-1, -1, _PyTime.ROUND_DOWN),
623-
(-1.0, -1, _PyTime.ROUND_DOWN),
624-
(-1.9, -1, _PyTime.ROUND_DOWN),
625-
(1.0, 1, _PyTime.ROUND_DOWN),
626-
(1.9, 1, _PyTime.ROUND_DOWN),
627612
# Round towards minus infinity (-inf)
628613
(0, 0, _PyTime.ROUND_FLOOR),
629614
(-1, -1, _PyTime.ROUND_FLOOR),
630615
(-1.0, -1, _PyTime.ROUND_FLOOR),
631616
(-1.9, -2, _PyTime.ROUND_FLOOR),
632617
(1.0, 1, _PyTime.ROUND_FLOOR),
633618
(1.9, 1, _PyTime.ROUND_FLOOR),
634-
# Round away from zero
635-
(0, 0, _PyTime.ROUND_UP),
636-
(-1, -1, _PyTime.ROUND_UP),
637-
(-1.0, -1, _PyTime.ROUND_UP),
638-
(-1.9, -2, _PyTime.ROUND_UP),
639-
(1.0, 1, _PyTime.ROUND_UP),
640-
(1.9, 2, _PyTime.ROUND_UP),
641619
# Round towards infinity (+inf)
642620
(0, 0, _PyTime.ROUND_CEILING),
643621
(-1, -1, _PyTime.ROUND_CEILING),
@@ -657,20 +635,6 @@ def test_time_t(self):
657635
def test_timespec(self):
658636
from _testcapi import pytime_object_to_timespec
659637
for obj, timespec, rnd in (
660-
# Round towards zero
661-
(0, (0, 0), _PyTime.ROUND_DOWN),
662-
(-1, (-1, 0), _PyTime.ROUND_DOWN),
663-
(-1.0, (-1, 0), _PyTime.ROUND_DOWN),
664-
(1e-9, (0, 1), _PyTime.ROUND_DOWN),
665-
(1e-10, (0, 0), _PyTime.ROUND_DOWN),
666-
(-1e-9, (-1, 999999999), _PyTime.ROUND_DOWN),
667-
(-1e-10, (0, 0), _PyTime.ROUND_DOWN),
668-
(-1.2, (-2, 800000000), _PyTime.ROUND_DOWN),
669-
(0.9999999999, (0, 999999999), _PyTime.ROUND_DOWN),
670-
(1.1234567890, (1, 123456789), _PyTime.ROUND_DOWN),
671-
(1.1234567899, (1, 123456789), _PyTime.ROUND_DOWN),
672-
(-1.1234567890, (-2, 876543211), _PyTime.ROUND_DOWN),
673-
(-1.1234567891, (-2, 876543211), _PyTime.ROUND_DOWN),
674638
# Round towards minus infinity (-inf)
675639
(0, (0, 0), _PyTime.ROUND_FLOOR),
676640
(-1, (-1, 0), _PyTime.ROUND_FLOOR),
@@ -685,20 +649,6 @@ def test_timespec(self):
685649
(1.1234567899, (1, 123456789), _PyTime.ROUND_FLOOR),
686650
(-1.1234567890, (-2, 876543211), _PyTime.ROUND_FLOOR),
687651
(-1.1234567891, (-2, 876543210), _PyTime.ROUND_FLOOR),
688-
# Round away from zero
689-
(0, (0, 0), _PyTime.ROUND_UP),
690-
(-1, (-1, 0), _PyTime.ROUND_UP),
691-
(-1.0, (-1, 0), _PyTime.ROUND_UP),
692-
(1e-9, (0, 1), _PyTime.ROUND_UP),
693-
(1e-10, (0, 1), _PyTime.ROUND_UP),
694-
(-1e-9, (-1, 999999999), _PyTime.ROUND_UP),
695-
(-1e-10, (-1, 999999999), _PyTime.ROUND_UP),
696-
(-1.2, (-2, 800000000), _PyTime.ROUND_UP),
697-
(0.9999999999, (1, 0), _PyTime.ROUND_UP),
698-
(1.1234567890, (1, 123456790), _PyTime.ROUND_UP),
699-
(1.1234567899, (1, 123456790), _PyTime.ROUND_UP),
700-
(-1.1234567890, (-2, 876543211), _PyTime.ROUND_UP),
701-
(-1.1234567891, (-2, 876543210), _PyTime.ROUND_UP),
702652
# Round towards infinity (+inf)
703653
(0, (0, 0), _PyTime.ROUND_CEILING),
704654
(-1, (-1, 0), _PyTime.ROUND_CEILING),
@@ -836,40 +786,26 @@ def test_FromSecondsObject(self):
836786
PyTime_FromSecondsObject(-9223372037.0, rnd)
837787

838788
# Conversion giving different results depending on the rounding method
839-
UP = _PyTime.ROUND_UP
840-
DOWN = _PyTime.ROUND_DOWN
841789
FLOOR = _PyTime.ROUND_FLOOR
842790
CEILING = _PyTime.ROUND_CEILING
843791
for obj, ts, rnd in (
844792
# close to zero
845-
( 1e-10, 1, CEILING),
846-
( 1e-10, 1, UP),
847-
( 1e-10, 0, DOWN),
848793
( 1e-10, 0, FLOOR),
849-
(-1e-10, 0, CEILING),
850-
(-1e-10, 0, DOWN),
851-
(-1e-10, -1, UP),
794+
( 1e-10, 1, CEILING),
852795
(-1e-10, -1, FLOOR),
796+
(-1e-10, 0, CEILING),
853797

854798
# test rounding of the last nanosecond
855-
( 1.1234567899, 1123456790, CEILING),
856-
( 1.1234567899, 1123456790, UP),
857-
( 1.1234567899, 1123456789, DOWN),
858799
( 1.1234567899, 1123456789, FLOOR),
859-
(-1.1234567899, -1123456789, CEILING),
860-
(-1.1234567899, -1123456789, DOWN),
861-
(-1.1234567899, -1123456790, UP),
800+
( 1.1234567899, 1123456790, CEILING),
862801
(-1.1234567899, -1123456790, FLOOR),
802+
(-1.1234567899, -1123456789, CEILING),
863803

864804
# close to 1 second
865-
( 0.9999999999, 1000000000, CEILING),
866-
( 0.9999999999, 1000000000, UP),
867-
( 0.9999999999, 999999999, DOWN),
868805
( 0.9999999999, 999999999, FLOOR),
869-
(-0.9999999999, -999999999, CEILING),
870-
(-0.9999999999, -999999999, DOWN),
871-
(-0.9999999999, -1000000000, UP),
806+
( 0.9999999999, 1000000000, CEILING),
872807
(-0.9999999999, -1000000000, FLOOR),
808+
(-0.9999999999, -999999999, CEILING),
873809
):
874810
with self.subTest(obj=obj, round=rnd, timestamp=ts):
875811
self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)
@@ -939,30 +875,20 @@ def test_timeval(self):
939875
with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
940876
self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)
941877

942-
UP = _PyTime.ROUND_UP
943-
DOWN = _PyTime.ROUND_DOWN
944878
FLOOR = _PyTime.ROUND_FLOOR
945879
CEILING = _PyTime.ROUND_CEILING
946880
for ns, tv, rnd in (
947881
# nanoseconds
948-
(1, (0, 1), CEILING),
949-
(1, (0, 1), UP),
950-
(1, (0, 0), DOWN),
951882
(1, (0, 0), FLOOR),
952-
(-1, (0, 0), CEILING),
953-
(-1, (0, 0), DOWN),
954-
(-1, (-1, 999999), UP),
883+
(1, (0, 1), CEILING),
955884
(-1, (-1, 999999), FLOOR),
885+
(-1, (0, 0), CEILING),
956886

957887
# seconds + nanoseconds
958-
(1234567001, (1, 234568), CEILING),
959-
(1234567001, (1, 234568), UP),
960-
(1234567001, (1, 234567), DOWN),
961888
(1234567001, (1, 234567), FLOOR),
962-
(-1234567001, (-2, 765433), CEILING),
963-
(-1234567001, (-2, 765433), DOWN),
964-
(-1234567001, (-2, 765432), UP),
889+
(1234567001, (1, 234568), CEILING),
965890
(-1234567001, (-2, 765432), FLOOR),
891+
(-1234567001, (-2, 765433), CEILING),
966892
):
967893
with self.subTest(nanoseconds=ns, timeval=tv, round=rnd):
968894
self.assertEqual(PyTime_AsTimeval(ns, rnd), tv)

Modules/_testcapimodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,8 +2634,7 @@ run_in_subinterp(PyObject *self, PyObject *args)
26342634
static int
26352635
check_time_rounding(int round)
26362636
{
2637-
if (round != _PyTime_ROUND_DOWN && round != _PyTime_ROUND_UP
2638-
&& round != _PyTime_ROUND_FLOOR && round != _PyTime_ROUND_CEILING) {
2637+
if (round != _PyTime_ROUND_FLOOR && round != _PyTime_ROUND_CEILING) {
26392638
PyErr_SetString(PyExc_ValueError, "invalid rounding");
26402639
return -1;
26412640
}

Python/pytime.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ error_time_t_overflow(void)
2626
"timestamp out of range for platform time_t");
2727
}
2828

29-
static int
30-
_PyTime_RoundTowardsPosInf(int is_neg, _PyTime_round_t round)
31-
{
32-
if (round == _PyTime_ROUND_FLOOR)
33-
return 0;
34-
if (round == _PyTime_ROUND_CEILING)
35-
return 1;
36-
return ((round == _PyTime_ROUND_UP) ^ is_neg);
37-
}
38-
3929
time_t
4030
_PyLong_AsTime_t(PyObject *obj)
4131
{
@@ -84,7 +74,7 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
8474
}
8575

8676
floatpart *= denominator;
87-
if (_PyTime_RoundTowardsPosInf(intpart < 0, round)) {
77+
if (round == _PyTime_ROUND_CEILING) {
8878
floatpart = ceil(floatpart);
8979
if (floatpart >= denominator) {
9080
floatpart = 0.0;
@@ -121,7 +111,7 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
121111
double d, intpart, err;
122112

123113
d = PyFloat_AsDouble(obj);
124-
if (_PyTime_RoundTowardsPosInf(d < 0, round))
114+
if (round == _PyTime_ROUND_CEILING)
125115
d = ceil(d);
126116
else
127117
d = floor(d);
@@ -223,7 +213,7 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
223213
d = PyFloat_AsDouble(obj);
224214
d *= 1e9;
225215

226-
if (_PyTime_RoundTowardsPosInf(d < 0, round))
216+
if (round == _PyTime_ROUND_CEILING)
227217
d = ceil(d);
228218
else
229219
d = floor(d);
@@ -289,7 +279,7 @@ _PyTime_Multiply(_PyTime_t t, unsigned int multiply, _PyTime_round_t round)
289279
_PyTime_t k;
290280
if (multiply < SEC_TO_NS) {
291281
k = SEC_TO_NS / multiply;
292-
if (_PyTime_RoundTowardsPosInf(t < 0, round))
282+
if (round == _PyTime_ROUND_CEILING)
293283
return (t + k - 1) / k;
294284
else
295285
return t / k;
@@ -350,7 +340,7 @@ _PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
350340
res = -1;
351341
#endif
352342

353-
if (_PyTime_RoundTowardsPosInf(tv->tv_sec < 0, round))
343+
if (round == _PyTime_ROUND_CEILING)
354344
tv->tv_usec = (int)((ns + US_TO_NS - 1) / US_TO_NS);
355345
else
356346
tv->tv_usec = (int)(ns / US_TO_NS);

0 commit comments

Comments
 (0)