summaryrefslogtreecommitdiffstats
path: root/Python/pytime.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-03-27 16:12:45 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-03-27 16:12:45 (GMT)
commit992c43fec900e204deffc026318b97ab0f83eff6 (patch)
tree17cb58a91cb6a44d7d53124d9e84a7ba535dca6e /Python/pytime.c
parent79644f9c83b3cd992c16c6e1bd6c7a5fd49f24c0 (diff)
downloadcpython-992c43fec900e204deffc026318b97ab0f83eff6.zip
cpython-992c43fec900e204deffc026318b97ab0f83eff6.tar.gz
cpython-992c43fec900e204deffc026318b97ab0f83eff6.tar.bz2
Issue #22117: Fix rounding in _PyTime_FromSecondsObject()
* Rename _PyTime_FromObject() to _PyTime_FromSecondsObject() * Add _PyTime_AsNanosecondsObject() and _testcapi.pytime_fromsecondsobject() * Add unit tests
Diffstat (limited to 'Python/pytime.c')
-rw-r--r--Python/pytime.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/Python/pytime.c b/Python/pytime.c
index 6bf7030..2aeeddc 100644
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -424,7 +424,7 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
#endif
int
-_PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
+_PyTime_FromSecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
{
if (PyFloat_Check(obj)) {
double d, err;
@@ -433,8 +433,7 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
d = PyFloat_AsDouble(obj);
d *= 1e9;
- /* FIXME: use sign */
- if (round == _PyTime_ROUND_UP)
+ if ((round == _PyTime_ROUND_UP) ^ (d < 0))
d = ceil(d);
else
d = floor(d);
@@ -471,6 +470,18 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round)
}
}
+PyObject *
+_PyTime_AsNanosecondsObject(_PyTime_t t)
+{
+#ifdef HAVE_LONG_LONG
+ assert(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
+ return PyLong_FromLongLong((PY_LONG_LONG)t);
+#else
+ assert(sizeof(long) >= sizeof(_PyTime_t));
+ return PyLong_FromLong((long)t);
+#endif
+}
+
static _PyTime_t
_PyTime_Multiply(_PyTime_t t, unsigned int multiply, _PyTime_round_t round)
{