Code Monkey home page Code Monkey logo

cron's Introduction

PHP Cron Expression Parser

Build Status Code Coverage License Latest Stable Version Total Downloads

Standard (V7) compliant crontab expression parser/validator with support for time zones; see "man 5 crontab" for possible expressions.

Installation

Using composer, add a requirement for poliander/cron to your composer.json file:

composer require poliander/cron

Examples

Validate a certain crontab expression:

use Poliander\Cron\CronExpression;

$expression = new CronExpression('15,45 */2 * * *');
$isValid = $expression->isValid(); // returns true

Check whether a given point in time is matching a certain cron expression:

use Poliander\Cron\CronExpression;

$expression = new CronExpression('45 9 * * *');
$dt = new \DateTime('2014-05-18 09:45');
$isMatching = $expression->isMatching($dt); // returns true

Match an expression across different time zones:

use Poliander\Cron\CronExpression;

$expression = new CronExpression('45 9 * * *', new DateTimeZone('Europe/Berlin'));
$dt = new \DateTime('2014-05-18 08:45', new DateTimeZone('Europe/London'));
$isMatching = $expression->isMatching($dt); // returns true

Calculate next timestamp matching a Friday, the 13th:

use Poliander\Cron\CronExpression;

$expression = new CronExpression('* * 13 * fri');
$when = $expression->getNext();

Supported PHP Versions

cron PHP Note
1.2.* 5.6 end of life
2.0.* 7.0 end of life
2.1.* 7.1 end of life
2.2.* 7.2 end of life
2.3.* 7.3 end of life
2.4.* 7.4 - 8.2 end of life
3.0.* 7.4 - 8.2 end of life
3.1.* 8.1 - 8.3

Changelog

version release notes
1.0.0 (2015-06-20) initial release
1.1.0 (2016-06-11) dropped PHP 5.4 support
1.2.0 (2016-12-11) added PHP 7.1 support
1.2.1 (2017-05-25) fixed #3
1.2.2 (2017-06-03) fixed #3, #4
2.0.0 (2017-11-30) dropped PHP 5.x, added PHP 7.2 support, added vendor namespace (closes #2)
2.1.0 (2018-12-08) dropped PHP 7.0, added PHP 7.3 support, updated PHPUnit dependency to 7.*
2.2.0 (2019-12-03) dropped PHP 7.1, added PHP 7.4 support, updated PHPUnit dependency to 8.*
2.3.0 (2020-12-29) dropped PHP 7.2, added PHP 8.0 support, updated PHPUnit dependency to 9.*
2.3.1 (2021-10-04) fixed #6
2.4.0 (2021-12-27) dropped PHP 7.3, added PHP 8.1 support
2.4.1 (2022-03-25) fixed #9
2.4.2 (2022-04-09) fixed #9, #10, #11
2.4.3 (2022-04-10) fixed #12
2.4.4 (2022-04-11) fixed #13
2.4.5 (2022-12-17) fixed #14
2.4.6 (2022-12-29) added PHP 8.2 support
2.4.7 (2023-01-20) fixed #16
2.4.8 (2023-07-30) fixed #18, #19
2.4.9 (2023-10-28) fixed #22
3.0.0 (2022-04-09) namespace changed to avoid package conflict (closes #8)
3.0.1 (2022-04-10) fixed #12
3.0.2 (2022-04-11) fixed #13
3.0.3 (2022-12-17) fixed #14
3.0.4 (2022-12-29) added PHP 8.2 support
3.0.5 (2023-01-20) fixed #16
3.0.6 (2023-07-30) fixed #18, #19
3.0.7 (2023-10-28) fixed #22
3.1.0 (2023-11-23) added PHP 8.3 support, dropped PHP 7/8.0 support, updated PHPUnit to 10.*

cron's People

Contributors

onlime avatar poliander avatar simoheinonen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cron's Issues

Compatibility issue with PHP 8.2

Change function parseRange to this:
Its impossible pass string $range with array.

private function parseRange(array &$register, int $index, string $range, int $stepping): void
    {
        if ($range === '*') {
            $rangeArr = [self::VALUE_BOUNDARIES[$index]['min'], self::VALUE_BOUNDARIES[$index]['max']];
        } else {
            $rangeArr = explode('-', $range);
        }

        $this->validateRange($rangeArr, $index);
        $this->fillRange($register, $index, $rangeArr, $stepping);
    }

Looping with getNext()

$t = null;

for ($i = 0; $i < 20; $i++) {
    $c = new \Cron('0 1 * * *');
    echo date('Y-m-d H:i:s', $t = $c->getNext($t)) . "<br />";
}

Expected:

2017-06-04 01:00:00
2017-06-05 01:00:00
...

Actual:

2017-06-04 01:00:00
2017-06-04 01:00:00
...

getNext() error result

$c = new \Cron('0 3 * * *');
echo $c->getNext();

Executed at 2022-03-30 01:55:01
Expected:

2022-03-30 03:00:00

Actual:

2022-03-30 05:00:00

Add a namespace

The package is missing a namespace and could possibly use a more on-point name (like CronExpression or something).
It's a small issue for a great tool, thanks! Saved me a bunch of time.

Next execution date skips a year if configured for January 1st at 00:00, but only from December 1st at 00:01

I've run into a strange problem while calculating the next execution date. I have a cron configured for January 1st at 00:00. It was working well before, saying that next execution would be on 2023-01-01 00:00. Today, however, it says 2024-01-01 00:00.

Using the existing test for the class I was able to pinpoint the breaking point. The calculation for next year works well until December 1st at 00:00:59. Past this time, the class will calculate the next execution skipping one year.

To reproduce, you can add this to getNextProvider on CronExpressionTest.php:

['0 0 1 1 *', 1669849200, 1672527600], // on 2022-12-01 00:00:00, next execution at 2023-01-01 00:00:00

This one works well. 1669849200 is December 1st, 2022 at 00:00. 1672527600 is January 1st, 2023 at 00:00, as expected.

['0 0 1 1 *', 1669849260, 1672527600], // on 2022-12-01 00:01:00, next execution at 2024-01-01 00:00:00

This one fails. 1669849260 is one minute later, December 1st, 2022 at 00:01. This results in 1704063600, which corresponds to January 1st, 2024 at 00:00.

Support for integers starting with zero

Hi there,

I found a bug with cron: 0-30/2 02,06-19 5-30/5 1-11 1-2,4-5.

In fact this is a valid cron, but the ìsValidî method sends false.

I think I have found the origin of this error:
image

Here we are comparing 2 and 02.

But 02 = 2 is an integer.

Date slip with getNext()

Similar #9

$c = new \Cron('0 3 * * *', new \DateTimeZone('Europe/Paris'));
echo $c->getNext();

When run at 1649202901 //2022-04-06 01:55:01

Expected:

1649206800 //2022-04-06 03:00:00

Actual:

1649206800 //2022-04-06 03:00:00 OK


When run at 1649203201 //2022-04-06 02:00:01

Expected:

1649206800 //2022-04-06 03:00:00

Actual:

1649293200 //2022-04-07 03:00:00 ERROR

getNext() errors

Thank you for the v3.0.1. It works better.
But I think that I found 2 new errors.

$tests = [
  [
    "expression" => '0 10 * * *',
    "timezone" => 'Europe/Paris',
    "now" => "2022-04-11T10:00:00+02:00", //1649664000
    "expected" => "2022-04-12T10:00:00+02:00" //1649750400
  ],
  [
    "expression" => '0 10 * * *',
    "timezone" => 'Europe/Paris',
    "now" => "2022-04-11T10:02:00+02:00", //1649664120
    "expected" => "2022-04-12T10:00:00+02:00" //1649750400
  ],
];

foreach($tests as $test){
  $expr = new CronExpression($test['expression'], new DateTimeZone($timezone));
  $now = strtotime($test['now']);
  $test['getNext'] = $expr->getNext($now);
  $test['getNext_c'] = date('c', $test['getNext']);
  $test['result'] = ($test['getNext_c']==$test['expected']?"ok":"** ERROR **");
  print_r($test);
}

Output:

Array
(
    [expression] => 0 10 * * *
    [timezone] => Europe/Paris
    [now] => 2022-04-11T10:00:00+02:00
    [expected] => 2022-04-12T10:00:00+02:00
    [getNext] => 1649714400
    [getNext_c] => 2022-04-12T00:00:00+02:00
    [result] => ** ERROR **
)
Array
(
    [expression] => 0 10 * * *
    [timezone] => Europe/Paris
    [now] => 2022-04-11T10:02:00+02:00
    [expected] => 2022-04-12T10:00:00+02:00
    [getNext] => 1649714400
    [getNext_c] => 2022-04-12T00:00:00+02:00
    [result] => ** ERROR **
)

Date slip with getNext()

$c = new \Cron('0 3 * * *');
echo $c->getNext();

When run at 2022-03-25 01:55:01

Expected:

2022-03-25 03:00:00
...

Actual:

2022-03-25 03:00:00 OK
...


When run at 2022-03-25 02:00:01

Expected:

2022-03-25 03:00:00
...

Actual:

2022-03-26 03:00:00 ERROR
...

Date slip with getNext() (again)

Thank you for the v3.0.0. I don't know if it is linked to #9 and #11 but I still encounter a similar issue :

$c = new \Cron('0 3 * * *', new \DateTimeZone('Europe/Paris'));
echo $c->getNext();

When run at 1649548801 //2022-04-10 02:00:01

Expected:

1649552400 //2022-04-10 03:00:00

Actual:

1649552400 //2022-04-10 03:00:00 OK


When run at 1649549102 //2022-04-10 02:05:02

Expected:

1649552400 //2022-04-10 03:00:00

Actual:

1649638800 //2022-04-11 03:00:00 ERROR

Suggest to use regular expression to check cron rule

Hello,
Sorry, I am not a native English speaker.
I read the repo's source code. This is what I need.
But this code can use regular expression to check cron rule more faster.
This is my regular expression: ^((\d+|(\d+-\d+|\*)(/\d+)?)(,(\d+|(\d+-\d+|\*)(/\d+)?))*|[a-zA-Z]+)$.

Support for PHP 8.3

Please update composer.json to support PHP 8.3

I have tested poliander/cron on latest PHP 8.3.0-dev (Homebrew) and all tests seem to run through fine:

$ php -v
PHP 8.3.0-dev (cli) (built: Oct 27 2023 15:45:44) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.0-dev, Copyright (c), by Zend Technologies

$ ./vendor/bin/phpunit 
PHPUnit 9.6.13 by Sebastian Bergmann and contributors.

...............................................................  63 / 120 ( 52%)
.........................................................       120 / 120 (100%)

Time: 00:00.008, Memory: 6.00 MB

OK (120 tests, 224 assertions)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.