虽然Loop无法与时间配合良好

时间:2019-04-12 01:03:20

标签: python python-3.x loops date time

我有一个包含此↓部分的代码,当时间和日期到来时,它应该执行某些操作。它具有outerinner时间和日期检查(while loops)。 Outer用于代码在timerange不在代码处理的时间(或日期)内开始处理的情况-should等到该时间(或日期)。它有效,但仅在我将其设置为timerange前10分钟(代码处理时间)的情况下有效。当我将其设置为前一天晚上时,它将无法工作,并且会在print("timerange wait")中停止,即使它应该为True,它也会显示False。是否因为不在同一天而有问题?或者是其他东西?请给我一些建议。

这是简单的代码版本(没有不必要的部分)


import os
import datetime
from time import sleep
from datetime import date



#setting used variables
now = datetime.datetime.now()
morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
evening = now.replace(hour=18, minute=9, second=0, microsecond=0)


# function for check creating "timerange -start time, end time"
def time_in_range(morning, evening, x):
    if morning <= evening:
        return morning <= x <= evening
    else:
        return morning <= x or x <= evening


# main program for doing something
def main():
    while True:
# Time period check - Check if time and weekdays are not out of range
# if not in range will wait till the time
        if date.today().weekday():
            dayz = True
        else:
            dayz = False
        now = datetime.datetime.now()
        timerange = time_in_range(morning, evening, now)
        if timerange != True:
            print("waiting for function-time")
            sleep(10)
            continue
        if dayz != True:
            print("waiting for function-days")
            sleep(3600)
            continue

        while True:
            print("do something")
#check if during while loop there wasn't date, time change (out of timerange etc)
            if date.today().weekday():
                dayz = True
            else:
                dayz = False
            now = datetime.datetime.now()
            timerange = time_in_range(morning, evening, now)
            if timerange != True:
                print("break inner loop-timerange")
                break
            if dayz != True:
                print("break inner loop-dayz")
                break
            print("do something -main work")



#program starts here        
timerange = time_in_range(morning, evening, now)


# check if today is weekday (outer loop)
if date.today().weekday():
    dayz = True
else:
    dayz = False

# if today is not a weekday wait till weekday (outer loop) 
#for situation where the code started not in weekday
while dayz != True:
    print("days wait")
    sleep(3600)
    dayz = date.today().weekday()
    if dayz == True:
        break
# If it is not processing time, wait until it becomes
# for the situation where code started not in "timerange"
while timerange != True:
    print("timerange wait") # ---------HERE IT STOPS---------
# But it works fine, when I set it like one hour before 'timerange' or so...
    sleep(10)
    now = datetime.datetime.now()
    timerange = time_in_range(morning, evening, now)
    print(timerange)
    if timerange == True:
        break

# Do the main function
main()

# Let know when code ends
print("Over of Code")


0 个答案:

没有答案