From 0ad1c8b58540fe5749d5766a4541196774365069 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 3 Nov 2012 21:52:55 +0000 Subject: [PATCH] Factor timezone methods to a separate helper class --- .gitignore | 1 + Classes/PHPExcel/Shared/Date.php | 116 +++------------ Classes/PHPExcel/Shared/PasswordHasher.php | 13 +- Classes/PHPExcel/Shared/TimeZone.php | 140 ++++++++++++++++++ .../Classes/PHPExcel/Shared/DateTest.php | 24 --- .../Classes/PHPExcel/Shared/TimeZoneTest.php | 39 +++++ 6 files changed, 203 insertions(+), 130 deletions(-) create mode 100644 Classes/PHPExcel/Shared/TimeZone.php create mode 100644 unitTests/Classes/PHPExcel/Shared/TimeZoneTest.php diff --git a/.gitignore b/.gitignore index 7a4478da..208750bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build/PHPExcel.phar unitTests/codeCoverage +analysis *.buildpath *.project \ No newline at end of file diff --git a/Classes/PHPExcel/Shared/Date.php b/Classes/PHPExcel/Shared/Date.php index 70a3094c..43cf56dd 100644 --- a/Classes/PHPExcel/Shared/Date.php +++ b/Classes/PHPExcel/Shared/Date.php @@ -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; } } diff --git a/Classes/PHPExcel/Shared/PasswordHasher.php b/Classes/PHPExcel/Shared/PasswordHasher.php index f4a773a1..34985752 100644 --- a/Classes/PHPExcel/Shared/PasswordHasher.php +++ b/Classes/PHPExcel/Shared/PasswordHasher.php @@ -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); diff --git a/Classes/PHPExcel/Shared/TimeZone.php b/Classes/PHPExcel/Shared/TimeZone.php new file mode 100644 index 00000000..02a01584 --- /dev/null +++ b/Classes/PHPExcel/Shared/TimeZone.php @@ -0,0 +1,140 @@ +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; + } + +} diff --git a/unitTests/Classes/PHPExcel/Shared/DateTest.php b/unitTests/Classes/PHPExcel/Shared/DateTest.php index 72c33589..80b77d3c 100644 --- a/unitTests/Classes/PHPExcel/Shared/DateTest.php +++ b/unitTests/Classes/PHPExcel/Shared/DateTest.php @@ -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 */ diff --git a/unitTests/Classes/PHPExcel/Shared/TimeZoneTest.php b/unitTests/Classes/PHPExcel/Shared/TimeZoneTest.php new file mode 100644 index 00000000..0e44ceb6 --- /dev/null +++ b/unitTests/Classes/PHPExcel/Shared/TimeZoneTest.php @@ -0,0 +1,39 @@ +assertTrue($result); + } + + } + + public function testSetTimezoneWithInvalidValue() + { + $unsupportedTimezone = 'Etc/GMT+10'; + $result = call_user_func(array('PHPExcel_Shared_TimeZone','setTimezone'),$unsupportedTimezone); + $this->assertFalse($result); + } + +}