[Python-Dev] datetime.timedelta total_microseconds

Nick Coghlan ncoghlan at gmail.com
Sat Feb 16 11:59:36 EST 2019

On Fri, 15 Feb 2019 at 04:15, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Thu, Feb 14, 2019 at 9:07 AM Paul Ganssle <paul at ganssle.io> wrote:
>> I don't think it's totally unreasonable to have other total_X() methods, where X would be days, hours, minutes and microseconds
> I do.  I was against adding the total_seconds() method to begin with because the same effect can be achieved with
> delta / timedelta(seconds=1)
> this is easily generalized to
> delta / timedelta(X=1)
> where X can be days, hours, minutes or microseconds.

As someone who reads date/time manipulation code far more often then
he writes it, it's immediately obvious to me what
"delta.total_seconds()" is doing, while "some_var / some_other_var"
could be doing anything.

So for the sake of those us that aren't as well versed in how time
delta division works, it seems to me that adding:

    def total_duration(td, interval=timedelta(seconds=1)):
        return td / interval

as a module level helper function would make a lot of sense. (This is
a variant on Paul's helper function that accepts the divisor as a
specifically named argument with a default value, rather than creating
it on every call)


P.S. Why a function rather than a method? Mostly because this feels
like "len() for timedelta objects" to me, but also because as a helper
function, the docs can easily describe how to add it as a utility
function for older versions.

Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list