Skip to content

implement cron_prev - #52

Merged
mariusbancila merged 1 commit into
masterfrom
feature/cron-prev
Aug 12, 2026
Merged

implement cron_prev#52
mariusbancila merged 1 commit into
masterfrom
feature/cron-prev

Conversation

@mariusbancila

Copy link
Copy Markdown
Owner

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.

std::time_t previous = cron::cron_prev(cron, std::time(0));

It is answered by calling cron_next repeatedly 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_next is 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:

 8 calls   * * * * * *        every second
21 calls   0 15 10 * * ?      daily
31 calls   * * * 1 1 ?        every second of 1 January
34 calls   0 0 0 29 2 *       leap day

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 iterating cron_next forward is the answer.

cron_prev reaches back four years, the same horizon cron_next searches 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.

@mariusbancila
mariusbancila merged commit 804e7f9 into master Aug 12, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get last execution time

1 participant