On 2008-04-27 09:12, Martin v. Löwis wrote:
Last time I brought up this sort of thing, it seemed fairly unanimous
that the shortcomings of the datetime module were 'deliberate' and
would not be fixed, patch or no patch.
Ok, so then if the answer to my question is "yes", the first step
should be to discuss it on python-dev.
i've had a look at the source code and written a small patch (attached; contains a case in classical/floor division as well as truediv). is there a defined escalation procedure from python-list to python-dev or should i just send the suggestion+patch there?

regards
webograph

ps: i hope the patch conforms to python c coding style (most of it is just copy/pasted and modified).
Index: Modules/datetimemodule.c
===================================================================
--- Modules/datetimemodule.c	(revision 62520)
+++ Modules/datetimemodule.c	(working copy)
@@ -1656,6 +1656,30 @@
 }
 
 static PyObject *
+anydivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right, PyObject* (*operator)(PyObject*, PyObject*))
+{
+	PyObject *pyus_left;
+	PyObject *pyus_right;
+	PyObject *result;
+
+	pyus_left = delta_to_microseconds(left);
+	if (pyus_left == NULL)
+		return NULL;
+
+	pyus_right = delta_to_microseconds(right);
+	if (pyus_right == NULL)
+	{
+		Py_DECREF(pyus_left);
+		return NULL;
+	}
+
+	result = operator(pyus_left, pyus_right);
+	Py_DECREF(pyus_left);
+	Py_DECREF(pyus_right);
+	return result;
+}
+
+static PyObject *
 delta_add(PyObject *left, PyObject *right)
 {
 	PyObject *result = Py_NotImplemented;
@@ -1808,6 +1832,11 @@
 			result = divide_timedelta_int(
 					(PyDateTime_Delta *)left,
 					right);
+		if (PyDelta_Check(right))
+			result = anydivide_timedelta_timedelta(
+					(PyDateTime_Delta *)left,
+					(PyDateTime_Delta *)right,
+					PyNumber_Divide);
 	}
 
 	if (result == Py_NotImplemented)
@@ -1815,6 +1844,24 @@
 	return result;
 }
 
+static PyObject *
+delta_truedivide(PyObject *left, PyObject *right)
+{
+	PyObject *result = Py_NotImplemented;
+
+	if (PyDelta_Check(left)) {
+		if (PyDelta_Check(right))
+			result = anydivide_timedelta_timedelta(
+					(PyDateTime_Delta *)left,
+					(PyDateTime_Delta *)right,
+					PyNumber_TrueDivide);
+	}
+
+	if (result == Py_NotImplemented)
+		Py_INCREF(result);
+	return result;
+}
+
 /* Fold in the value of the tag ("seconds", "weeks", etc) component of a
  * timedelta constructor.  sofar is the # of microseconds accounted for
  * so far, and there are factor microseconds per current unit, the number
@@ -2147,7 +2194,7 @@
 	0,					/*nb_inplace_xor*/
 	0,					/*nb_inplace_or*/
 	delta_divide,				/* nb_floor_divide */
-	0,					/* nb_true_divide */
+	delta_truedivide,			/* nb_true_divide */
 	0,					/* nb_inplace_floor_divide */
 	0,					/* nb_inplace_true_divide */
 };
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to