jmrozanec / cron-utils

Cron utils for parsing, validations and human readable descriptions as well as date/time interoperability.
http://cron-utils.com
Apache License 2.0
1.13k stars 263 forks source link

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

Open liuyuquan150 opened 3 weeks ago

liuyuquan150 commented 3 weeks ago

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

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
austek commented 3 weeks ago

looks to be be duplicate of #605

msajawalsial commented 1 week ago

@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?

austek commented 1 week ago

@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