From 4c38c16a4992ecd8b081ec8264157eeee1a6ecf5 Mon Sep 17 00:00:00 2001
From: sgiehl <stefan@piwik.org>
Date: Mon, 3 Oct 2016 22:17:30 +0200
Subject: [PATCH] move timezone offest calculation to date class

---
 core/Date.php                           | 13 +++++++++++++
 core/Updates/2.16.3-b3.php              |  5 +----
 plugins/ScheduledReports/Controller.php |  5 +----
 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
@@ -219,6 +219,19 @@ class Date
         return new Date($this->timestamp, $timezone);
     }
 
+    /**
+     * 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
      */
-- 
GitLab