TimeZone.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /**
  8. * PHPExcel_Shared_TimeZone
  9. *
  10. * @category PHPExcel
  11. * @package PHPExcel_Shared
  12. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  13. */
  14. class PHPExcel_Shared_TimeZone
  15. {
  16. /*
  17. * Default Timezone used for date/time conversions
  18. *
  19. * @private
  20. * @var string
  21. */
  22. protected static $timezone = 'UTC';
  23. /**
  24. * Validate a Timezone name
  25. *
  26. * @param string $timezone Time zone (e.g. 'Europe/London')
  27. * @return boolean Success or failure
  28. */
  29. public static function _validateTimeZone($timezone)
  30. {
  31. if (in_array($timezone, DateTimeZone::listIdentifiers())) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. /**
  37. * Set the Default Timezone used for date/time conversions
  38. *
  39. * @param string $timezone Time zone (e.g. 'Europe/London')
  40. * @return boolean Success or failure
  41. */
  42. public static function setTimeZone($timezone)
  43. {
  44. if (self::_validateTimezone($timezone)) {
  45. self::$timezone = $timezone;
  46. return true;
  47. }
  48. return false;
  49. }
  50. /**
  51. * Return the Default Timezone used for date/time conversions
  52. *
  53. * @return string Timezone (e.g. 'Europe/London')
  54. */
  55. public static function getTimeZone()
  56. {
  57. return self::$timezone;
  58. }
  59. /**
  60. * Return the Timezone transition for the specified timezone and timestamp
  61. *
  62. * @param DateTimeZone $objTimezone The timezone for finding the transitions
  63. * @param integer $timestamp PHP date/time value for finding the current transition
  64. * @return array The current transition details
  65. */
  66. private static function getTimezoneTransitions($objTimezone, $timestamp)
  67. {
  68. $allTransitions = $objTimezone->getTransitions();
  69. $transitions = array();
  70. foreach ($allTransitions as $key => $transition) {
  71. if ($transition['ts'] > $timestamp) {
  72. $transitions[] = ($key > 0) ? $allTransitions[$key - 1] : $transition;
  73. break;
  74. }
  75. if (empty($transitions)) {
  76. $transitions[] = end($allTransitions);
  77. }
  78. }
  79. return $transitions;
  80. }
  81. /**
  82. * Return the Timezone offset used for date/time conversions to/from UST
  83. * This requires both the timezone and the calculated date/time to allow for local DST
  84. *
  85. * @param string $timezone The timezone for finding the adjustment to UST
  86. * @param integer $timestamp PHP date/time value
  87. * @return integer Number of seconds for timezone adjustment
  88. * @throws PHPExcel_Exception
  89. */
  90. public static function getTimeZoneAdjustment($timezone, $timestamp)
  91. {
  92. if ($timezone !== null) {
  93. if (!self::_validateTimezone($timezone)) {
  94. throw new PHPExcel_Exception("Invalid timezone " . $timezone);
  95. }
  96. } else {
  97. $timezone = self::$timezone;
  98. }
  99. if ($timezone == 'UST') {
  100. return 0;
  101. }
  102. $objTimezone = new DateTimeZone($timezone);
  103. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  104. $transitions = $objTimezone->getTransitions($timestamp, $timestamp);
  105. } else {
  106. $transitions = self::getTimezoneTransitions($objTimezone, $timestamp);
  107. }
  108. return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
  109. }
  110. }