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.14k stars 261 forks source link

lastExecution not correct #403

Open aibany opened 4 years ago

aibany commented 4 years ago

图片

Current System time is 2019-10-30 12:16:00 My cron is: 0 0/2 * ? . means every 2 hours execute one times. My cron-utils version is :9.0.2

but the calc result nextTime and lastTime is both incorrect.

aibany commented 4 years ago

Even my cron-utils version is 8.0.0, both the problem exsisted.

jmrozanec commented 4 years ago

@aibany thank you for reporting this. May we ask you to submit a PR with the corresponding tests? Thanks!

aibany commented 4 years ago
public static void main(String[] args) {

        CronDefinition defaultDef = CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ);
        CronDescriptor descriptor = CronDescriptor.instance(Locale.CHINESE);
        CronParser parser = new CronParser(defaultDef);

        String str = "0 * 0/2 * * ?";

        String description = descriptor.describe(parser.parse(str));

        ZonedDateTime now = ZonedDateTime.now();
        ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(str));
        Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(now);
        Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(now);
        String sb = nextExecution.get().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(sb);
        System.out.println("My machine time:" + new Date().toString());
    }

this console print is : 2019-10-30 18:09:00 My machine time:Wed Oct 30 18:08:50 CST 2019

jmrozanec commented 4 years ago

@aibany can you push this as a test into a PR? Thanks!

aibany commented 4 years ago

@brusazorzo thank you.

IndeedSi commented 3 years ago

Looking at the example, I think what was causing confusion was the description. 0 * 0/2 * * ? should be every minute every 2 hours instead of simply every 2 hours

And I think the last and next execution time works just as expected since it's being triggered every minute. I added a test case for this issue.

jmrozanec commented 3 years ago

@IndeedSi thank you! Expected executions are ok, the description is misleading.