b0d52ced6f79b4434c1c85eb71fb486c3d363bc1.svn-base 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Shared
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Shared_TimeZone
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_TimeZone
  35. {
  36. /*
  37. * Default Timezone used for date/time conversions
  38. *
  39. * @private
  40. * @var string
  41. */
  42. protected static $_timezone = 'UTC';
  43. /**
  44. * Validate a Timezone name
  45. *
  46. * @param string $timezone Time zone (e.g. 'Europe/London')
  47. * @return boolean Success or failure
  48. */
  49. public static function _validateTimeZone($timezone) {
  50. if (in_array($timezone, DateTimeZone::listIdentifiers())) {
  51. return TRUE;
  52. }
  53. return FALSE;
  54. }
  55. /**
  56. * Set the Default Timezone used for date/time conversions
  57. *
  58. * @param string $timezone Time zone (e.g. 'Europe/London')
  59. * @return boolean Success or failure
  60. */
  61. public static function setTimeZone($timezone) {
  62. if (self::_validateTimezone($timezone)) {
  63. self::$_timezone = $timezone;
  64. return TRUE;
  65. }
  66. return FALSE;
  67. } // function setTimezone()
  68. /**
  69. * Return the Default Timezone used for date/time conversions
  70. *
  71. * @return string Timezone (e.g. 'Europe/London')
  72. */
  73. public static function getTimeZone() {
  74. return self::$_timezone;
  75. } // function getTimezone()
  76. /**
  77. * Return the Timezone transition for the specified timezone and timestamp
  78. *
  79. * @param DateTimeZone $objTimezone The timezone for finding the transitions
  80. * @param integer $timestamp PHP date/time value for finding the current transition
  81. * @return array The current transition details
  82. */
  83. private static function _getTimezoneTransitions($objTimezone, $timestamp) {
  84. $allTransitions = $objTimezone->getTransitions();
  85. $transitions = array();
  86. foreach($allTransitions as $key => $transition) {
  87. if ($transition['ts'] > $timestamp) {
  88. $transitions[] = ($key > 0) ? $allTransitions[$key - 1] : $transition;
  89. break;
  90. }
  91. if (empty($transitions)) {
  92. $transitions[] = end($allTransitions);
  93. }
  94. }
  95. return $transitions;
  96. }
  97. /**
  98. * Return the Timezone offset used for date/time conversions to/from UST
  99. * This requires both the timezone and the calculated date/time to allow for local DST
  100. *
  101. * @param string $timezone The timezone for finding the adjustment to UST
  102. * @param integer $timestamp PHP date/time value
  103. * @return integer Number of seconds for timezone adjustment
  104. * @throws PHPExcel_Exception
  105. */
  106. public static function getTimeZoneAdjustment($timezone, $timestamp) {
  107. if ($timezone !== NULL) {
  108. if (!self::_validateTimezone($timezone)) {
  109. throw new PHPExcel_Exception("Invalid timezone " . $timezone);
  110. }
  111. } else {
  112. $timezone = self::$_timezone;
  113. }
  114. if ($timezone == 'UST') {
  115. return 0;
  116. }
  117. $objTimezone = new DateTimeZone($timezone);
  118. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  119. $transitions = $objTimezone->getTransitions($timestamp,$timestamp);
  120. } else {
  121. $transitions = self::_getTimezoneTransitions($objTimezone, $timestamp);
  122. }
  123. return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
  124. }
  125. }