diff options
author | benakamoorthi <benaka.moorthi@gmail.com> | 2011-12-02 06:51:45 +0400 |
---|---|---|
committer | benakamoorthi <benaka.moorthi@gmail.com> | 2011-12-02 06:51:45 +0400 |
commit | 6a91d6ace826425a5e287c68175ff16d7008def1 (patch) | |
tree | 197d000e5955ab988a737f9f5563420deee3d8ea /core/Date.php | |
parent | 273c3cb92820a53873690b426f34a03628333168 (diff) |
Fixes #1077, Assorted optimizations for the Multi Sites plugin:
* Modified Piwik_Archive::build so it can accept Piwik_Segment instances. This way there won't be hundreds of segments created when they get overwritten by IndexedBySite anyway.
* Removed previously committed optimization that selected all sites in Piwik_Archive and replaced w/ a similar optimization in the code that selects data for every website (the Multi Sites controller & Sites Manager controller).
* Modified the setMinMaxDateAcrossWebsites function of the Multi Sites controller so Piwik_Date instances wouldn't be created within its loop and modified the Piwik_Date class to make the former change possible.
git-svn-id: http://dev.piwik.org/svn/trunk@5505 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core/Date.php')
-rw-r--r-- | core/Date.php | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/core/Date.php b/core/Date.php index 86272aecb5..78f225924b 100644 --- a/core/Date.php +++ b/core/Date.php @@ -91,20 +91,8 @@ class Piwik_Date return $date; } - // manually adjust for UTC timezones - $utcOffset = self::extractUtcOffset($timezone); - if($utcOffset !== false) - { - return $date->addHour($utcOffset); - } - - date_default_timezone_set($timezone); - $datetime = $date->getDatetime(); - date_default_timezone_set('UTC'); - - $date = Piwik_Date::factory(strtotime($datetime)); - - return $date; + $timestamp = self::adjustForTimezone($timestamp, $timezone); + return Piwik_Date::factory($timestamp); } /* @@ -195,6 +183,29 @@ class Piwik_Date } /** + * Adjusts a UNIX timestamp in UTC to a specific timezone. + * + * @param int $timestamp The UNIX timestamp to adjust. + * @param string $timezone The timezone to adjust to. + * @return int The adjusted time as seconds from EPOCH. + */ + static public function adjustForTimezone($timestamp, $timezone) + { + // manually adjust for UTC timezones + $utcOffset = self::extractUtcOffset($timezone); + if($utcOffset !== false) + { + return self::addHourTo($timestamp, $utcOffset); + } + + date_default_timezone_set($timezone); + $datetime = date(self::DATE_TIME_FORMAT, $timestamp); + date_default_timezone_set('UTC'); + + return strtotime($datetime); + } + + /** * Returns the Unix timestamp of the date in UTC * * @return int @@ -526,6 +537,19 @@ class Piwik_Date */ public function addHour( $n ) { + $ts = self::addHourTo( $this->timestamp, $n ); + return new Piwik_Date( $ts, $this->timezone ); + } + + /** + * Adds N number of hours to a UNIX timestamp and returns the result. + * + * @param int $timestamp The timestamp to add to. + * @param int|float Number of hours to add. + * @return int The result as a UNIX timestamp. + */ + public static function addHourTo( $timestamp, $n ) + { $isNegative = ($n < 0); $minutes = 0; if($n != round($n)) @@ -551,8 +575,7 @@ class Piwik_Date $n *= -1; } } - $ts = $this->timestamp + round($minutes * 60) + $n * 3600; - return new Piwik_Date( (int)$ts, $this->timezone ); + return (int)($timestamp + round($minutes * 60) + $n * 3600); } /** |