This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow Division of datetime.timedelta Objects
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeremy Banks
Priority: low Keywords:

Created on 2008-11-09 23:57 by Jeremy Banks, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (10)
msg75670 - (view) Author: Jeremy Banks (Jeremy Banks) Date: 2008-11-09 23:57
It would be convenient if it were possible to divide one
datetime.timedelta object by another to determine their relative durations.

Were the datetime module pure Python a crude solution would just be to
add two methods like this:

	def toMicroseconds(self):
		return ((self.days * 24 * 60) + self.seconds * 1000000) +
self.microseconds
	
	def __truediv__(self, other):
		return self.toMicroseconds() / other.toMicroseconds()

However, I don't understand know the Python C API well enough to know
how to patch the C module.
msg75671 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-11-10 00:02
That's just too weird. A long time ago I suggested to implement __int__
and __float__ on timedelta objects: int(timedelta) -> seconds,
float(timedelta) -> seconds.micros. Then your use case could be written
as float(td1) / float(td2) which is far more obvious than td1 / td2.
Unfortunately I wasn't a core developer back in those days.
msg75672 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-11-10 00:10
I don't understand what do you expect with the divison. Can you give an
use case and/or examples?
msg75673 - (view) Author: Jeremy Banks (Jeremy Banks) Date: 2008-11-10 00:16
Sorry, allowing for conversion to int/float is probably a more sensible
solution.

This idea was brought to my mind when I was making a very very simple
script for a friend to display how far through a time range we currently
are. For example:

	elapsed = datetime.timedelta(hours=4, days=3)
	duration = datetime.timedelta(days=30)
	
	percentage = (100 * elapsed / duration)

In my case, precision wasn't important so I just divided elapsed.days by
duration.days, but it would be continent to have an accurate result by
just writing what I did above.
msg75678 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-11-10 01:23
The issue #1673409 may help: delta1.toseconds() / delta2.toseconds().
msg75679 - (view) Author: Jeremy Banks (Jeremy Banks) Date: 2008-11-10 01:32
Thanks, I should have paid more attention to the results when I searched
for duplicates. I think that Christian's suggestion of enabling float()
and int() for timedeltas is worth having here, though.
msg75910 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-11-15 10:47
[Christian]
> float(td1) / float(td2) which is far more obvious than td1 / td2

To me, td1/td2 is more obvious that float(td1)/float(td2).

float(td) involves an arbitrary choice, to return time in *seconds* 
(rather than days, or milliseconds, or ...);  I think this violates 
EIBTI.  To me, the obvious and easy-to-read way to get the number
of seconds is to do the division:

seconds_in_td = td1 / timedelta(seconds = 1)
msg75919 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-11-16 00:29
@Christian

Adding a __float__ method to datetime was entertained back in 2003, but 
was rejected.  The same reasons apply to timedelta:

"""
- A C double doesn't have enough precision for roundtrip guarantees.

- Does it really need to be automatic?  I.e., does it really need to
  be __float__()?  I'd be less against this if it was an explicit
  method, e.g. dt.asposixtime().

--Guido van Rossum (home page: http://www.python.org/~guido/)
"""

http://mail.python.org/pipermail/python-dev/2003-January/032166.html
msg78186 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-12-22 14:28
See related issues: #1289118 and #2706.
msg85982 - (view) Author: Jeremy Banks (Jeremy Banks) Date: 2009-04-15 06:19
Redundant with #2706 and others.
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48541
2009-04-15 06:19:30Jeremy Bankssetstatus: open -> closed
nosy: - mark.dickinson, belopolsky, vstinner
messages: + msg85982

2008-12-22 14:28:40vstinnersetmessages: + msg78186
2008-11-16 00:29:40belopolskysetnosy: + belopolsky
messages: + msg75919
2008-11-15 10:47:23mark.dickinsonsetnosy: + mark.dickinson
messages: + msg75910
2008-11-10 01:32:13Jeremy Bankssetnosy: - christian.heimes
messages: + msg75679
2008-11-10 01:23:31vstinnersetmessages: + msg75678
2008-11-10 00:16:56Jeremy Bankssetmessages: + msg75673
2008-11-10 00:10:54vstinnersetnosy: + vstinner
messages: + msg75672
2008-11-10 00:02:54christian.heimessetpriority: low
nosy: + christian.heimes
messages: + msg75671
2008-11-09 23:57:54Jeremy Bankscreate