Skip to content

Commit

Permalink
Merge pull request #48 from holtkamp/patch-different-timezone-support
Browse files Browse the repository at this point in the history
Initial different timezone support
  • Loading branch information
mtdowling committed Apr 29, 2014
2 parents 11a4457 + 4b8ef5f commit 6fcc276
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Cron/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public function isDue($currentTime = 'now')
$currentDate = date('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} elseif ($currentTime instanceof \DateTime) {
$currentDate = $currentTime->format('Y-m-d H:i');
$currentDate = clone $currentTime;
$currentDate->setTimezone(new \DateTimeZone(date_default_timezone_get())); //Ensure time in 'current' timezone is used
$currentDate = $currentDate->format('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} else {
$currentTime = new \DateTime($currentTime);
Expand Down
27 changes: 27 additions & 0 deletions tests/Cron/CronExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ public function testIsDueHandlesDifferentDates()
$this->assertTrue($cron->isDue(date('Y-m-d H:i')));
}

/**
* @covers Cron\CronExpression::isDue
*/
public function testIsDueHandlesDifferentTimezones()
{
$cron = CronExpression::factory('0 15 * * 3'); //Wednesday at 15:00
$date = '2014-01-01 15:00'; //Wednesday
$utc = new \DateTimeZone('UTC');
$amsterdam = new \DateTimeZone('Europe/Amsterdam');
$tokyo = new \DateTimeZone('Asia/Tokyo');

date_default_timezone_set('UTC');
$this->assertTrue($cron->isDue(new DateTime($date, $utc)));
$this->assertFalse($cron->isDue(new DateTime($date, $amsterdam)));
$this->assertFalse($cron->isDue(new DateTime($date, $tokyo)));

date_default_timezone_set('Europe/Amsterdam');
$this->assertFalse($cron->isDue(new DateTime($date, $utc)));
$this->assertTrue($cron->isDue(new DateTime($date, $amsterdam)));
$this->assertFalse($cron->isDue(new DateTime($date, $tokyo)));

date_default_timezone_set('Asia/Tokyo');
$this->assertFalse($cron->isDue(new DateTime($date, $utc)));
$this->assertFalse($cron->isDue(new DateTime($date, $amsterdam)));
$this->assertTrue($cron->isDue(new DateTime($date, $tokyo)));
}

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

0 comments on commit 6fcc276

Please sign in to comment.