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