Refactor pytime.c

Move code to convert double timestamp to subfunctions.
This commit is contained in:
Victor Stinner 2015-09-02 00:49:16 +02:00
parent 799b05b052
commit 53e137c8dd
1 changed files with 67 additions and 50 deletions

View File

@ -61,16 +61,13 @@ _PyLong_FromTime_t(time_t t)
} }
static int static int
_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, _PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
double denominator, _PyTime_round_t round) double denominator, _PyTime_round_t round)
{ {
assert(denominator <= LONG_MAX); double intpart, err;
if (PyFloat_Check(obj)) {
double d, intpart, err;
/* volatile avoids unsafe optimization on float enabled by gcc -O3 */ /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
volatile double floatpart; volatile double floatpart;
d = PyFloat_AsDouble(obj);
floatpart = modf(d, &intpart); floatpart = modf(d, &intpart);
if (floatpart < 0) { if (floatpart < 0) {
floatpart = 1.0 + floatpart; floatpart = 1.0 + floatpart;
@ -98,6 +95,17 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
*numerator = (long)floatpart; *numerator = (long)floatpart;
return 0; return 0;
}
static int
_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
double denominator, _PyTime_round_t round)
{
assert(denominator <= LONG_MAX);
if (PyFloat_Check(obj)) {
double d = PyFloat_AsDouble(obj);
return _PyTime_DoubleToDenominator(d, sec, numerator,
denominator, round);
} }
else { else {
*sec = _PyLong_AsTime_t(obj); *sec = _PyLong_AsTime_t(obj);
@ -221,15 +229,14 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
#endif #endif
static int static int
_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, _PyTime_FromFloatObject(_PyTime_t *t, double value, _PyTime_round_t round,
long to_nanoseconds) long to_nanoseconds)
{ {
if (PyFloat_Check(obj)) {
/* volatile avoids unsafe optimization on float enabled by gcc -O3 */ /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
volatile double d, err; volatile double d, err;
/* convert to a number of nanoseconds */ /* convert to a number of nanoseconds */
d = PyFloat_AsDouble(obj); d = value;
d *= to_nanoseconds; d *= to_nanoseconds;
if (round == _PyTime_ROUND_CEILING) if (round == _PyTime_ROUND_CEILING)
@ -244,6 +251,16 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
return -1; return -1;
} }
return 0; return 0;
}
static int
_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
long to_nanoseconds)
{
if (PyFloat_Check(obj)) {
double d;
d = PyFloat_AsDouble(obj);
return _PyTime_FromFloatObject(t, d, round, to_nanoseconds);
} }
else { else {
#ifdef HAVE_LONG_LONG #ifdef HAVE_LONG_LONG