implement cron_prev - #52
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The counterpart of
cron_next: the last time matching an expression that is strictly earlier than the one given. Same three argument types, same failure sentinels.It is answered by calling
cron_nextrepeatedly rather than by searching backwards, so there is one implementation of the calendar rules instead of two — a mirror-image search would have had to restate every DST correction, gap guard and monotonicity rule the forward one already carries.cron_nextis monotonic, so "the next occurrence is still earlier than the time asked about" is true up to the answer and false after it, and that boundary can be bisected. A ladder of trial intervals - a minute, an hour, a day, a month, a year, the four-year horizon, brackets the answer first so that common expressions don't pay for the full search.Cost, measured rather than estimated:
34 is the ceiling: 6 probes, 27 halvings, 1 final lookup. The count follows the width of the interval searched, not how often the expression fires -
* * * 1 1 ?has 86,400 occurrences inside the interval and still costs 31. A forward walk through those occurrences, which is the obvious implementation, would have cost 86,401. Documented in the README along with the guidance that follows: fine for a single lookup, wrong tool for walking history, where iteratingcron_nextforward is the answer.cron_prevreaches back four years, the same horizoncron_nextsearches forward; anything older reports as no occurrence.New
test_prev.cpp, including a property test that for each of seven expressions over five successive steps the answer is earlier than the time asked about and nothing matches in between.Fixes #5.