Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DST transitions cause APScheduler to incorrectly calculate next trigger times #529

Open
rodrigoformi-zh opened this issue Jul 20, 2021 · 1 comment
Labels

Comments

@rodrigoformi-zh
Copy link

rodrigoformi-zh commented Jul 20, 2021

Description
DST transitions cause APScheduler to incorrectly calculate next trigger times.

Given:
a cron trigger of month='', day='', day_of_week='0-4', hour='0', minute='0',
a last trigger time of None
a current time of pd.Timestamp(2021, 3, 12, 0, 0,0,1,0,pytz.timezone('US/Central'))

APScheduler returns '2021-03-16T00:00:00-05:00' for the cron_trigger's get_next_fire_time() call.

NOTE: there is a DST transition CST -> CDT at 2AM on 14 March.

from apscheduler.triggers.cron import CronTrigger
import pytz
import pandas as pd

cron_trigger = CronTrigger.from_crontab('0 0 * * 0-4', pytz.timezone('US/Central'))
now = pd.Timestamp(2021, 3, 12, 0, 0,0,1,0,pytz.timezone('US/Central'))
print(cron_trigger.get_next_fire_time(None,now).isoformat())

Expected behavior
To return '2021-03-15T00:00:00-05:00' not '2021-03-16T00:00:00-05:00'

Additional context
The APScheduler library determines the next fire time by going through each part of the expression and finding the next viable value iteratively until all parts of the expression are satisfied. When the day is incremented, 24 hours are added. This causes the time to go forward 1 day and 1 hour as DST causes a day to only contain 23 hours.

It seems that the _increment_field_value function needs to be corrected to accommodate DST changes
NOTE: the same behavior doesn't happen on CDT->CST transitions

@agronholm
Copy link
Owner

Seems like this is an unfortunate corner case that nobody else has come across these last 12 years.

If I adjust the code to make it work with this particular case, two other cases start failing in the test suite.

There are 4 test cases in the test suite that should have ensured correct operation across DST boundaries (here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants