I don't think it's totally unreasonable to have other total_X() methods, where X would be days, hours, minutes and microseconds, but it also doesn't seem like a pressing need to me. I think the biggest argument against it is that they are all trivial to implement as necessary, because they're just unit conversions that involve multiplication or division by constants, which is nowhere near as complicated to implement as the original `total_seconds` method. Here's the issue where total_seconds() was implemented, it doesn't seem like there was any discussion of other total methods until after the issue was closed: https://bugs.python.org/issue5788 I think the main issue is how "thick" we want the timedelta class to be. With separate methods for every unit, we have to maintain and document 5 methods instead of 1, though the methods are trivial and the documentation could maybe be shared. If I had a time machine, I'd probably recommend an interface something like this: def total_duration(self, units='seconds'): return self._total_seconds() * _SECONDS_PER_UNIT[units] I suppose it would be possible to move to that interface today, though I think it would be mildly confusing to have two functions that do the same thing (total_seconds and total_duration), which may not be worth it considering that these functions are a pretty minor convenience. Best, Paul On 2/14/19 12:05 AM, Richard Belleville via Python-Dev wrote:
In a recent code review, the following snippet was called out as reinventing the wheel:
_MICROSECONDS_PER_SECOND = 1000000
def _timedelta_to_microseconds(delta): return int(delta.total_seconds() * _MICROSECONDS_PER_SECOND)
The reviewer thought that there must already exist a standard library function that fulfills this functionality. After we had both satisfied ourselves that we hadn't simply missed something in the documentation, we decided that we had better raise the issue with a wider audience.
Does this functionality already exist within the standard library? If not, would a datetime.timedelta.total_microseconds function be a reasonable addition? I would be happy to submit a patch for such a thing.
Richard Belleville
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/paul%40ganssle.io