Skip to content

Fixed backward incompatible change in php8.2 #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Cron/HoursField.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,37 +167,37 @@ public function increment(DateTimeInterface &$date, $invert = false, $parts = nu
if (! $invert) {
if ($originalHour >= $target) {
$distance = 24 - $originalHour;
$date = $this->timezoneSafeModify($date, "+{$distance} hours");
$date = $this->timezoneSafeModify($date, "{$distance} hours");

$actualDay = (int)$date->format('d');
$actualHour = (int)$date->format('H');
if (($actualDay !== ($originalDay + 1)) && ($actualHour !== 0)) {
$offsetChange = ($previousOffset - $date->getOffset());
$date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds");
$date = $this->timezoneSafeModify($date, "{$offsetChange} seconds");
}

$originalHour = (int)$date->format('H');
}

$distance = $target - $originalHour;
$date = $this->timezoneSafeModify($date, "+{$distance} hours");
$date = $this->timezoneSafeModify($date, "{$distance} hours");
} else {
if ($originalHour <= $target) {
$distance = ($originalHour + 1);
$date = $this->timezoneSafeModify($date, "-" . $distance . " hours");
$date = $this->timezoneSafeModify($date, (-$distance) . " hours");

$actualDay = (int)$date->format('d');
$actualHour = (int)$date->format('H');
if (($actualDay !== ($originalDay - 1)) && ($actualHour !== 23)) {
$offsetChange = ($previousOffset - $date->getOffset());
$date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds");
$date = $this->timezoneSafeModify($date, "{$offsetChange} seconds");
}

$originalHour = (int)$date->format('H');
}

$distance = $originalHour - $target;
$date = $this->timezoneSafeModify($date, "-{$distance} hours");
$date = $this->timezoneSafeModify($date, (-$distance) . " hours");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accordingly to relative formats docs this should be "hours ago"

What you wrote is just different syntax, it is being interpreted the same way - see https://3v4l.org/KRVLH

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mabar Yes, right, but it will work incorrect if $distance is negative.
For example if we are calling modify("--10 hours), we expect it like "+10 hours", but actually since php8.2 it will be working like "-10 hours": https://3v4l.org/X1XJd#v8.3.4

}

$date = $this->setTimeHour($date, $invert, $originalTimestamp);
Expand Down
8 changes: 4 additions & 4 deletions src/Cron/MinutesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ public function increment(DateTimeInterface &$date, $invert = false, $parts = nu
if (! $invert) {
if ($originalMinute >= $target) {
$distance = 60 - $originalMinute;
$date = $this->timezoneSafeModify($date, "+{$distance} minutes");
$date = $this->timezoneSafeModify($date, "{$distance} minutes");

$originalMinute = (int) $date->format("i");
}

$distance = $target - $originalMinute;
$date = $this->timezoneSafeModify($date, "+{$distance} minutes");
$date = $this->timezoneSafeModify($date, "{$distance} minutes");
} else {
if ($originalMinute <= $target) {
$distance = ($originalMinute + 1);
$date = $this->timezoneSafeModify($date, "-{$distance} minutes");
$date = $this->timezoneSafeModify($date, (-$distance) . " minutes");

$originalMinute = (int) $date->format("i");
}

$distance = $originalMinute - $target;
$date = $this->timezoneSafeModify($date, "-{$distance} minutes");
$date = $this->timezoneSafeModify($date, (-$distance) . " minutes");
}

return $this;
Expand Down
28 changes: 28 additions & 0 deletions tests/Cron/DaylightSavingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,32 @@ public function testOffsetDecrementsMidnight(): void
$this->assertContainsEquals($dtExpected, $actual);
}
}

public function testOffsetIncrementDailyMidnight(): void
{
$expression = '15 0 * * *';
$cron = new CronExpression($expression);
$tz = new \DateTimeZone("America/Los_Angeles");

$expected = [
$this->createDateTimeExactly("2024-03-08 00:15-08:00", $tz),
$this->createDateTimeExactly("2024-03-09 00:15-08:00", $tz),
$this->createDateTimeExactly("2024-03-10 00:15-08:00", $tz),
$this->createDateTimeExactly("2024-03-11 00:15-07:00", $tz),
$this->createDateTimeExactly("2024-03-12 00:15-07:00", $tz),
$this->createDateTimeExactly("2024-03-13 00:15-07:00", $tz),
];

$dtCurrent = $this->createDateTimeExactly("2024-03-08 00:15-08:00", $tz);
$actual = $cron->getMultipleRunDates(6, $dtCurrent, false, true, $tz->getName());
foreach ($expected as $dtExpected) {
$this->assertContainsEquals($dtExpected, $actual);
}

$dtCurrent = $this->createDateTimeExactly("2024-03-13 00:15-07:00", $tz);
$actual = $cron->getMultipleRunDates(6, $dtCurrent, true, true, $tz->getName());
foreach ($expected as $dtExpected) {
$this->assertContainsEquals($dtExpected, $actual);
}
}
}
Loading