0

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")
3
  • By the way: dayz = 0 <= date.today().weekday() < 5. Commented Apr 17, 2019 at 1:31
  • When you start running this code at 20:00 today, the morning variable points to 8am today and will never be updated in the loop. So when the date is tomorrow it will point to yesterday. Commented Apr 17, 2019 at 1:36
  • @chepner They are defined, you should use the scroll bar. Commented Apr 17, 2019 at 1:37

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer! I would like to use the datetime.time, but how can I make it now? Because the datetime.time has no now attribute
datetime.datetime.now().time() will give you a datetime.time object corresponding to the time portion of what you get from .now().
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.