将timedelta转换为总秒数

问题:将timedelta转换为总秒数

我有时间差

time1 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
...
time2 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
diff = time2 - time1

现在,我如何找到经过的总秒数?diff.seconds不算数天。我可以:

diff.seconds + diff.days * 24 * 3600

有内置的方法吗?

I have a time difference

import time
import datetime

time1 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
...
time2 = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
diff = time2 - time1

Now, how do I find the total number of seconds that passed? diff.seconds doesn’t count days. I could do:

diff.seconds + diff.days * 24 * 3600

Is there a builtin method for this?


回答 0

使用timedelta.total_seconds()

>>> import datetime
>>> datetime.timedelta(seconds=24*60*60).total_seconds()
86400.0

Use timedelta.total_seconds().

>>> import datetime
>>> datetime.timedelta(seconds=24*60*60).total_seconds()
86400.0

回答 1

您的一个或另一个问题 datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))表达。

(1)如果您所需要的只是两秒之间的差异,那么就很简单time.time()

(2)如果您将这些时间戳记用于其他目的,则需要考虑您在做什么,因为结果在整个过程中都有很大的气味:

gmtime()UTC返回一个时间元组,但是mktime()期望以本地时间为时间元组

我在澳大利亚的墨尔本,标准TZ是UTC + 10,但是夏令时仍然有效,直到明天早上,所以是UTC + 11。当我执行以下命令时,这是当地时间2011-04-02T20:31 … UTC是2011-04-02T09:31

>>> import time, datetime
>>> t1 = time.gmtime()
>>> t2 = time.mktime(t1)
>>> t3 = datetime.datetime.fromtimestamp(t2)
>>> print t0
1301735358.78
>>> print t1
time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=9, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=0) ### this is UTC
>>> print t2
1301700663.0
>>> print t3
2011-04-02 10:31:03 ### this is UTC+1
>>> tt = time.time(); print tt
1301736663.88
>>> print datetime.datetime.now()
2011-04-02 20:31:03.882000 ### UTC+11, my local time
>>> print datetime.datetime(1970,1,1) + datetime.timedelta(seconds=tt)
2011-04-02 09:31:03.880000 ### UTC
>>> print time.localtime()
time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=20, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=1) ### UTC+11, my local time

您会注意到,t3表达式的结果是UTC + 1,这似乎是UTC +(我的本地DST差异)……不是很有意义。您应该考虑使用datetime.datetime.utcnow()DST开启/关闭时不会跳一个小时的方法,它可能会比time.time()

You have a problem one way or the other with your datetime.datetime.fromtimestamp(time.mktime(time.gmtime())) expression.

(1) If all you need is the difference between two instants in seconds, the very simple time.time() does the job.

(2) If you are using those timestamps for other purposes, you need to consider what you are doing, because the result has a big smell all over it:

gmtime() returns a time tuple in UTC but mktime() expects a time tuple in local time.

I’m in Melbourne, Australia where the standard TZ is UTC+10, but daylight saving is still in force until tomorrow morning so it’s UTC+11. When I executed the following, it was 2011-04-02T20:31 local time here … UTC was 2011-04-02T09:31

>>> import time, datetime
>>> t1 = time.gmtime()
>>> t2 = time.mktime(t1)
>>> t3 = datetime.datetime.fromtimestamp(t2)
>>> print t0
1301735358.78
>>> print t1
time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=9, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=0) ### this is UTC
>>> print t2
1301700663.0
>>> print t3
2011-04-02 10:31:03 ### this is UTC+1
>>> tt = time.time(); print tt
1301736663.88
>>> print datetime.datetime.now()
2011-04-02 20:31:03.882000 ### UTC+11, my local time
>>> print datetime.datetime(1970,1,1) + datetime.timedelta(seconds=tt)
2011-04-02 09:31:03.880000 ### UTC
>>> print time.localtime()
time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=20, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=1) ### UTC+11, my local time

You’ll notice that t3, the result of your expression is UTC+1, which appears to be UTC + (my local DST difference) … not very meaningful. You should consider using datetime.datetime.utcnow() which won’t jump by an hour when DST goes on/off and may give you more precision than time.time()


回答 2

您可以使用mx.DateTime模块

import mx.DateTime as mt

t1 = mt.now() 
t2 = mt.now()
print int((t2-t1).seconds)

You can use mx.DateTime module

import mx.DateTime as mt

t1 = mt.now() 
t2 = mt.now()
print int((t2-t1).seconds)