[Python-Dev] datetime.timedelta total_microseconds

Chris Barker chris.barker at noaa.gov
Wed Feb 27 18:18:20 EST 2019


On Wed, Feb 27, 2019 at 3:04 PM Richard Belleville <rbellevi at google.com>
wrote:

> Timedelta division is quite a nice solution to the problem. However, since
> we're maintaining a python version agnostic library at least until 2020, we
> need a solution that works in python 2 as well.
>

So you were limited to a py2 solution.

But di you poke around in py3 before posting?

(you should have, python-dev is really about active development, i.e.
python 3 -- but that's not the point here)


> For the moment, we've left the code as in the original snippet.
>

If you care about microsecond precision for large timeseltas, you may want
to improve that.

I *think* this is the "correct" way to do it:

def timedelta_to_microseconds(td):
    return td.microseconds + td.seconds * 1000 + td.days * 86400000

(hardly tested)

-CHB

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20190227/f12e9d24/attachment.html>


More information about the Python-Dev mailing list