Here's a little proof you can run:
package com.example;
import org.junit.jupiter.api.Test;
import org.springframework.scheduling.support.CronExpression;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class CronExpressionBugProof {
@Test
public void proveCronExpressionIsBroken() {
CronExpression expression = CronExpression.parse("0 15 12 */10 1-8 5");
LocalDateTime a = LocalDateTime.ofEpochSecond(1619784899L, 0, ZoneOffset.UTC);
LocalDateTime b = LocalDateTime.ofEpochSecond(1619784900L, 0, ZoneOffset.UTC);
LocalDateTime nextA = expression.next(a);
LocalDateTime nextB = expression.next(b);
System.out.println("If I ask for the next match of \"" + expression + "\" on " + a + ", I get " + nextA);
System.out.println("If I ask for the next match of \"" + expression + "\" on " + b + ", one second later, I get " + nextB);
System.out.println("This is bad.");
}
}
This prints
If I ask for the next match of "0 15 12 */10 1-8 5" on 2021-04-30T12:14:59, I get 2021-06-11T12:15
If I ask for the next match of "0 15 12 */10 1-8 5" on 2021-04-30T12:15, one second later, I get 2021-05-21T12:15
This is bad.
As you can see, something is deeply wrong with CronExpression
. As you move forward in time, the next match of a cron expression should never move backwards, especially not by 3 whole weeks.
Let me know if you need any more info.
Comment From: sbrannen
Thanks for raising the issue and providing the test case.
Let me know if you need any more info.
I think that's sufficient for the team to act on.