Here is a little function I put together to do this right down to microseconds:
def tdToDict(td:datetime.timedelta) -> dict:
def __t(t, n):
if t < n: return (t, 0)
v = t//n
return (t - (v * n), v)
(s, h) = __t(td.seconds, 3600)
(s, m) = __t(s, 60)
(micS, milS) = __t(td.microseconds, 1000)
return {
'days': td.days
,'hours': h
,'minutes': m
,'seconds': s
,'milliseconds': milS
,'microseconds': micS
}
Here is a version that returns a tuple
:
# usage: (_d, _h, _m, _s, _mils, _mics) = tdTuple(td)
def tdTuple(td:datetime.timedelta) -> tuple:
def _t(t, n):
if t < n: return (t, 0)
v = t//n
return (t - (v * n), v)
(s, h) = _t(td.seconds, 3600)
(s, m) = _t(s, 60)
(mics, mils) = _t(td.microseconds, 1000)
return (td.days, h, m, s, mics, mils)