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
_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
_PyTime_DoubleToDenominator(double d, time_t *sec, long *numerator,
double denominator, _PyTime_round_t round)
{
assert(denominator <= LONG_MAX);
if (PyFloat_Check(obj)) {
double d, intpart, err;
double intpart, err;
/* volatile avoids unsafe optimization on float enabled by gcc -O3 */
volatile double floatpart;
d = PyFloat_AsDouble(obj);
floatpart = modf(d, &intpart);
if (floatpart < 0) {
floatpart = 1.0 + floatpart;
@ -99,6 +96,17 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
*numerator = (long)floatpart;
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 {
*sec = _PyLong_AsTime_t(obj);
if (*sec == (time_t)-1 && PyErr_Occurred())
@ -221,15 +229,14 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
#endif
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)
{
if (PyFloat_Check(obj)) {
/* volatile avoids unsafe optimization on float enabled by gcc -O3 */
volatile double d, err;
/* convert to a number of nanoseconds */
d = PyFloat_AsDouble(obj);
d = value;
d *= to_nanoseconds;
if (round == _PyTime_ROUND_CEILING)
@ -245,6 +252,16 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
}
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 {
#ifdef HAVE_LONG_LONG
PY_LONG_LONG sec;