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.
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:41 | admin | set | github: 48541 |
| 2009年04月15日 06:19:30 | Jeremy Banks | set | status: open -> closed nosy: - mark.dickinson, belopolsky, vstinner messages: + msg85982 |
| 2008年12月22日 14:28:40 | vstinner | 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 | vstinner | set | messages: + msg75678 |
| 2008年11月10日 00:16:56 | Jeremy Banks | set | messages: + msg75673 |
| 2008年11月10日 00:10:54 | vstinner | set | nosy:
+ vstinner 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 | |