Changeset View
Changeset View
Standalone View
Standalone View
lib/kolab_sync_timezone_converter.php
Show First 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | public function getListOfTimezones($_offsets) | ||||
$_offsets = $this->_unpackTimezoneInfo($_offsets); | $_offsets = $this->_unpackTimezoneInfo($_offsets); | ||||
} | } | ||||
if (!$this->_validateOffsets($_offsets)) { | if (!$this->_validateOffsets($_offsets)) { | ||||
return array(); | return array(); | ||||
} | } | ||||
$this->_setDefaultStartDateIfEmpty($_offsets); | $this->_setDefaultStartDateIfEmpty($_offsets); | ||||
$cacheId = $this->_getCacheId('timezones', $_offsets); | |||||
$timezones = $this->_loadFromCache($cacheId); | |||||
if (!is_array($timezones)) { | |||||
$timezones = array(); | $timezones = array(); | ||||
foreach (DateTimeZone::listIdentifiers() as $timezoneIdentifier) { | foreach (DateTimeZone::listIdentifiers() as $timezoneIdentifier) { | ||||
$timezone = new DateTimeZone($timezoneIdentifier); | $timezone = new DateTimeZone($timezoneIdentifier); | ||||
if (false !== ($matchingTransition = $this->_checkTimezone($timezone, $_offsets))) { | if (false !== ($matchingTransition = $this->_checkTimezone($timezone, $_offsets))) { | ||||
$timezones[$timezoneIdentifier] = $matchingTransition['abbr']; | $timezones[$timezoneIdentifier] = $matchingTransition['abbr']; | ||||
} | } | ||||
} | } | ||||
$this->_saveInCache($timezones, $cacheId); | |||||
} | |||||
} | } | ||||
return $timezones; | return $timezones; | ||||
} | } | ||||
/** | /** | ||||
* Returns PHP timezone that matches to the {@param $_offsets} | * Returns PHP timezone that matches to the {@param $_offsets} | ||||
* | * | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | class kolab_sync_timezone_converter | ||||
* @param string|int $_startDate Start date | * @param string|int $_startDate Start date | ||||
* | * | ||||
* @return array Timezone offsets | * @return array Timezone offsets | ||||
*/ | */ | ||||
public function getOffsetsForTimezone($_timezone, $_startDate = null) | public function getOffsetsForTimezone($_timezone, $_startDate = null) | ||||
{ | { | ||||
$this->_setStartDate($_startDate); | $this->_setStartDate($_startDate); | ||||
$cacheId = $this->_getCacheId('offsets', array($_timezone)); | |||||
if (!($offsets = $this->_loadFromCache($cacheId))) { | |||||
$offsets = $this->_getOffsetsTemplate(); | $offsets = $this->_getOffsetsTemplate(); | ||||
try { | try { | ||||
$timezone = new DateTimeZone($_timezone); | $timezone = new DateTimeZone($_timezone); | ||||
} | } | ||||
catch (Exception $e) { | catch (Exception $e) { | ||||
return null; | return null; | ||||
} | } | ||||
list($standardTransition, $daylightTransition) = $this->_getTransitionsForTimezoneAndYear($timezone, $this->_startDate['year']); | list($standardTransition, $daylightTransition) = $this->_getTransitionsForTimezoneAndYear($timezone, $this->_startDate['year']); | ||||
if ($standardTransition) { | if ($standardTransition) { | ||||
$offsets['bias'] = $standardTransition['offset']/60*-1; | $offsets['bias'] = $standardTransition['offset']/60*-1; | ||||
if ($daylightTransition) { | if ($daylightTransition) { | ||||
$offsets = $this->_generateOffsetsForTransition($offsets, $standardTransition, 'standard', $timezone); | $offsets = $this->_generateOffsetsForTransition($offsets, $standardTransition, 'standard', $timezone); | ||||
$offsets = $this->_generateOffsetsForTransition($offsets, $daylightTransition, 'daylight', $timezone); | $offsets = $this->_generateOffsetsForTransition($offsets, $daylightTransition, 'daylight', $timezone); | ||||
//@todo how do we get the standardBias (is usually 0)? | //@todo how do we get the standardBias (is usually 0)? | ||||
//$offsets['standardBias'] = ... | //$offsets['standardBias'] = ... | ||||
$offsets['daylightBias'] = ($daylightTransition['offset'] - $standardTransition['offset'])/60*-1; | $offsets['daylightBias'] = ($daylightTransition['offset'] - $standardTransition['offset'])/60*-1; | ||||
$offsets['standardHour'] -= $offsets['daylightBias'] / 60; | $offsets['standardHour'] -= $offsets['daylightBias'] / 60; | ||||
$offsets['daylightHour'] += $offsets['daylightBias'] / 60; | $offsets['daylightHour'] += $offsets['daylightBias'] / 60; | ||||
} | } | ||||
} | } | ||||
$this->_saveInCache($offsets, $cacheId); | |||||
} | |||||
return $offsets; | return $offsets; | ||||
} | } | ||||
/** | /** | ||||
* Get offsets for timezone transition | * Get offsets for timezone transition | ||||
* | * | ||||
* @param array $_offsets Timezone offsets | * @param array $_offsets Timezone offsets | ||||
* @param array $_transition Timezone transition information | * @param array $_transition Timezone transition information | ||||
▲ Show 20 Lines • Show All 364 Lines • ▼ Show 20 Lines | protected function _getTransitionsForTimezoneAndYear(DateTimeZone $_timezone, $_year) | ||||
} | } | ||||
else if ($index == count($transitions) -1) { | else if ($index == count($transitions) -1) { | ||||
$standardTransition = $transition; | $standardTransition = $transition; | ||||
} | } | ||||
} | } | ||||
return array($standardTransition, $daylightTransition); | return array($standardTransition, $daylightTransition); | ||||
} | } | ||||
protected function _getCacheId($_prefix, $_offsets) | |||||
{ | |||||
return $_prefix . md5(serialize($_offsets)); | |||||
} | |||||
protected function _loadFromCache($key) | |||||
{ | |||||
if ($cache = $this->getCache()) { | |||||
return $cache->get($key); | |||||
} | |||||
return false; | |||||
} | |||||
protected function _saveInCache($value, $key) | |||||
{ | |||||
if ($cache = $this->getCache()) { | |||||
$cache->set($key, $value); | |||||
} | |||||
} | |||||
/** | |||||
* Getter for the cache engine object | |||||
*/ | |||||
protected function getCache() | |||||
{ | |||||
if ($this->cache === null) { | |||||
$rcube = rcube::get_instance(); | |||||
$cache = $rcube->get_cache_shared('activesync'); | |||||
$this->cache = $cache ? $cache : false; | |||||
} | |||||
return $this->cache; | |||||
} | |||||
} | } |