dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.71k stars 960 forks source link

Why is every 5 hours is 06:00? #538

Closed AGDholo closed 1 year ago

AGDholo commented 2 years ago

https://schedule.readthedocs.io/en/stable/examples.html

# Run jobs every 5th hour, 20 minutes and 30 seconds in.
# If current time is 02:00, first execution is at 06:20:30
schedule.every(5).hours.at("20:30").do(job)

If I want set a schedule at the 00:00 - 04:00 - 08:00 - 12:00 ...

so, is this true?

schedule.every(3).hours.do(short)
SijmenHuizenga commented 1 year ago
schedule.every(4).hours.at("00:00").do(short)

When writing every.hour, the .at("00:00") relates to minutes:seconds. In this case we are telling schedule to run the job at the 0 minutes and 0 seconds of every 4th hour.

If we are running this code at 09:30, the first execution will be at 13:00. This is because 09:00 is the 0th hour (current hour), 10:00 is the 1st hour and so is 13:00 is the 4th hour.

Now, if you need to run at exactly 00:00 - 04:00 - 08:00 - 12:00 ... you could do something like this:

def run_job_if_hour_multiple_of_4():
    current_hour = datetime.now().hour
    if current_hour % 4 == 0:
        job()

schedule.every(1).hour.at("00:00").do(run_job_if_hour_multiple_of_4)