cron-utils icon indicating copy to clipboard operation
cron-utils copied to clipboard

Bug report: Incorrect next execution date calculation for "0 15 10 ? * 6#3"

Open liuyuquan150 opened this issue 1 year ago • 3 comments

Bug: Incorrect next execution date calculation for "0 15 10 ? * 6#3"

Description

I encountered a bug in cron-utils version 9.2.0 when calculating the next execution date for the cron expression "0 15 10 ? * 6#3" using CronType.SPRING53. The expected result should be July 19th, 2024, but the method returns July 20th, 2024.

Steps to Reproduce

  1. Use the following code to parse the cron expression and calculate the next execution time:

    import com.cronutils.model.Cron;
    import com.cronutils.model.definition.CronDefinitionBuilder;
    import com.cronutils.model.time.ExecutionTime;
    import com.cronutils.parser.CronParser;
    import com.cronutils.descriptor.CronDescriptor;
    import com.cronutils.model.CronType;
    
    import java.time.ZonedDateTime;
    import java.time.LocalDateTime;
    import java.util.Locale;
    import java.util.Optional;
    
    public class Main {
        public static void main(String[] args){
            CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING53));
            Cron quartzCron = parser.parse("0 15 10 ? * 6#3");
            ZonedDateTime now = ZonedDateTime.now();
            ExecutionTime executionTime = ExecutionTime.forCron(quartzCron);
            Optional<ZonedDateTime> zonedDateTimeOptional = executionTime.nextExecution(now);
            if (zonedDateTimeOptional.isPresent()) {
                ZonedDateTime zonedDateTime = zonedDateTimeOptional.get();
                LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
                System.err.println(localDateTime);
            }
        }
    }
    
  2. Run the code. The expected result should be the third Friday of the current month (July 2024), which is July 19th, 2024, at 10:15 AM local time.

Actual Result

The method returns July 20th, 2024, instead of July 19th, 2024.

Environment Information

  • cron-utils version: 9.2.0
  • Java version: 17
  • Operating system: Windows 10

Expected Result

The next execution date for the cron expression "0 15 10 ? * 6#3" should be July 19th, 2024, at 10:15 AM local time.

Additional Information

Below is the output of the code:

2024-07-20T10:15

liuyuquan150 avatar Jul 04 '24 22:07 liuyuquan150

~~looks to be be duplicate of #605~~

austek avatar Jul 08 '24 10:07 austek

@austek, isn't this expected behavior? We are trying to parse a Quartz cron expression using a Spring parser. Since 6#3 represents the 3rd Friday in Quartz and the 3rd Saturday in Spring, it seems like this is just a misunderstanding. The next execution time should infact be 20th July 2024 (3rd Saturday of the month). Or am I missing something?

msajawalsial avatar Jul 23 '24 10:07 msajawalsial

@msajawalsial you're right, this is the expected behaviour, https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions#second-friday-of-the-month

austek avatar Jul 23 '24 11:07 austek