diff options
author | sgiehl <stefan@piwik.org> | 2016-10-03 23:17:30 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2016-10-04 00:16:00 +0300 |
commit | 4c38c16a4992ecd8b081ec8264157eeee1a6ecf5 (patch) | |
tree | b751ca132c615854004ded0becfff807e0adbacc | |
parent | a16ea5af913cc20527cf3f2fbc1766c91c64d366 (diff) |
move timezone offest calculation to date class
-rw-r--r-- | core/Date.php | 13 | ||||
-rw-r--r-- | core/Updates/2.16.3-b3.php | 5 | ||||
-rw-r--r-- | plugins/ScheduledReports/Controller.php | 5 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/DateTest.php | 23 |
4 files changed, 38 insertions, 8 deletions
diff --git a/core/Date.php b/core/Date.php index ad492eb6ea..8922ed8ac2 100644 --- a/core/Date.php +++ b/core/Date.php @@ -220,6 +220,19 @@ class Date } /** + * Returns the offset to UTC time for the given timezone + * + * @param $timezone + * @return int offest in minutes + */ + public static function getUtcOffset($timezone) + { + $timestampUTC = self::today()->getTimestampUTC(); + $timestampZone = self::adjustForTimezone($timestampUTC, $timezone); + return ($timestampZone - $timestampUTC); + } + + /** * Helper function that returns the offset in the timezone string 'UTC+14' * Returns false if the timezone is not UTC+X or UTC-X * diff --git a/core/Updates/2.16.3-b3.php b/core/Updates/2.16.3-b3.php index a13f7379d0..e5f2075c71 100644 --- a/core/Updates/2.16.3-b3.php +++ b/core/Updates/2.16.3-b3.php @@ -34,10 +34,7 @@ class Updates_2_16_3_b3 extends PiwikUpdates protected function adjustTimezoneBySite($hour, $idSite) { - $timezone = Site::getTimezoneFor($idSite); - $timestampUTC = Date::today()->getTimestampUTC(); - $timestampZone = Date::adjustForTimezone($timestampUTC, $timezone); - $timeZoneDifference = -ceil(($timestampZone - $timestampUTC) / 3600); + $timeZoneDifference = -ceil(Date::getUtcOffset($timezone)/3600); return (24 + $hour + $timeZoneDifference) % 24; } } diff --git a/plugins/ScheduledReports/Controller.php b/plugins/ScheduledReports/Controller.php index 8bdca90ab7..789b30fbc4 100644 --- a/plugins/ScheduledReports/Controller.php +++ b/plugins/ScheduledReports/Controller.php @@ -29,10 +29,7 @@ class Controller extends \Piwik\Plugin\Controller $siteTimezone = $this->site->getTimezone(); - $timestampUTC = Date::today()->getTimestampUTC(); - $timestampZone = Date::adjustForTimezone($timestampUTC, $siteTimezone); - - $view->timeZoneDifference = ($timestampZone - $timestampUTC) / 3600; + $view->timeZoneDifference = Date::getUtcOffset($siteTimezone) / 3600; $view->countWebsites = count(APISitesManager::getInstance()->getSitesIdWithAtLeastViewAccess()); // get report types diff --git a/tests/PHPUnit/Unit/DateTest.php b/tests/PHPUnit/Unit/DateTest.php index ee37aba1eb..0e075ee985 100644 --- a/tests/PHPUnit/Unit/DateTest.php +++ b/tests/PHPUnit/Unit/DateTest.php @@ -58,6 +58,29 @@ class DateTest extends \PHPUnit_Framework_TestCase $this->fail('Expected exception not raised'); } + public function getTimezoneOffsets() + { + return array( + array('UTC-2', -7200), + array('UTC+1.5', 5400), + array('UTC', 0), + array('America/Belize', -21600), + array('EST', -18000), + array('Antarctica/Syowa', 10800), + ); + } + + /** + * @group Core + * @group DateTest + * @dataProvider getTimezoneOffsets + */ + public function testGetUtcOffset($timezone, $expectedOffset) + { + $offset = Date::getUtcOffset($timezone); + $this->assertEquals($expectedOffset, $offset); + } + /** * @group Core */ |