From cbde1b5a2974c83a6926a7d387479f0e04bb93c7 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 15 Apr 2014 19:43:43 +0200 Subject: [PATCH 1/2] Respect potential difference in TimeZones --- src/Cron/CronExpression.php | 4 +++- tests/Cron/CronExpressionTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) 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..684b565b 100644 --- a/tests/Cron/CronExpressionTest.php +++ b/tests/Cron/CronExpressionTest.php @@ -213,6 +213,37 @@ public function testIsDueHandlesDifferentDates() $this->assertTrue($cron->isDue('now')); $this->assertTrue($cron->isDue(new DateTime('now'))); $this->assertTrue($cron->isDue(date('Y-m-d H:i'))); + + $cron = CronExpression::factory('0 15 * * 2'); + $this->assertFalse($cron->isDue(new DateTime('2014-04-15 15:00', new \DateTimeZone('UTC')))); + $this->assertTrue($cron->isDue(new DateTime('2014-04-15 15:00', new \DateTimeZone('Europe/Amsterdam')))); + } + + /** + * @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))); } /** From 4b8ef5fd5cfe79f5579f30402407a59dc7320188 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 15 Apr 2014 19:49:38 +0200 Subject: [PATCH 2/2] Removed useless test --- tests/Cron/CronExpressionTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Cron/CronExpressionTest.php b/tests/Cron/CronExpressionTest.php index 684b565b..b6dc194c 100644 --- a/tests/Cron/CronExpressionTest.php +++ b/tests/Cron/CronExpressionTest.php @@ -213,10 +213,6 @@ public function testIsDueHandlesDifferentDates() $this->assertTrue($cron->isDue('now')); $this->assertTrue($cron->isDue(new DateTime('now'))); $this->assertTrue($cron->isDue(date('Y-m-d H:i'))); - - $cron = CronExpression::factory('0 15 * * 2'); - $this->assertFalse($cron->isDue(new DateTime('2014-04-15 15:00', new \DateTimeZone('UTC')))); - $this->assertTrue($cron->isDue(new DateTime('2014-04-15 15:00', new \DateTimeZone('Europe/Amsterdam')))); } /**