Skip to content

Commit

Permalink
Fixed run date calculations when ? is used to skip
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmantank committed Jan 13, 2022
1 parent 9545dea commit 6e0003e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Cron/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ protected function getRunDate($currentTime = null, int $nth = 0, bool $invert =
$domRunDates = $domExpression->getMultipleRunDates($nth + 1, $currentTime, $invert, $allowCurrentDate, $timeZone);
$dowRunDates = $dowExpression->getMultipleRunDates($nth + 1, $currentTime, $invert, $allowCurrentDate, $timeZone);

if ($parts[self::DAY] === '?' || $parts[self::DAY] === '*') {
$domRunDates = [];
}

if ($parts[self::WEEKDAY] === '?' || $parts[self::WEEKDAY] === '*') {
$dowRunDates = [];
}

$combined = array_merge($domRunDates, $dowRunDates);
usort($combined, function ($a, $b) {
return $a->format('Y-m-d H:i:s') <=> $b->format('Y-m-d H:i:s');
Expand Down
20 changes: 20 additions & 0 deletions tests/Cron/CronExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,24 @@ public function testIssue131()
$prev = $e->getPreviousRunDate(new \DateTime('2022-08-20 03:44:02'));
$this->assertEquals($expected, $prev);
}

public function testIssue128()
{
$e = new CronExpression('0 20 L 6,12 ?');
$expected = new \DateTime('2022-12-31 20:00:00');
$next = $e->getNextRunDate(new \DateTime('2022-08-20 03:44:02'));
$this->assertEquals($expected, $next);

$expected = new \DateTime('2023-12-31 20:00:00');
$next = $e->getNextRunDate(new \DateTime('2022-08-20 03:44:02'), 2);
$this->assertEquals($expected, $next);

$expected = new \DateTime('2022-06-30 20:00:00');
$prev = $e->getPreviousRunDate(new \DateTime('2022-08-20 03:44:02'));
$this->assertEquals($expected, $prev);

$expected = new \DateTime('2021-12-31 20:00:00');
$prev = $e->getPreviousRunDate(new \DateTime('2022-08-20 03:44:02'), 1);
$this->assertEquals($expected, $prev);
}
}

0 comments on commit 6e0003e

Please # to comment.