Factor timezone methods to a separate helper class

This commit is contained in:
Mark Baker 2012-11-03 21:52:55 +00:00
parent 53a32ce7f7
commit 0ad1c8b585
6 changed files with 203 additions and 130 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
build/PHPExcel.phar build/PHPExcel.phar
unitTests/codeCoverage unitTests/codeCoverage
analysis
*.buildpath *.buildpath
*.project *.project

View File

@ -67,15 +67,7 @@ class PHPExcel_Shared_Date
* @private * @private
* @var int * @var int
*/ */
private static $_excelBaseDate = self::CALENDAR_WINDOWS_1900; protected static $_excelBaseDate = self::CALENDAR_WINDOWS_1900;
/*
* Default Timezone used for date/time conversions
*
* @private
* @var string
*/
private static $_timezone = 'UTC';
/** /**
* Set the Excel calendar (Windows 1900 or Mac 1904) * Set the Excel calendar (Windows 1900 or Mac 1904)
@ -103,86 +95,6 @@ class PHPExcel_Shared_Date
} // function getExcelCalendar() } // 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 * Convert a date from Excel to PHP
* *
@ -217,7 +129,9 @@ class PHPExcel_Shared_Date
$returnValue = (integer) gmmktime($hours, $mins, $secs); $returnValue = (integer) gmmktime($hours, $mins, $secs);
} }
$timezoneAdjustment = ($adjustToTimezone) ? self::_getTimezoneAdjustment($timezone, $returnValue) : 0; $timezoneAdjustment = ($adjustToTimezone) ?
PHPExcel_Shared_TimeZone::getTimezoneAdjustment($timezone, $returnValue) :
0;
// Return // Return
return $returnValue + $timezoneAdjustment; return $returnValue + $timezoneAdjustment;
@ -227,8 +141,8 @@ class PHPExcel_Shared_Date
/** /**
* Convert a date from Excel to a PHP Date/Time object * Convert a date from Excel to a PHP Date/Time object
* *
* @param long $dateValue Excel date/time value * @param integer $dateValue Excel date/time value
* @return long PHP date/time object * @return integer PHP date/time object
*/ */
public static function ExcelToPHPObject($dateValue = 0) { public static function ExcelToPHPObject($dateValue = 0) {
$dateTime = self::ExcelToPHP($dateValue); $dateTime = self::ExcelToPHP($dateValue);
@ -249,6 +163,9 @@ 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 * @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 * @return mixed Excel date/time value
* or boolean FALSE on failure * or boolean FALSE on failure
*/ */
@ -390,10 +307,11 @@ class PHPExcel_Shared_Date
// We might also have a format mask containing quoted strings... // 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 // we don't want to test for any of our characters within the quoted blocks
if (strpos($pFormatCode,'"') !== FALSE) { if (strpos($pFormatCode,'"') !== FALSE) {
$i = FALSE; $segMatcher = FALSE;
foreach(explode('"',$pFormatCode) as $subVal) { foreach(explode('"',$pFormatCode) as $subVal) {
// Only test in alternate array entries (the non-quoted blocks) // 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; return TRUE;
} }
} }

View File

@ -47,16 +47,15 @@ class PHPExcel_Shared_PasswordHasher
*/ */
public static function hashPassword($pPassword = '') { public static function hashPassword($pPassword = '') {
$password = 0x0000; $password = 0x0000;
$i = 1; // char position $charPos = 1; // char position
// split the plain text password in its component characters // split the plain text password in its component characters
$chars = preg_split('//', $pPassword, -1, PREG_SPLIT_NO_EMPTY); $chars = preg_split('//', $pPassword, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) { foreach ($chars as $char) {
$value = ord($char) << $i; // shifted ASCII value $value = ord($char) << $charPos++; // shifted ASCII value
$rotated_bits = $value >> 15; // rotated bits beyond bit 15 $rotated_bits = $value >> 15; // rotated bits beyond bit 15
$value &= 0x7fff; // first 15 bits $value &= 0x7fff; // first 15 bits
$password ^= ($value | $rotated_bits); $password ^= ($value | $rotated_bits);
++$i;
} }
$password ^= strlen($pPassword); $password ^= strlen($pPassword);

View File

@ -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;
}
}

View File

@ -34,30 +34,6 @@ class DateTest extends PHPUnit_Framework_TestCase
$this->assertFalse($result); $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 * @dataProvider providerDateTimeExcelToPHP1900
*/ */

View File

@ -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);
}
}