Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (revision 81610) +++ Modules/datetimemodule.c (working copy) @@ -2125,7 +2125,29 @@ } if (leftover_us) { /* Round to nearest whole # of us, and add into x. */ - PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); + double whole_us = round(leftover_us); + int x_is_odd; + PyObject *temp; + + 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. */ + temp = PyNumber_And(x, us_per_us); + 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(whole_us); if (temp == NULL) { Py_DECREF(x); goto Done;