【发布时间】:2023-03-12 10:14:01
【问题描述】:
我有以下方法:
# 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 中做到这一点?
【问题讨论】:
-
最后更新的格式是什么?
-
什么是
last_updated?是当地时间吗?你想知道是超过 24 小时还是第二天的当前时间更大,例如 last_updated=Nov 1, 7pm并且当前时间是Nov 2, 6:30pm(超过 24 小时,但是下午 6:30 是不到晚上 7 点)(在纽约)。 -
@PadraicCunningham 我已经更新了问题...... last_updated 是一个 datetime() ,它告诉程序上次更新的时间。如果自上次更新以来已经过去了 24 小时,我现在需要做的就是(从现在())
-
@J.F.Sebastian 查看对 PadraicCunningham 的评论