poliander / cron

Parse and validate crontab expressions in PHP
https://github.com/poliander/cron
GNU General Public License v3.0
63 stars 10 forks source link

Date slip with getNext() (again) #12

Closed ThomasPerraudin closed 2 years ago

ThomasPerraudin commented 2 years ago

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

ThomasPerraudin commented 2 years ago

I would like to help but I have difficulties to clearly understand your code. I guess that the issue may be in adjust() or forward().

poliander commented 2 years ago

I admit that there is still something fishy. The basic idea was: the timestamp (both the parameter and the return value of getNext) is always UTC/GMT while the timezone specified in the constructor parameter is the timezone the cron expression refers to.

poliander commented 2 years ago

Oh, I think the calculation routine has indeed a problem.

poliander commented 2 years ago

Very nice find, please try v3.0.1 or v2.4.3. I also added unit tests for your cases.

$expr = new CronExpression('0 3 * * *', new DateTimeZone('Europe/Paris'));
$ts = 1649549102;

echo date('c', $ts) . "\n";
echo date('c', $expr->getNext($ts)) . "\n";

Output:

2022-04-10T02:05:02+02:00
2022-04-10T03:00:00+02:00