mtdowling / cron-expression

CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due
http://mtdowling.com/blog/2012/06/03/cron-expressions-in-php/
MIT License
4.9k stars 335 forks source link

Errors when Day of the Week and Day of the Month set #1

Closed jerv13 closed 13 years ago

jerv13 commented 13 years ago

I think there my be a bug when both the Day of the Week and the Day of the Month are specified (See results below)

cron: * * * * * ---------------------------------------------- (OK) Next Due: 2011-06-15 23:09:00

cron: 0 17 * * * ---------------------------------------------- (OK) Next Due: 2011-06-16 17:00:00

cron: 0 0 1 1 0 ----------------------------------------------(Should be 2012-01-01 00:00:00) Next Due: 2249-10-01 00:00:00

cron: 0 0 1 * 0 ----------------------------------------------(Should be 2012-01-01 00:00:00) Next Due: 2038-10-31 00:00:00

cron: 59 17 5 7 * ----------------------------------------------(OK) Next Due: 2011-07-05 17:59:00

cron: 00 11 15 6 * ----------------------------------------------(OK) Next Due: 2012-06-15 11:00:00

cron: 00 11 14 6 * ----------------------------------------------()OK Next Due: 2012-06-14 11:00:00

I will look into this and see if I can find a solution.

tucksaun commented 13 years ago

According to Cron, 0 and 7 are Sunday but the DateTime->format('N') returns an integer between 1 and 7. So, in order to be compatible with cron's expressions, you have to :

Michael, I can provide you the patch which correct and test this.

mtdowling commented 13 years ago

Thanks for the bug report, jerv.

Good catch, tucksaun. Providing a patch would be really helpful! I played around with it for a minute, and it looks like intervals will be more difficult than a 5 minute change.

I want to come up with more accurate test cases so that we can catch these types of issues. Anyone know a good implementation of cron that can be used to calculate the next run date of a cron expression?

-Michael

tucksaun commented 13 years ago

Here it is: https://gist.github.com/ad486f026738f1fc534b

Indeed the intervals were more difficult, but I think they are ok (for now). I play with this code all afternoon and it seems ok.

By the way, thank you for this code ;)

Tugdual

mtdowling commented 13 years ago

That looks great, tucksaun. Can you send a pull request?

tucksaun commented 13 years ago

done ;)

mtdowling commented 13 years ago

Thanks for the patch!