为什么我的代码无法理解用于在 if 语句中进行比较的时间变量?
Why my code can't understand time variables for comparing within the if statement?
我写了一个程序运行一段代码在我设定的某个时间,但问题是程序无法理解用于比较的时间变量(没有任何错误,但它是没有按应有的方式工作)并且当程序达到 if 语句中的限制时它不会停止,因此它会继续打印我不想要的小时和分钟。如果有人帮助我,我将不胜感激。
from datetime import time, datetime
while True:
now = datetime.now()
end = datetime(2020, 9, 29, 16, 37, 5, 850000)
now_str = str(now)
end_str = str(end)
diff = end - now
print(diff)
if now_str == end_str:
break
其余代码将在 if 语句之后。
您可以比较时间戳本身,而不是比较字符串。因为你想 运行 代码直到到达结束时间戳,所以使用 >= 进行比较而不是 ==,因为代码可能不会在确切的时刻评估结束时间戳并且永远不会等于条件遇见了
您的代码应如下所示:
from datetime import time, datetime
while True:
now = datetime.now()
end = datetime(2020, 9, 30, 15, 36, 5, 850000)
diff = end - now
print(diff)
if now >= end:
break
您应该能够 compare datetimes directly 使用 < 和 >:
>>> n1 = datetime.now()
>>> n2 = datetime.now()
>>> n1
datetime.datetime(2020, 9, 30, 9, 34, 32, 350522)
>>> n2
datetime.datetime(2020, 9, 30, 9, 34, 39, 373229)
>>> n1 < n2
True
你应该避免使用 ==
和 datetime.now()
来结束循环,因为它们非常精确,微秒的差异可能意味着你超过了计划的结束时间。请改用 if now >= end:
。
这是另一个建议。
from datetime import datetime
import time
now = datetime.now()
end = datetime(year=2020, month=9, day=30, hour=15, minute=40, second=0, microsecond=850000)
diff = end - now
print("trying to sleep for {} seconds!".format(diff.total_seconds()))
try:
time.sleep(diff.total_seconds())
except:
# when the difference "diff" is negative, the sleep() function with raise an exception
print("Oups, No time to sleep!")
print("Run the code here...!")
像这样使用循环等待时间结束并不是一个好主意。由于这些指令将重复执行,同时尝试尽可能多地使用可用的 CPU 直到条件为真。
我认为您最好使用 time.sleep ()
。让操作系统为您处理。
我写了一个程序运行一段代码在我设定的某个时间,但问题是程序无法理解用于比较的时间变量(没有任何错误,但它是没有按应有的方式工作)并且当程序达到 if 语句中的限制时它不会停止,因此它会继续打印我不想要的小时和分钟。如果有人帮助我,我将不胜感激。
from datetime import time, datetime
while True:
now = datetime.now()
end = datetime(2020, 9, 29, 16, 37, 5, 850000)
now_str = str(now)
end_str = str(end)
diff = end - now
print(diff)
if now_str == end_str:
break
其余代码将在 if 语句之后。
您可以比较时间戳本身,而不是比较字符串。因为你想 运行 代码直到到达结束时间戳,所以使用 >= 进行比较而不是 ==,因为代码可能不会在确切的时刻评估结束时间戳并且永远不会等于条件遇见了
您的代码应如下所示:
from datetime import time, datetime
while True:
now = datetime.now()
end = datetime(2020, 9, 30, 15, 36, 5, 850000)
diff = end - now
print(diff)
if now >= end:
break
您应该能够 compare datetimes directly 使用 < 和 >:
>>> n1 = datetime.now()
>>> n2 = datetime.now()
>>> n1
datetime.datetime(2020, 9, 30, 9, 34, 32, 350522)
>>> n2
datetime.datetime(2020, 9, 30, 9, 34, 39, 373229)
>>> n1 < n2
True
你应该避免使用 ==
和 datetime.now()
来结束循环,因为它们非常精确,微秒的差异可能意味着你超过了计划的结束时间。请改用 if now >= end:
。
这是另一个建议。
from datetime import datetime
import time
now = datetime.now()
end = datetime(year=2020, month=9, day=30, hour=15, minute=40, second=0, microsecond=850000)
diff = end - now
print("trying to sleep for {} seconds!".format(diff.total_seconds()))
try:
time.sleep(diff.total_seconds())
except:
# when the difference "diff" is negative, the sleep() function with raise an exception
print("Oups, No time to sleep!")
print("Run the code here...!")
像这样使用循环等待时间结束并不是一个好主意。由于这些指令将重复执行,同时尝试尽可能多地使用可用的 CPU 直到条件为真。
我认为您最好使用 time.sleep ()
。让操作系统为您处理。