PhpSpreadsheet/Classes/PHPExcel/Shared/TimeZone.php

145 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* PHPExcel
*
2015-05-02 22:50:37 +00:00
* Copyright (c) 2006 - 2015 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
2015-05-12 09:22:06 +00:00
* @package PHPExcel_Shared
2015-05-02 22:50:37 +00:00
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
2015-05-12 09:22:06 +00:00
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/**
* PHPExcel_Shared_TimeZone
*
* @category PHPExcel
2015-05-12 09:22:06 +00:00
* @package PHPExcel_Shared
2015-05-02 22:50:37 +00:00
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Shared_TimeZone
{
2015-05-12 09:22:06 +00:00
/*
* Default Timezone used for date/time conversions
*
* @private
* @var string
*/
protected static $timezone = 'UTC';
2015-05-12 09:22:06 +00:00
/**
* Validate a Timezone name
*
* @param string $timezone Time zone (e.g. 'Europe/London')
* @return boolean Success or failure
*/
2015-05-16 17:00:31 +00:00
public static function _validateTimeZone($timezone)
{
2015-05-12 09:22:06 +00:00
if (in_array($timezone, DateTimeZone::listIdentifiers())) {
return true;
}
return false;
}
2015-05-12 09:22:06 +00:00
/**
* Set the Default Timezone used for date/time conversions
*
* @param string $timezone Time zone (e.g. 'Europe/London')
* @return boolean Success or failure
*/
2015-05-16 17:00:31 +00:00
public static function setTimeZone($timezone)
{
2015-05-12 09:22:06 +00:00
if (self::_validateTimezone($timezone)) {
self::$timezone = $timezone;
2015-05-12 09:22:06 +00:00
return true;
}
return false;
}
2015-05-12 09:22:06 +00:00
/**
* Return the Default Timezone used for date/time conversions
*
* @return string Timezone (e.g. 'Europe/London')
*/
2015-05-16 17:00:31 +00:00
public static function getTimeZone()
{
return self::$timezone;
}
2015-05-12 09:22:06 +00:00
/**
* Return the Timezone transition for the specified timezone and timestamp
*
* @param DateTimeZone $objTimezone The timezone for finding the transitions
* @param integer $timestamp PHP date/time value for finding the current transition
* @return array The current transition details
*/
private static function getTimezoneTransitions($objTimezone, $timestamp)
2015-05-16 17:00:31 +00:00
{
2015-05-12 09:22:06 +00:00
$allTransitions = $objTimezone->getTransitions();
$transitions = array();
foreach ($allTransitions as $key => $transition) {
if ($transition['ts'] > $timestamp) {
$transitions[] = ($key > 0) ? $allTransitions[$key - 1] : $transition;
break;
}
if (empty($transitions)) {
$transitions[] = end($allTransitions);
}
}
2015-05-12 09:22:06 +00:00
return $transitions;
}
2015-05-12 09:22:06 +00:00
/**
* Return the Timezone offset used for date/time conversions to/from UST
* This requires both the timezone and the calculated date/time to allow for local DST
*
* @param string $timezone The timezone for finding the adjustment to UST
* @param integer $timestamp PHP date/time value
* @return integer Number of seconds for timezone adjustment
* @throws PHPExcel_Exception
*/
2015-05-16 17:00:31 +00:00
public static function getTimeZoneAdjustment($timezone, $timestamp)
{
2015-05-12 09:22:06 +00:00
if ($timezone !== null) {
if (!self::_validateTimezone($timezone)) {
throw new PHPExcel_Exception("Invalid timezone " . $timezone);
}
} else {
$timezone = self::$timezone;
2015-05-12 09:22:06 +00:00
}
2015-05-12 09:22:06 +00:00
if ($timezone == 'UST') {
return 0;
}
2015-05-12 09:22:06 +00:00
$objTimezone = new DateTimeZone($timezone);
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
2015-05-13 10:27:01 +00:00
$transitions = $objTimezone->getTransitions($timestamp, $timestamp);
2015-05-12 09:22:06 +00:00
} else {
$transitions = self::getTimezoneTransitions($objTimezone, $timestamp);
2015-05-12 09:22:06 +00:00
}
2015-05-12 09:22:06 +00:00
return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
}
}