Skip to content
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
42 changes: 20 additions & 22 deletions src/Cron/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function setPart($position, $value)
public function setMaxIterationCount($maxIterationCount)
{
$this->maxIterationCount = $maxIterationCount;

return $this;
}

Expand Down Expand Up @@ -286,30 +286,27 @@ public function isDue($currentTime = 'now', $timeZone = null)
if (is_null($timeZone)) {
$timeZone = date_default_timezone_get();
}

if ('now' === $currentTime) {
$currentDate = date('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} elseif ($currentTime instanceof DateTime) {

if ($currentTime instanceof DateTime) {
$currentDate = clone $currentTime;
// Ensure time in 'current' timezone is used
$currentDate->setTimezone(new DateTimeZone($timeZone));
$currentDate = $currentDate->format('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} elseif ($currentTime instanceof DateTimeImmutable) {
$currentDate = DateTime::createFromFormat('U', $currentTime->format('U'));
$currentDate->setTimezone(new DateTimeZone($timeZone));
$currentDate = $currentDate->format('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} else {
$currentTime = new DateTime($currentTime);
$currentTime->setTime($currentTime->format('H'), $currentTime->format('i'), 0);
$currentDate = $currentTime->format('Y-m-d H:i');
$currentTime = $currentTime->getTimeStamp();
$currentDate = new DateTime($currentTime ?: 'now', new DateTimeZone($timeZone));
}

//Set seconds to zero
$currentDate->setTime($currentDate->format('H'), $currentDate->format('i'), 0);

//Set timezone
$currentDate->setTimezone(new DateTimeZone($timeZone));

//Format Dates
$currentTime = $currentDate->getTimeStamp();
$currentDate = $currentDate->format('Y-m-d H:i');

try {
return $this->getNextRunDate($currentDate, 0, true)->getTimestamp() == $currentTime;
return $this->getNextRunDate($currentDate, 0, true, $timeZone)->getTimestamp() == $currentTime;
} catch (Exception $e) {
return false;
}
Expand All @@ -333,17 +330,18 @@ protected function getRunDate($currentTime = null, $nth = 0, $invert = false, $a
if (is_null($timeZone)) {
$timeZone = date_default_timezone_get();
}

if ($currentTime instanceof DateTime) {
$currentDate = clone $currentTime;
} elseif ($currentTime instanceof DateTimeImmutable) {
$currentDate = DateTime::createFromFormat('U', $currentTime->format('U'));
$currentDate->setTimezone($currentTime->getTimezone());
} else {
$currentDate = new DateTime($currentTime ?: 'now');
$currentDate->setTimezone(new DateTimeZone($timeZone));
$currentDate = new DateTime($currentTime ?: 'now', new DateTimeZone($timeZone));
}

//Set timezone
$currentDate->setTimezone(new DateTimeZone($timeZone));

$currentDate->setTime($currentDate->format('H'), $currentDate->format('i'), 0);
$nextRun = clone $currentDate;
$nth = (int) $nth;
Expand Down
6 changes: 5 additions & 1 deletion tests/Cron/CronExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public function testIsDueHandlesDifferentTimezones()
$utc = new DateTimeZone('UTC');
$amsterdam = new DateTimeZone('Europe/Amsterdam');
$tokyo = new DateTimeZone('Asia/Tokyo');
$defaultTimeZone = date_default_timezone_get();

date_default_timezone_set('UTC');
$this->assertTrue($cron->isDue(new DateTime($date, $utc)));
Expand All @@ -239,6 +240,9 @@ public function testIsDueHandlesDifferentTimezones()
$this->assertFalse($cron->isDue(new DateTime($date, $utc)));
$this->assertFalse($cron->isDue(new DateTime($date, $amsterdam)));
$this->assertTrue($cron->isDue(new DateTime($date, $tokyo)));

//Set default timezone again
date_default_timezone_set($defaultTimeZone);
}

/**
Expand All @@ -261,7 +265,7 @@ public function testIsDueHandlesDifferentTimezonesAsArgument()
$this->assertFalse($cron->isDue(new DateTime($date, $amsterdam), 'Asia/Tokyo'));
$this->assertTrue($cron->isDue(new DateTime($date, $tokyo), 'Asia/Tokyo'));
}

/**
* @covers \Cron\CronExpression::getPreviousRunDate
*/
Expand Down