我如何在两个日期之间,以秒为单位检查差异?

时间:2021-08-15 21:27:33

There has to be an easier way to do this. I have objects that want to be refreshed every so often, so I want to record when they were created, check against the current timestamp, and refresh as necessary.

必须有一个更简单的方法。我有一些对象想要经常刷新,所以我想在创建它们时记录它们,检查当前时间戳,并在必要时刷新。

datetime.datetime has proven to be difficult, and I don't want to dive into the ctime library. Is there anything easier for this sort of thing?

datetime。datetime已经被证明是很困难的,我不想深入到ctime库中。这类事情有什么更容易的吗?

4 个解决方案

#1


312  

if you want to compute differences between two known dates, use total_seconds like this:

如果要计算两个已知日期之间的差异,请使用total_seconds:

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()

86400.0

86400.0

#note that seconds doesn't give you what you want:
(b-a).seconds

0

0

#2


25  

import time  
current = time.time()

...job...
end = time.time()
diff = end - current

would that work for you?

这对你有用吗?

#3


12  

>>> from datetime import datetime

>>>  a = datetime.now()

# wait a bit 
>>> b = datetime.now()

>>> d = b - a # yields a timedelta object
>>> d.seconds
7

(7 will be whatever amount of time you waited a bit above)

(7将是你在上面等待的时间)

I find datetime.datetime to be fairly useful, so if there's a complicated or awkward scenario that you've encountered, please let us know.

我发现datetime。datetime是相当有用的,所以如果您遇到了一个复杂或尴尬的场景,请告诉我们。

EDIT: Thanks to @WoLpH for pointing out that one is not always necessarily looking to refresh so frequently that the datetimes will be close together. By accounting for the days in the delta, you can handle longer timestamp discrepancies:

编辑:感谢@WoLpH指出,一个人并不总是需要经常刷新,所以datetimes将会紧密结合在一起。通过计算delta的天数,您可以处理较长的时间戳差异:

>>> a = datetime(2010, 12, 5)
>>> b = datetime(2010, 12, 7)
>>> d = b - a
>>> d.seconds
0
>>> d.days
2
>>> d.seconds + d.days * 86400
172800

#4


8  

We have function total_seconds() with Python 2.7 Please see below code for python 2.6

我们使用Python 2.7的函数total_seconds(),请参见下面的Python 2.6代码。

import datetime
import time  

def diffdates(d1, d2):
    #Date format: %Y-%m-%d %H:%M:%S
    return (time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S")) -
               time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S")))

d1 = datetime.now()
d2 = datetime.now() + timedelta(days=1)
diff = diffdates(d1, d2)

#1


312  

if you want to compute differences between two known dates, use total_seconds like this:

如果要计算两个已知日期之间的差异,请使用total_seconds:

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()

86400.0

86400.0

#note that seconds doesn't give you what you want:
(b-a).seconds

0

0

#2


25  

import time  
current = time.time()

...job...
end = time.time()
diff = end - current

would that work for you?

这对你有用吗?

#3


12  

>>> from datetime import datetime

>>>  a = datetime.now()

# wait a bit 
>>> b = datetime.now()

>>> d = b - a # yields a timedelta object
>>> d.seconds
7

(7 will be whatever amount of time you waited a bit above)

(7将是你在上面等待的时间)

I find datetime.datetime to be fairly useful, so if there's a complicated or awkward scenario that you've encountered, please let us know.

我发现datetime。datetime是相当有用的,所以如果您遇到了一个复杂或尴尬的场景,请告诉我们。

EDIT: Thanks to @WoLpH for pointing out that one is not always necessarily looking to refresh so frequently that the datetimes will be close together. By accounting for the days in the delta, you can handle longer timestamp discrepancies:

编辑:感谢@WoLpH指出,一个人并不总是需要经常刷新,所以datetimes将会紧密结合在一起。通过计算delta的天数,您可以处理较长的时间戳差异:

>>> a = datetime(2010, 12, 5)
>>> b = datetime(2010, 12, 7)
>>> d = b - a
>>> d.seconds
0
>>> d.days
2
>>> d.seconds + d.days * 86400
172800

#4


8  

We have function total_seconds() with Python 2.7 Please see below code for python 2.6

我们使用Python 2.7的函数total_seconds(),请参见下面的Python 2.6代码。

import datetime
import time  

def diffdates(d1, d2):
    #Date format: %Y-%m-%d %H:%M:%S
    return (time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S")) -
               time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S")))

d1 = datetime.now()
d2 = datetime.now() + timedelta(days=1)
diff = diffdates(d1, d2)