我正在编写一个程序,如果现在==特定的时间和日期,它将执行某些操作,如果不相等,请等到那个时间.
我有一个早,晚,天值,该值显示开始时间,结束时间和今天.我想制作一个程序,检查今天是否是工作日和特定时间(早晨和晚上之间),如果为True,则执行某项操作,甚至其中之一为False,则要等到该时间之后再执行某项操作.
我用while循环来完成它,但是当它以False值(不是在正确的时间)开始时,即使该时间到了并且值应该更改为True,但它继续打印False,但是当我在正确的时间启动它时,它显示True.
这是代码:
import datetime
from datetime import date
from time import sleep
#setting values
now = datetime.datetime.now()
morning = now.replace(hour=9, minute=00, second=0, microsecond=0)
evening = now.replace(hour=16, minute=0, second=0, microsecond=0)
days = now.strftime("%A")
#check if time is in range and return True or False
def time_in_range(morning, evening, x):
if morning <= evening:
return morning <= x <= evening
else:
return morning <= x or x <= evening
timerange = time_in_range(morning, evening, now)
#check if today is weekday
if date.today().weekday() == 0:
dayz = True
else:
dayz = False
# If today weekday and time range do something
if dayz == True and timerange == True:
print("Yes")
while dayz == False or timerange == False:
sleep(5)
timerange = time_in_range(morning, evening, now)
print(timerange) #this printing false even if is right time.
# But it shows True when I turn it on in right time