diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-03-30 19:36:10 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-03-30 19:36:10 (GMT) |
commit | fa09beb1508f782b51ba0a2815c07e0294f40e95 (patch) | |
tree | 6215208a85dae67128419b939aceb12ba92c2adf /Python | |
parent | 749a6a85c645648199eb4e855a9537a8b703ffeb (diff) | |
download | cpython-fa09beb1508f782b51ba0a2815c07e0294f40e95.zip cpython-fa09beb1508f782b51ba0a2815c07e0294f40e95.tar.gz cpython-fa09beb1508f782b51ba0a2815c07e0294f40e95.tar.bz2 |
Issue #23485: Add _PyTime_FromMillisecondsObject() function
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pytime.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/Python/pytime.c b/Python/pytime.c index 5bf8c56..003003b 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -203,8 +203,9 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise) } #endif -int -_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) +static int +_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, + long to_nanoseconds) { if (PyFloat_Check(obj)) { /* volatile avoids unsafe optimization on float enabled by gcc -O3 */ @@ -212,7 +213,7 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) /* convert to a number of nanoseconds */ d = PyFloat_AsDouble(obj); - d *= 1e9; + d *= to_nanoseconds; if (round == _PyTime_ROUND_CEILING) d = ceil(d); @@ -242,8 +243,8 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) _PyTime_overflow(); return -1; } - *t = sec * SEC_TO_NS; - if (*t / SEC_TO_NS != sec) { + *t = sec * to_nanoseconds; + if (*t / to_nanoseconds != sec) { _PyTime_overflow(); return -1; } @@ -251,6 +252,18 @@ _PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) } } +int +_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) +{ + return _PyTime_FromObject(t, obj, round, SEC_TO_NS); +} + +int +_PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round) +{ + return _PyTime_FromObject(t, obj, round, MS_TO_NS); +} + double _PyTime_AsSecondsDouble(_PyTime_t t) { |