dbader / schedule

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

Strange timezone behavior #579

Closed martin-marko closed 9 months ago

martin-marko commented 1 year ago

I have a job that I want to schedule for every midnight in US/Central:

import schedule
from datetime import datetime

def job():
    return True

schedule.every().day.at("00:00", "US/Central").do(job)

print(
    f"Current local time: {datetime.now()}\n"
    f"Next run time: {schedule.jobs[0].next_run}\n"
    f"Idle time: {round(schedule.idle_seconds() / 3600)} hours"
)

If I start the script at 4:50 AM in my local timezone Europe/Budapest (yesterday at 9:50 PM in US/Central), it will result in the following output:

Current local time: 2023-04-14 04:50:31.130487
Next run time: 2023-04-15 07:00:00
Idle time: 26 hours

The library skips the closest midnight, which in the example should be today (in my timezone) 2 hours later, not 1 day and 2 hours later. How am I supposed to start with the next closest US/Central midnight?

If I change it to schedule.every().day.at("05:00", "US/Central").do(job), then the output is the following:

Current local time: 2023-04-14 04:50:38.652124
Next run time: 2023-04-14 12:00:00
Idle time: 7 hours

Which is the correct closest 5:00 AM in US/Central (12:00 AM in Europe/Budapest).

AnezeR commented 1 year ago

Please, see if fixed in #583

SijmenHuizenga commented 9 months ago

Thank you for the clean write-up and example code. Bug has been fixed in #583 and released in 1.2.1. A test based on your sample has been added as well.