Python -While loop doesn't work with Time

时间:2019-04-17 01:28:03

标签: python python-3.x date while-loop

I have a code which starts a main function. In this function have a while loop which should starts program when certain time comes. I have set this time in morning (start time), evening(end time) variables. It is in while loop and it works, but only if I start the program the day I want to use it. For example: When I start it Monday evening (20:00) and start time(morning variable) is from 8:00 (next day), it will continue loop

print("Waiting for the right time") <=(doing this)

even if that time the next day comes. But It works when I start it the next day at 6:00 or so...

Can someone explain me, why this happens? Here is the code

import datetime
from time import sleep
from datetime import date


#variables
now = datetime.datetime.now()
morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
evening = now.replace(hour=16, minute=15, second=0, microsecond=0)

#function for time-setting
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)


#main function
def main():
    while True:
# Time period check
        if date.today().weekday() < 5 and date.today().weekday() >= 0:
            dayz = True
        else:
            dayz = False
        if dayz != True:
            print("Waiting for the day")
            sleep(3600)
            continue
        now = datetime.datetime.now()
        timerange = time_in_range(morning, evening, now)
        if timerange != True:  # HERE IT MAKES THE TROUBLE
            print("Waiting for the right time")
            sleep(200)
            continue
        print("do something")

main()

print("end of code")

2 个答案:

答案 0 :(得分:2)

When you call .replace() to set the morning and evening times, it keeps the current date as part of the datetime object. So if you were to call it a day before, the dates would be set to the previous day's date, and thus .now() will never be in between the previous day's time range.

E.g. if on January 1st you make the calls to set morning and evening, the stored datetimes will be "January 1st 8am" and "January 1st 4pm". The next time when your loop is checking, it asks "Is January 2nd 10am between January 1st 8am and January 1st 4pm" and of course the answer is no, because January 1st was the day before.

You probably want to use the datetime.time class instead of the datetime.datetime class, if you're only wanting to check for time. Alternatively, you could set the date portion of your evening and morning datetimes to the specific date you want to match (but that wouldn't help for repeating weekly).

答案 1 :(得分:0)

import datetime
from time import sleep
from datetime import date




#variables
now = datetime.datetime.now()
morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
evening = now.replace(hour=16, minute=15, second=0, microsecond=0)

#function for time-setting
def time_in_range(morning, evening, x):
    # Updated code
    morning = x.replace(hour=8, minute=0, second=0, microsecond=0)
    evening = x.replace(hour=16, minute=15, second=0, microsecond=0)
    if morning <= evening:
        return morning <= x <= evening
    else:
        return morning <= x or x <= evening


timerange = time_in_range(morning, evening, now)
print(timerange)




#main function
def main():
    while True:
# Time period check
        if date.today().weekday() < 5 and date.today().weekday() >= 0:
            dayz = True
        else:
            dayz = False
        if dayz != True:
            print("Waiting for the day")
            sleep(3600)
            continue
        now = datetime.datetime.now()
        timerange = time_in_range(morning, evening, now)
        if timerange != True:  # HERE IT MAKES THE TROUBLE
            print("Waiting for the right time")
            sleep(200)
            continue
        print("do something")

main()

print("end of code")