Factor timezone methods to a separate helper class
This commit is contained in:
parent
53a32ce7f7
commit
0ad1c8b585
|
@ -1,5 +1,6 @@
|
|||
build/PHPExcel.phar
|
||||
unitTests/codeCoverage
|
||||
analysis
|
||||
|
||||
*.buildpath
|
||||
*.project
|
|
@ -67,15 +67,7 @@ class PHPExcel_Shared_Date
|
|||
* @private
|
||||
* @var int
|
||||
*/
|
||||
private static $_excelBaseDate = self::CALENDAR_WINDOWS_1900;
|
||||
|
||||
/*
|
||||
* Default Timezone used for date/time conversions
|
||||
*
|
||||
* @private
|
||||
* @var string
|
||||
*/
|
||||
private static $_timezone = 'UTC';
|
||||
protected static $_excelBaseDate = self::CALENDAR_WINDOWS_1900;
|
||||
|
||||
/**
|
||||
* Set the Excel calendar (Windows 1900 or Mac 1904)
|
||||
|
@ -103,86 +95,6 @@ class PHPExcel_Shared_Date
|
|||
} // function getExcelCalendar()
|
||||
|
||||
|
||||
/**
|
||||
* Validate a Timezone value
|
||||
*
|
||||
* @param string $timezone Time zone (e.g. 'Europe/London')
|
||||
* @return boolean Success or failure
|
||||
*/
|
||||
private static function _validateTimezone($timezone) {
|
||||
if (in_array($timezone, DateTimeZone::listIdentifiers())) {
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Default Timezone used for date/time conversions
|
||||
*
|
||||
* @param string $timezone Time zone (e.g. 'Europe/London')
|
||||
* @return boolean Success or failure
|
||||
*/
|
||||
public static function setTimezone($timezone) {
|
||||
if (self::_validateTimezone($timezone)) {
|
||||
self::$_timezone = $timezone;
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
} // function setTimezone()
|
||||
|
||||
|
||||
/**
|
||||
* Return the Default Timezone used for date/time conversions
|
||||
*
|
||||
* @return string Timezone (e.g. 'Europe/London')
|
||||
*/
|
||||
public static function getTimezone() {
|
||||
return self::$_timezone;
|
||||
} // function getTimezone()
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private static function _getTimezoneAdjustment($timezone, $timestamp) {
|
||||
if ($timezone !== NULL) {
|
||||
if (!self::_validateTimezone($timezone)) {
|
||||
throw new PHPExcel_Exception("Invalid timezone " . $timezone);
|
||||
}
|
||||
} else {
|
||||
$timezone = self::$_timezone;
|
||||
}
|
||||
|
||||
if ($timezone == 'UST') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$objTimezone = new DateTimeZone($timezone);
|
||||
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
|
||||
$transitions = $objTimezone->getTransitions($timestamp,$timestamp);
|
||||
} else {
|
||||
$allTransitions = $objTimezone->getTransitions();
|
||||
$transitions = array();
|
||||
foreach($allTransitions as $key => $transition) {
|
||||
if ($transition['ts'] > $timestamp) {
|
||||
$transitions[] = ($key > 0) ? $allTransitions[$key - 1] : $transitions1[] = $transition;
|
||||
break;
|
||||
}
|
||||
if (empty($transitions)) {
|
||||
$transitions[] = end($allTransitions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a date from Excel to PHP
|
||||
*
|
||||
|
@ -190,7 +102,7 @@ class PHPExcel_Shared_Date
|
|||
* @param boolean $adjustToTimezone Flag indicating whether $dateValue should be treated as
|
||||
* a UST timestamp, or adjusted to UST
|
||||
* @param string $timezone The timezone for finding the adjustment from UST
|
||||
* @return long PHP serialized date/time
|
||||
* @return long PHP serialized date/time
|
||||
*/
|
||||
public static function ExcelToPHP($dateValue = 0, $adjustToTimezone = FALSE, $timezone = NULL) {
|
||||
if (self::$_excelBaseDate == self::CALENDAR_WINDOWS_1900) {
|
||||
|
@ -217,7 +129,9 @@ class PHPExcel_Shared_Date
|
|||
$returnValue = (integer) gmmktime($hours, $mins, $secs);
|
||||
}
|
||||
|
||||
$timezoneAdjustment = ($adjustToTimezone) ? self::_getTimezoneAdjustment($timezone, $returnValue) : 0;
|
||||
$timezoneAdjustment = ($adjustToTimezone) ?
|
||||
PHPExcel_Shared_TimeZone::getTimezoneAdjustment($timezone, $returnValue) :
|
||||
0;
|
||||
|
||||
// Return
|
||||
return $returnValue + $timezoneAdjustment;
|
||||
|
@ -227,8 +141,8 @@ class PHPExcel_Shared_Date
|
|||
/**
|
||||
* Convert a date from Excel to a PHP Date/Time object
|
||||
*
|
||||
* @param long $dateValue Excel date/time value
|
||||
* @return long PHP date/time object
|
||||
* @param integer $dateValue Excel date/time value
|
||||
* @return integer PHP date/time object
|
||||
*/
|
||||
public static function ExcelToPHPObject($dateValue = 0) {
|
||||
$dateTime = self::ExcelToPHP($dateValue);
|
||||
|
@ -246,11 +160,14 @@ class PHPExcel_Shared_Date
|
|||
|
||||
|
||||
/**
|
||||
* Convert a date from PHP to Excel
|
||||
* Convert a date from PHP to Excel
|
||||
*
|
||||
* @param mixed $dateValue PHP serialized date/time or date object
|
||||
* @return mixed Excel date/time value
|
||||
* or boolean FALSE on failure
|
||||
* @param mixed $dateValue PHP serialized date/time or date object
|
||||
* @param boolean $adjustToTimezone Flag indicating whether $dateValue should be treated as
|
||||
* a UST timestamp, or adjusted to UST
|
||||
* @param string $timezone The timezone for finding the adjustment from UST
|
||||
* @return mixed Excel date/time value
|
||||
* or boolean FALSE on failure
|
||||
*/
|
||||
public static function PHPToExcel($dateValue = 0, $adjustToTimezone = FALSE, $timezone = NULL) {
|
||||
$saveTimeZone = date_default_timezone_get();
|
||||
|
@ -390,10 +307,11 @@ class PHPExcel_Shared_Date
|
|||
// We might also have a format mask containing quoted strings...
|
||||
// we don't want to test for any of our characters within the quoted blocks
|
||||
if (strpos($pFormatCode,'"') !== FALSE) {
|
||||
$i = FALSE;
|
||||
$segMatcher = FALSE;
|
||||
foreach(explode('"',$pFormatCode) as $subVal) {
|
||||
// Only test in alternate array entries (the non-quoted blocks)
|
||||
if (($i = !$i) && (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$subVal))) {
|
||||
if (($segMatcher = !$segMatcher) &&
|
||||
(preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$subVal))) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,17 +46,16 @@ class PHPExcel_Shared_PasswordHasher
|
|||
* @return string Hashed password
|
||||
*/
|
||||
public static function hashPassword($pPassword = '') {
|
||||
$password = 0x0000;
|
||||
$i = 1; // char position
|
||||
$password = 0x0000;
|
||||
$charPos = 1; // char position
|
||||
|
||||
// split the plain text password in its component characters
|
||||
$chars = preg_split('//', $pPassword, -1, PREG_SPLIT_NO_EMPTY);
|
||||
foreach ($chars as $char) {
|
||||
$value = ord($char) << $i; // shifted ASCII value
|
||||
$rotated_bits = $value >> 15; // rotated bits beyond bit 15
|
||||
$value &= 0x7fff; // first 15 bits
|
||||
$password ^= ($value | $rotated_bits);
|
||||
++$i;
|
||||
$value = ord($char) << $charPos++; // shifted ASCII value
|
||||
$rotated_bits = $value >> 15; // rotated bits beyond bit 15
|
||||
$value &= 0x7fff; // first 15 bits
|
||||
$password ^= ($value | $rotated_bits);
|
||||
}
|
||||
|
||||
$password ^= strlen($pPassword);
|
||||
|
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2012 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
|
||||
* @package PHPExcel_Shared
|
||||
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* PHPExcel_Shared_TimeZone
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Shared
|
||||
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Shared_TimeZone
|
||||
{
|
||||
/*
|
||||
* Default Timezone used for date/time conversions
|
||||
*
|
||||
* @private
|
||||
* @var string
|
||||
*/
|
||||
protected static $_timezone = 'UTC';
|
||||
|
||||
/**
|
||||
* Validate a Timezone name
|
||||
*
|
||||
* @param string $timezone Time zone (e.g. 'Europe/London')
|
||||
* @return boolean Success or failure
|
||||
*/
|
||||
public static function _validateTimeZone($timezone) {
|
||||
if (in_array($timezone, DateTimeZone::listIdentifiers())) {
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Default Timezone used for date/time conversions
|
||||
*
|
||||
* @param string $timezone Time zone (e.g. 'Europe/London')
|
||||
* @return boolean Success or failure
|
||||
*/
|
||||
public static function setTimeZone($timezone) {
|
||||
if (self::_validateTimezone($timezone)) {
|
||||
self::$_timezone = $timezone;
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
} // function setTimezone()
|
||||
|
||||
|
||||
/**
|
||||
* Return the Default Timezone used for date/time conversions
|
||||
*
|
||||
* @return string Timezone (e.g. 'Europe/London')
|
||||
*/
|
||||
public static function getTimeZone() {
|
||||
return self::$_timezone;
|
||||
} // function getTimezone()
|
||||
|
||||
|
||||
/**
|
||||
* Return the Timezone transition for the specified timezone and timestamp
|
||||
*
|
||||
* @param string $timezone 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) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
return $transitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static function getTimeZoneAdjustment($timezone, $timestamp) {
|
||||
if ($timezone !== NULL) {
|
||||
if (!self::_validateTimezone($timezone)) {
|
||||
throw new PHPExcel_Exception("Invalid timezone " . $timezone);
|
||||
}
|
||||
} else {
|
||||
$timezone = self::$_timezone;
|
||||
}
|
||||
|
||||
if ($timezone == 'UST') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$objTimezone = new DateTimeZone($timezone);
|
||||
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
|
||||
$transitions = $objTimezone->getTransitions($timestamp,$timestamp);
|
||||
} else {
|
||||
$transitions = self::_getTimezoneTransitions($objTimezone, $timestamp);
|
||||
}
|
||||
|
||||
return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -34,30 +34,6 @@ class DateTest extends PHPUnit_Framework_TestCase
|
|||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testSetTimezone()
|
||||
{
|
||||
$timezoneValues = array(
|
||||
'Europe/Prague',
|
||||
'Asia/Tokyo',
|
||||
'America/Indiana/Indianapolis',
|
||||
'Pacific/Honolulu',
|
||||
'Atlantic/St_Helena',
|
||||
);
|
||||
|
||||
foreach($timezoneValues as $timezoneValue) {
|
||||
$result = call_user_func(array('PHPExcel_Shared_Date','setTimezone'),$timezoneValue);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function testSetTimezoneWithInvalidValue()
|
||||
{
|
||||
$unsupportedTimezone = 'Etc/GMT+10';
|
||||
$result = call_user_func(array('PHPExcel_Shared_Date','setTimezone'),$unsupportedTimezone);
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerDateTimeExcelToPHP1900
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
|
||||
class TimeZoneTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
|
||||
}
|
||||
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
public function testSetTimezone()
|
||||
{
|
||||
$timezoneValues = array(
|
||||
'Europe/Prague',
|
||||
'Asia/Tokyo',
|
||||
'America/Indiana/Indianapolis',
|
||||
'Pacific/Honolulu',
|
||||
'Atlantic/St_Helena',
|
||||
);
|
||||
|
||||
foreach($timezoneValues as $timezoneValue) {
|
||||
$result = call_user_func(array('PHPExcel_Shared_TimeZone','setTimezone'),$timezoneValue);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function testSetTimezoneWithInvalidValue()
|
||||
{
|
||||
$unsupportedTimezone = 'Etc/GMT+10';
|
||||
$result = call_user_func(array('PHPExcel_Shared_TimeZone','setTimezone'),$unsupportedTimezone);
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue