Python's timedelta: can't I just get in whatever time unit I want the value of the entire difference?

被刻印的时光 ゝ 提交于 2019-12-01 15:09:10

It seems that Python 2.7 has introduced a total_seconds() method, which is what you were looking for, I believe!

You can compute the difference in seconds.

total_seconds = delta.days * 86400 + delta.seconds

No, you're no "missing something". It doesn't provide deltas in seconds.

It would be perfect if I could just get the entire difference in seconds.

Then plain-old-unix-timestamp as provided by the 'time' module may be more to your taste.

I personally have yet to be convinced by a lot of what's in 'datetime'.

Like bobince said, you could use timestamps, like this:

# assuming ts1 and ts2 are the two datetime objects
from time import mktime
mktime(ts1.timetuple()) - mktime(ts2.timetuple())

Although I would think this is even uglier than just calculating the seconds from the timedelta object...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!