diff -r 3fd9970d9e65 Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c Sat Feb 23 19:56:15 2013 +0100 +++ b/Modules/_datetimemodule.c Sat Feb 23 21:27:08 2013 +0100 @@ -3846,6 +3846,76 @@ return result; } +/* + * time arithmetic. + */ + +/* factor must be 1 (to add) or -1 (to subtract). The result inherits + * the tzinfo state of date. + */ +static PyObject * +add_time_timedelta(PyDateTime_Time *time, PyDateTime_Delta *delta, + int factor) +{ + /* Note that the C-level additions can't overflow, because of + * invariant bounds on the member values. + */ + int day = 0; // Used for normalize_pair + int hour = TIME_GET_HOUR(time); + int minute = TIME_GET_MINUTE(time); + int second = TIME_GET_SECOND(time) + GET_TD_SECONDS(delta) * factor; + int microsecond = TIME_GET_MICROSECOND(time) + + GET_TD_MICROSECONDS(delta) * factor; + + assert(factor == 1 || factor == -1); + + normalize_pair(&second, µsecond, 1000000); + normalize_pair(&minute, &second, 60); + normalize_pair(&hour, &minute, 60); + normalize_pair(&day, &hour, 24); + + return new_time(hour, minute, second, microsecond, + HASTZINFO(time) ? time->tzinfo : Py_None); +} + +static PyObject * +time_add(PyObject *left, PyObject *right) +{ + if (PyTime_Check(left)) { + /* time + ??? */ + if (PyDelta_Check(right)) + /* time + delta */ + return add_time_timedelta( + (PyDateTime_Time *)left, + (PyDateTime_Delta *)right, + 1); + } + else if (PyDelta_Check(left)) { + /* delta + datetime */ + return add_time_timedelta((PyDateTime_Time *) right, + (PyDateTime_Delta *) left, + 1); + } + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; +} + +static PyObject * +time_subtract(PyObject *left, PyObject *right) +{ + if (PyTime_Check(left)) { + /* time + ??? */ + if (PyDelta_Check(right)) + /* time + delta */ + return add_time_timedelta( + (PyDateTime_Time *)left, + (PyDateTime_Delta *)right, + -1); + } + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; +} + static PyObject * time_reduce(PyDateTime_Time *self, PyObject *arg) { @@ -3889,8 +3959,8 @@ a tzinfo subclass. The remaining arguments may be ints or longs.\n"); static PyNumberMethods time_as_number = { - 0, /* nb_add */ - 0, /* nb_subtract */ + time_add, /* nb_add */ + time_subtract, /* nb_subtract */ 0, /* nb_multiply */ 0, /* nb_remainder */ 0, /* nb_divmod */