summaryrefslogtreecommitdiffstats
path: root/Modules/_datetimemodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-09-08 21:58:54 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-09-08 21:58:54 (GMT)
commit69cc487df42d9064a74551ae26a8c115dade3e3a (patch)
treedc5407073d3fdeeae4049a2540852604f60ec986 /Modules/_datetimemodule.c
parent1638bdfa1a6f0183eef47dd43b1f0ab0f6b9f78d (diff)
downloadcpython-69cc487df42d9064a74551ae26a8c115dade3e3a.zip
cpython-69cc487df42d9064a74551ae26a8c115dade3e3a.tar.gz
cpython-69cc487df42d9064a74551ae26a8c115dade3e3a.tar.bz2
Revert change 0eb8c182131e:
"""Issue #23517: datetime.timedelta constructor now rounds microseconds to nearest with ties going away from zero (ROUND_HALF_UP), as Python 2 and Python older than 3.3, instead of rounding to nearest with ties going to nearest even integer (ROUND_HALF_EVEN).""" datetime.timedelta uses rounding mode ROUND_HALF_EVEN again.
Diffstat (limited to 'Modules/_datetimemodule.c')
-rw-r--r--Modules/_datetimemodule.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index ea2bae6..24e83d3 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -2149,9 +2149,29 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (leftover_us) {
/* Round to nearest whole # of us, and add into x. */
double whole_us = round(leftover_us);
+ int x_is_odd;
PyObject *temp;
- whole_us = _PyTime_RoundHalfUp(leftover_us);
+ whole_us = round(leftover_us);
+ if (fabs(whole_us - leftover_us) == 0.5) {
+ /* We're exactly halfway between two integers. In order
+ * to do round-half-to-even, we must determine whether x
+ * is odd. Note that x is odd when it's last bit is 1. The
+ * code below uses bitwise and operation to check the last
+ * bit. */
+ temp = PyNumber_And(x, one); /* temp <- x & 1 */
+ if (temp == NULL) {
+ Py_DECREF(x);
+ goto Done;
+ }
+ x_is_odd = PyObject_IsTrue(temp);
+ Py_DECREF(temp);
+ if (x_is_odd == -1) {
+ Py_DECREF(x);
+ goto Done;
+ }
+ whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd;
+ }
temp = PyLong_FromLong((long)whole_us);