Python时间差异 [英] Python time differences

查看:100
本文介绍了Python时间差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个时间对象。

示例

time.struct_time(tm_year=2010, tm_mon=9, tm_mday=24, tm_hour=19, tm_min=13, tm_sec=37, tm_wday=4, tm_yday=267, tm_isdst=-1)

time.struct_time(tm_year=2010, tm_mon=9, tm_mday=25, tm_hour=13, tm_min=7, tm_sec=25, tm_wday=5, tm_yday=268, tm_isdst=-1)

我该怎么办?
我只需要几分钟和几秒钟,以及这两个的持续时间。

I want to have the difference of those two. How could I do that? I need minutes and seconds only, as well as the duration of those two.

推荐答案

时间实例不支持减法运算。考虑到解决这个问题的一种方法是将时间转换为从时代开始的秒数,然后找到差异,请使用:

Time instances do not support the subtraction operation. Given that one way to solve this would be to convert the time to seconds since epoch and then find the difference, use:

>>> t1 = time.localtime()
>>> t1
time.struct_time(tm_year=2010, tm_mon=10, tm_mday=13, tm_hour=10, tm_min=12, tm_sec=27, tm_wday=2, tm_yday=286, tm_isdst=0)
>>> t2 = time.gmtime()
>>> t2
time.struct_time(tm_year=2010, tm_mon=10, tm_mday=13, tm_hour=4, tm_min=42, tm_sec=37, tm_wday=2, tm_yday=286, tm_isdst=0)

>>> (time.mktime(t1) - time.mktime(t2)) / 60
329.83333333333331

这篇关于Python时间差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆