我需要一个返回布尔值的函数,指示午夜是否刚刚过去。我想出了这个,但我对“形式”不满意。有人能想到更好的办法吗?就效率/优雅而言?
from datetime import datetime, timedelta
def passed_midnight(delta=1):
time_now = datetime.today # see other comment below
time_ago = time_now() - timedelta(minutes=delta)
# next line with a dummy delta (zero) cuz "datetime.today - timedelta(days=1)" gives an error
today = time_now() - timedelta(days=0)
return today.strftime("%Y%m%d") != time_ago.strftime("%Y%m%d")
>>> print(passed_midnight, 10)
浮云间
吃鸡游戏
相关分类