下面是一个小函数,我把它放在一起,精确到微秒:
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
}
下面是一个返回元组的版本:
# 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)