我需要检查一个纪元时间是否少于1天,3天和5天。这似乎有效,但对于python来说感觉过于复杂。可以简化吗?
import datetime
feed_time = 1508522102
d1 = round((datetime.datetime.now() - datetime.timedelta(1)).timestamp())
d2 = round((datetime.datetime.now() - datetime.timedelta(3)).timestamp())
d3 = round((datetime.datetime.now() - datetime.timedelta(5)).timestamp())
if feed_time > d1:
print("less than one day")
# do something if feed_time is less than 1 day
if feed_time > d2:
print("less than three days")
# do something if feed_time is less than 3 days
if feed_time > d3:
print("less than five days")
# do something if feed_time is less than 5 days
答案 0 :(得分:0)
不确定;将时间延迟列入清单;使用索引遍历列表。找到正确的输出级别后,从另一个列表中打印相应的消息。
check = [datetime.timedelta(1)).timestamp(),
datetime.timedelta(3)).timestamp(),
datetime.timedelta(5)).timestamp()]
message = ["one day", "three days", "five days"]
for day in range(3):
if feed_time > round(datetime.datetime.now() - check[day]):
print message[day]
break
那样的东西?
答案 1 :(得分:0)
您只需time.now() - feed_time
并将其与x*24*60*60
进行比较。
如果您想考虑DST,则会更复杂。