Changeset View
Changeset View
Standalone View
Standalone View
lib/kolab_sync_timezone_converter.php
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | class kolab_sync_timezone_converter | ||||
{ | { | ||||
if (self::$_instance === NULL) { | if (self::$_instance === NULL) { | ||||
self::$_instance = new kolab_sync_timezone_converter(); | self::$_instance = new kolab_sync_timezone_converter(); | ||||
} | } | ||||
return self::$_instance; | return self::$_instance; | ||||
} | } | ||||
/** | |||||
* Returns a timezone with an offset matching the time difference | |||||
* of $dt from $referenceDt. | |||||
* | |||||
* If set and matching the offset, kolab_format::$timezone is preferred. | |||||
* | |||||
* @param DateTime $dt The date time value for which we | |||||
* calculate the offset. | |||||
* @param DateTime $referenceDt The reference value, for instance in UTC. | |||||
* | |||||
* @return DateTimeZone|null | |||||
*/ | |||||
public function getOffsetTimezone($dt, $referenceDt) | |||||
{ | |||||
$interval = $referenceDt->diff($dt); | |||||
$tz = new DateTimeZone($interval->format('%R%H%I')); //e.g. +0200 | |||||
$utcOffset = $tz->getOffset($dt); | |||||
machniak: Hmmm.. One more thing. Using 'now' everywhere is probably wrong. You're interested in an offset… | |||||
//Prefer the configured timezone if it matches the offset. | |||||
if (kolab_format::$timezone) { | |||||
if (kolab_format::$timezone->getOffset($dt) == $utcOffset) { | |||||
return kolab_format::$timezone; | |||||
} | |||||
} | |||||
//Look for any timezone with a matching offset. | |||||
foreach (DateTimeZone::listIdentifiers() as $timezoneIdentifier) { | |||||
$timezone = new DateTimeZone($timezoneIdentifier); | |||||
if ($timezone->getOffset($dt) == $utcOffset) { | |||||
return $timezone; | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
/** | /** | ||||
* Returns a list of timezones that match to the {@param $_offsets} | * Returns a list of timezones that match to the {@param $_offsets} | ||||
* | * | ||||
* If {@see $_expectedTimezone} is set then the method will terminate as soon | * If {@see $_expectedTimezone} is set then the method will terminate as soon | ||||
* as the expected timezone has matched and the expected timezone will be the | * as the expected timezone has matched and the expected timezone will be the | ||||
* first entry to the returned array. | * first entry to the returned array. | ||||
* | * | ||||
* @param string|array $_offsets | * @param string|array $_offsets | ||||
▲ Show 20 Lines • Show All 513 Lines • Show Last 20 Lines |
Hmmm.. One more thing. Using 'now' everywhere is probably wrong. You're interested in an offset on the specified task start/due date not now. Right?