@@ -72,6 +72,17 @@ _PyTime_RoundHalfEven(double x)
7272 return rounded ;
7373}
7474
75+ static double
76+ _PyTime_Round (double x , _PyTime_round_t round )
77+ {
78+ if (round == _PyTime_ROUND_HALF_EVEN )
79+ return _PyTime_RoundHalfEven (x );
80+ else if (round == _PyTime_ROUND_CEILING )
81+ return ceil (x );
82+ else
83+ return floor (x );
84+ }
85+
7586static int
7687_PyTime_DoubleToDenominator (double d , time_t * sec , long * numerator ,
7788 double denominator , _PyTime_round_t round )
@@ -83,12 +94,7 @@ _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
8394 floatpart = modf (d , & intpart );
8495
8596 floatpart *= denominator ;
86- if (round == _PyTime_ROUND_HALF_EVEN )
87- floatpart = _PyTime_RoundHalfEven (floatpart );
88- else if (round == _PyTime_ROUND_CEILING )
89- floatpart = ceil (floatpart );
90- else
91- floatpart = floor (floatpart );
97+ floatpart = _PyTime_Round (floatpart , round );
9298 if (floatpart >= denominator ) {
9399 floatpart -= denominator ;
94100 intpart += 1.0 ;
@@ -139,12 +145,7 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
139145 volatile double d ;
140146
141147 d = PyFloat_AsDouble (obj );
142- if (round == _PyTime_ROUND_HALF_EVEN )
143- d = _PyTime_RoundHalfEven (d );
144- else if (round == _PyTime_ROUND_CEILING )
145- d = ceil (d );
146- else
147- d = floor (d );
148+ d = _PyTime_Round (d , round );
148149 (void )modf (d , & intpart );
149150
150151 * sec = (time_t )intpart ;
@@ -255,22 +256,16 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
255256
256257static int
257258_PyTime_FromFloatObject (_PyTime_t * t , double value , _PyTime_round_t round ,
258- long to_nanoseconds )
259+ long unit_to_ns )
259260{
260261 double err ;
261262 /* volatile avoids optimization changing how numbers are rounded */
262263 volatile double d ;
263264
264265 /* convert to a number of nanoseconds */
265266 d = value ;
266- d *= to_nanoseconds ;
267-
268- if (round == _PyTime_ROUND_HALF_EVEN )
269- d = _PyTime_RoundHalfEven (d );
270- else if (round == _PyTime_ROUND_CEILING )
271- d = ceil (d );
272- else
273- d = floor (d );
267+ d *= (double )unit_to_ns ;
268+ d = _PyTime_Round (d , round );
274269
275270 * t = (_PyTime_t )d ;
276271 err = d - (double )* t ;
@@ -283,12 +278,12 @@ _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
283278
284279static int
285280_PyTime_FromObject (_PyTime_t * t , PyObject * obj , _PyTime_round_t round ,
286- long to_nanoseconds )
281+ long unit_to_ns )
287282{
288283 if (PyFloat_Check (obj )) {
289284 double d ;
290285 d = PyFloat_AsDouble (obj );
291- return _PyTime_FromFloatObject (t , d , round , to_nanoseconds );
286+ return _PyTime_FromFloatObject (t , d , round , unit_to_ns );
292287 }
293288 else {
294289#ifdef HAVE_LONG_LONG
@@ -305,8 +300,8 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
305300 _PyTime_overflow ();
306301 return -1 ;
307302 }
308- * t = sec * to_nanoseconds ;
309- if (* t / to_nanoseconds != sec ) {
303+ * t = sec * unit_to_ns ;
304+ if (* t / unit_to_ns != sec ) {
310305 _PyTime_overflow ();
311306 return -1 ;
312307 }
0 commit comments