查找日期时间之间是否经过了24小时 [英] Find if 24 hrs have passed between datetimes
问题描述
我有以下方法:
# last_updated is a datetime() object, representing the last time this program ran
def time_diff(last_updated):
day_period = last_updated.replace(day=last_updated.day + 1,
hour=1,
minute=0,
second=0,
microsecond=0)
delta_time = day_period - last_updated
hours = delta_time.seconds // 3600
# make sure a period of 24hrs have passed before shuffling
if hours >= 24:
print "hello"
else:
print "do nothing"
我想了解自 last_updated
起是否经过了24小时,我该如何在Python中做到这一点?
I want to find out if 24 hrs have passed since last_updated
, how can I do that in Python?
推荐答案
如果 last_updated
是一个简单的datetime对象,表示UTC时间:
If last_updated
is a naive datetime object representing the time in UTC:
from datetime import datetime, timedelta
if (datetime.utcnow() - last_updated) > timedelta(1):
# more than 24 hours passed
如果 last_updated
是本地时间(天真(不知道时区)日期时间对象):
If last_updated
is the local time (naive (timezone-unaware) datetime object):
import time
DAY = 86400
now = time.time()
then = time.mktime(last_updated.timetuple())
if (now - then) > DAY:
# more than 24 hours passed
如果 last_updated
是一个模棱两可的时间,例如DST结束过渡期间的时间(在许多时区中,一年一次),那么 mktime()就有五十五十的机会了
返回错误结果(例如,关闭一个小时)。
If last_updated
is an ambiguous time e.g., the time during an end-of-DST transition (once a year in many timezones) then there is a fifty-fifty chance that mktime()
returns a wrong result (e.g., off by an hour).
time.mktime()
time 库未在给定平台上使用历史时区数据库,并且本地时区的UTC偏移量不同,>也可能会失败与现在相比,时间为 last_updated
。它可能适用于过去一年中所有时区的三分之一以上。 Linux,OS X,Windows的最新版本都有tz数据库(我不知道旧的Windows版本是否可以在过去的日期使用)。
time.mktime()
may also fail if C time
library doesn't use a historical timezone database on a given platform and the UTC offset for the local timezone was different at last_updated
time compared to now. It may apply to more than a third of all timezones in the last year. Linux, OS X, the recent versions of Windows have the tz database (I don't know whether old Windows versions would work for such past dates).
当心:它可能很想写 datetime.now()-last_updated
(类似于UTC情况),但是如果UTC偏移量在<$处不同,则可以保证在所有平台上都失败。 c $ c> last_updated 时间(在很多时区都是可能的)。基于 mktime()
的解决方案至少可以在某些平台上使用tz数据库,因此无论出于何种原因,它都可以处理UTC偏移量的变化。
Beware: it might be tempting to write datetime.now() - last_updated
(similar to the UTC case) but it is guaranteed to fail on all platforms if the UTC offset was different at last_updated
time (it is possible in many timezones). mktime()
-based solution can utilize the tz database at least on some platforms and therefore it can handle the changes in the UTC offset for whatever reason there.
要获得可移植性,您可以安装tz数据库。它由Python中的 pytz
模块提供。 tzlocal
可以返回与本地时区相对应的 pytz
时区:
For portability, you could install the tz database. It is provided by pytz
module in Python. tzlocal
can return pytz
timezone corresponding to the local timezone:
from datetime import datetime, timedelta
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone() # local timezone
then = tz.normalize(tz.localize(last_updated)) # make it timezone-aware
now = datetime.now(tz) # timezone-aware current time in the local timezone
if (now - then) > timedelta(1):
# more than 24 hours passed
即使UTC仍然有效抵销过去是不同的。但是它不能(以及 time.mktime()
)修复不明确的时间( tz.localize()
默认情况下选择 is_dst = False
时间)。调用 tz.normalize()
来调整不存在的时间,例如与DST开始过渡相对应的时间(它不会影响结果)。
It works even if the UTC offset was different in the past. But it can't (as well as time.mktime()
) fix ambiguous times (tz.localize()
picks is_dst=False
time by default). tz.normalize()
is called to adjust non-existing times e.g., those that correspond to a start-of-DST transition (it should not affect the result).
上面的代码假定 last_updated
是一个简单的日期时间对象(没有关联的时区信息)。如果 last_updated
是已知的日期时间对象,则很容易将其转换为UTC:
The above code assumes that last_updated
is a naive datetime object (no associated timezone info). If last_updated
is an aware datetime object then it is easy to convert it to UTC:
from datetime import datetime, timedelta
then_in_utc = last_updated.replace(tzinfo=None) - last_updated.utcoffset()
if (datetime.utcnow() - then_in_utc) > timedelta(1):
# more than 24 hours passed
一般说明:您应该了解现在,为什么人们建议使用UTC时间并仅将本地时间用于显示。
General note: you should understand now why people recommend to work with UTC time and to use local time only for display.
这篇关于查找日期时间之间是否经过了24小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!