[issue2706] datetime: define division timedelta/timedelta

webograph report at bugs.python.org
Sun Apr 27 23:04:07 CEST 2008


New submission from webograph <webograph at eml.cc>:

i suggest that division be defined for timedelta1/timedelta2, in that
sense that it gives how many times timedelta2 fits in timedelta1 (ie the
usual meaning of division), using integer arithmetics for floor division
(//) and returning float for truediv (/ after `from __future__ import
division`)

use case
--------

aside from the obvious how-many-times-does-a-fit-into-b, this solves the
issue of having individual methods for conversion to a number of
seconds, hours, days or nanocenturies (as described in #1673409).
example:

from datetime import timedelta
duration = timedelta(hours=1.5, seconds=20)
print "Until the time is up, you can listen to 'We will rock you' %d
times."%(duration//timedelta(minutes=5, seconds=3))
import time
time.sleep(duration/timedelta(seconds=1))


history
-------

this issue follows a discussion on python-list, re-initiated by [1].

there have previously been similar feature requests on datetime, most of
which have been rejected due to ambiguities (e.g. [2]), conflicts with
time_t or issues with time zones.

the only issue i've seen that can be relevant here is the
integer-vs-float discussion, which is here handled by floordiv (//) and
truediv.

patch
-----

i've written a patch against svn trunk revision 62520.

it uses function pointers to reduce code duplication; in case this
inappropriate here, i also have a pointerless version.

i familiar with c but not experienced, especially with the python ways
of writing c. most of the code is just adapted from other functions in
the same files, so it is probably, but should nevertheless checked with
special care.

i've also added test, but am not sure what has to be tested and what not.


compatibility
-------------

only cases in which division would fail without the patch are changed.
this will be a problem if (and only if) someone divides unknown objects
and waits for TypeError to be raised.
such behavior is probably rare.


[1] <mid:4813CD56.40800 at eml.cc>,
http://mail.python.org/pipermail/python-list/2008-April/488406.html
[2] http://mail.python.org/pipermail/python-dev/2002-March/020604.html

----------
components: Library (Lib)
files: datetime_datetime_division.patch
keywords: patch
messages: 65902
nosy: webograph
severity: normal
status: open
title: datetime: define division timedelta/timedelta
type: feature request
Added file: http://bugs.python.org/file10126/datetime_datetime_division.patch

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2706>
__________________________________


More information about the Python-bugs-list mailing list