diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php index 3070e2bd..c44b4272 100644 --- a/src/Cron/CronExpression.php +++ b/src/Cron/CronExpression.php @@ -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); diff --git a/tests/Cron/CronExpressionTest.php b/tests/Cron/CronExpressionTest.php index 98292c13..b6dc194c 100644 --- a/tests/Cron/CronExpressionTest.php +++ b/tests/Cron/CronExpressionTest.php @@ -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 */