Issue4291
Created on 2008-11-09 23:57 by Jeremy Banks, last changed 2009-04-15 06:19 by Jeremy Banks.
|
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) |
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 (haypo) |
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 (haypo) |
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) |
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) |
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 (haypo) |
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.
|
|
| Date |
User |
Action |
Args |
| 2009-04-15 06:19:30 | Jeremy Banks | set | status: open -> closed nosy:
- mark.dickinson, belopolsky, haypo messages:
+ msg85982
|
| 2008-12-22 14:28:40 | haypo | set | messages:
+ msg78186 |
| 2008-11-16 00:29:40 | belopolsky | set | nosy:
+ belopolsky messages:
+ msg75919 |
| 2008-11-15 10:47:23 | mark.dickinson | set | nosy:
+ mark.dickinson messages:
+ msg75910 |
| 2008-11-10 01:32:13 | Jeremy Banks | set | nosy:
- christian.heimes messages:
+ msg75679 |
| 2008-11-10 01:23:31 | haypo | set | messages:
+ msg75678 |
| 2008-11-10 00:16:56 | Jeremy Banks | set | messages:
+ msg75673 |
| 2008-11-10 00:10:54 | haypo | set | nosy:
+ haypo messages:
+ msg75672 |
| 2008-11-10 00:02:54 | christian.heimes | set | priority: low nosy:
+ christian.heimes messages:
+ msg75671 |
| 2008-11-09 23:57:54 | Jeremy Banks | create | |
|