ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilTimeZone.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program 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 |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
41include_once('Services/Calendar/classes/class.ilTimeZoneException.php');
42
44{
45 const UTC = 'UTC';
46
47 public static $instances = array();
48 public static $valid_tz = array();
49
50 protected static $default_timezone = '';
51 protected static $current_timezone = '';
52 protected static $server_timezone = '';
53
54 protected $log;
55 protected $timezone = "UTC";
56
65 private function __construct($a_timezone)
66 {
67 global $ilLog;
68
69 $this->log = $ilLog;
70
71 if($a_timezone)
72 {
73 $this->timezone = $a_timezone;
74 }
75 else
76 {
77 $this->timezone = self::_getDefaultTimeZone();
78 }
79
80 if(!self::$server_timezone)
81 {
82 self::$server_timezone = self::_getDefaultTimeZone();
83 }
84
85 if(!self::$default_timezone)
86 {
88 }
89 }
90
97 public function getIdentifier()
98 {
99 return $this->timezone;
100 }
101
111 public static function _getInstance($a_tz = '')
112 {
113 global $ilLog;
114
115 if(!$a_tz)
116 {
118 }
119
120 if(isset(self::$instances[$a_tz]))
121 {
122 $instance = self::$instances[$a_tz];
123 }
124 else
125 {
126 $instance = self::$instances[$a_tz] = new ilTimeZone($a_tz);
127 }
128
129 // Validate timezone if it is not validated before
130 if(!array_key_exists($instance->getIdentifier(),self::$valid_tz))
131 {
132 if(!$instance->validateTZ())
133 {
134 throw new ilTimeZoneException('Unsupported timezone given.');
135 }
136 self::$valid_tz[$instance->getIdentifier()] = true;
137 }
138
139 // now validate timezone setting
140 return $instance;
141 }
142
148 public function switchTZ()
149 {
150 try
151 {
152 self::_switchTimeZone($this->timezone);
153 return true;
154 }
155 catch(ilTimeZoneException $exc)
156 {
157 // Shouldn't happen since this has been checked during initialisation
158 $this->log->write(__METHOD__.': Unsupported timezone given: Timzone: '.$this->timezone);
159 return false;
160 }
161 }
162
168 public function restoreTZ()
169 {
170 try
171 {
172 self::_switchTimeZone(self::$default_timezone);
173 return true;
174 }
175 catch(ilTimeZoneException $e)
176 {
177 // Shouldn't happen since this has been checked during initialisation
178 $this->log->write(__METHOD__.': Unsupported timezone given: Timzone: '.$this->timezone);
179 return false;
180 }
181 }
182
189 public function validateTZ()
190 {
191 // this is done by switching to the current tz
192 if($this->switchTZ() and $this->restoreTZ())
193 {
194 return true;
195 }
196 return false;
197 }
198
206 protected static function _switchTimeZone($a_timezone)
207 {
208 global $ilLog;
209
210 if(self::$current_timezone == $a_timezone)
211 {
212 #$ilLog->write(__METHOD__.': Do not switch to active timezone: '.$a_timezone);
213 return true;
214 }
215
216 // PHP >= 5.2.0
217 if(function_exists('date_default_timezone_set'))
218 {
219 if(!date_default_timezone_set($a_timezone))
220 {
221 $ilLog->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_timezone);
222 throw new ilTimeZoneException('Invalid timezone given');
223 }
224 #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
225 self::$current_timezone = $a_timezone;
226 return true;
227 }
228 if(!putenv('TZ='.$a_timezone))
229 {
230 $ilLog->write(__METHOD__.': Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
231 throw new ilTimeZoneException('Cannot set TZ environment variable.');
232 }
233 self::$current_timezone = $a_timezone;
234 return true;
235 }
236
245 public static function _setDefaultTimeZone($a_tz)
246 {
247 // Save the server timezone, since there is no way to read later.
248 if(!self::$server_timezone)
249 {
250 self::$server_timezone = self::_getDefaultTimeZone();
251 }
252
253 self::$default_timezone = $a_tz;
254 }
255
264 public static function _restoreDefaultTimeZone()
265 {
266 self::$default_timezone = self::$server_timezone;
267 self::_switchTimeZone(self::$default_timezone);
268 }
269
277 public static function _getDefaultTimeZone()
278 {
279 if(strlen(self::$default_timezone))
280 {
282 }
283 // PHP >= 5.2.0
284 if(function_exists('date_default_timezone_get') and $tz = date_default_timezone_get())
285 {
286 return self::$default_timezone = $tz;
287 }
288 // PHP ini option (PHP >= 5.1.0)
289 if($tz = ini_get('date.timezone'))
290 {
291 return self::$default_timezone = $tz;
292 }
293 // is $_ENV['PHP_TZ'] set ?
294 if($tz = getenv('PHP_TZ'))
295 {
296 return self::$default_timezone = $tz;
297 }
298 // is $_ENV['TZ'] set ?
299 if($tz = getenv('TZ'))
300 {
301 return self::$default_timezone = $tz;
302 }
303 if(strlen($tz = date('T')))
304 {
305 return self::$default_timezone = $tz;
306 }
307 return self::$default_timezone = self::UTC;
308 }
309}
310?>
Class for TimeZone exceptions.
This class offers methods for timezone handling.
restoreTZ()
Restore default timezone.
getIdentifier()
get identifier
static _restoreDefaultTimeZone()
restore default timezone to server timezone
static _getDefaultTimeZone()
Calculate and set default time zone.
static _switchTimeZone($a_timezone)
Switch tz
static _setDefaultTimeZone($a_tz)
set default timezone
static _getInstance($a_tz='')
get instance by timezone
static $current_timezone
__construct($a_timezone)
Create new timezone object If no timezone is given, the default server timezone is chosen.
switchTZ()
Switch timezone to given timezone.
validateTZ()
validate timezone
static $default_timezone
static $server_timezone