TimeZone.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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 - 2015 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. {
  51. if (in_array($timezone, DateTimeZone::listIdentifiers())) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. /**
  57. * Set the Default Timezone used for date/time conversions
  58. *
  59. * @param string $timezone Time zone (e.g. 'Europe/London')
  60. * @return boolean Success or failure
  61. */
  62. public static function setTimeZone($timezone)
  63. {
  64. if (self::_validateTimezone($timezone)) {
  65. self::$timezone = $timezone;
  66. return true;
  67. }
  68. return false;
  69. }
  70. /**
  71. * Return the Default Timezone used for date/time conversions
  72. *
  73. * @return string Timezone (e.g. 'Europe/London')
  74. */
  75. public static function getTimeZone()
  76. {
  77. return self::$timezone;
  78. }
  79. /**
  80. * Return the Timezone transition for the specified timezone and timestamp
  81. *
  82. * @param DateTimeZone $objTimezone The timezone for finding the transitions
  83. * @param integer $timestamp PHP date/time value for finding the current transition
  84. * @return array The current transition details
  85. */
  86. private static function getTimezoneTransitions($objTimezone, $timestamp)
  87. {
  88. $allTransitions = $objTimezone->getTransitions();
  89. $transitions = array();
  90. foreach ($allTransitions as $key => $transition) {
  91. if ($transition['ts'] > $timestamp) {
  92. $transitions[] = ($key > 0) ? $allTransitions[$key - 1] : $transition;
  93. break;
  94. }
  95. if (empty($transitions)) {
  96. $transitions[] = end($allTransitions);
  97. }
  98. }
  99. return $transitions;
  100. }
  101. /**
  102. * Return the Timezone offset used for date/time conversions to/from UST
  103. * This requires both the timezone and the calculated date/time to allow for local DST
  104. *
  105. * @param string $timezone The timezone for finding the adjustment to UST
  106. * @param integer $timestamp PHP date/time value
  107. * @return integer Number of seconds for timezone adjustment
  108. * @throws PHPExcel_Exception
  109. */
  110. public static function getTimeZoneAdjustment($timezone, $timestamp)
  111. {
  112. if ($timezone !== null) {
  113. if (!self::_validateTimezone($timezone)) {
  114. throw new PHPExcel_Exception("Invalid timezone " . $timezone);
  115. }
  116. } else {
  117. $timezone = self::$timezone;
  118. }
  119. if ($timezone == 'UST') {
  120. return 0;
  121. }
  122. $objTimezone = new DateTimeZone($timezone);
  123. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  124. $transitions = $objTimezone->getTransitions($timestamp, $timestamp);
  125. } else {
  126. $transitions = self::getTimezoneTransitions($objTimezone, $timestamp);
  127. }
  128. return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
  129. }
  130. }