ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
91 public function __sleep()
92 {
93 return array('timezone');
94 }
95
96 public function __wakeup()
97 {
98 global $ilLog;
99
100 $this->log = $ilLog;
101 }
102
109 public function getIdentifier()
110 {
111 return $this->timezone;
112 }
113
123 public static function _getInstance($a_tz = '')
124 {
125 global $ilLog;
126
127 if(!$a_tz)
128 {
130 }
131
132 if(isset(self::$instances[$a_tz]))
133 {
134 $instance = self::$instances[$a_tz];
135 }
136 else
137 {
138 $instance = self::$instances[$a_tz] = new ilTimeZone($a_tz);
139 }
140
141 // Validate timezone if it is not validated before
142 if(!array_key_exists($instance->getIdentifier(),self::$valid_tz))
143 {
144 if(!$instance->validateTZ())
145 {
146 throw new ilTimeZoneException('Unsupported timezone given.');
147 }
148 self::$valid_tz[$instance->getIdentifier()] = true;
149 }
150
151 // now validate timezone setting
152 return $instance;
153 }
154
160 public function switchTZ()
161 {
162 try
163 {
164 self::_switchTimeZone($this->timezone);
165 return true;
166 }
167 catch(ilTimeZoneException $exc)
168 {
169 // Shouldn't happen since this has been checked during initialisation
170 $this->log->write(__METHOD__.': Unsupported timezone given: Timzone: '.$this->timezone);
171 return false;
172 }
173 }
174
180 public function restoreTZ()
181 {
182 try
183 {
184 self::_switchTimeZone(self::$default_timezone);
185 return true;
186 }
187 catch(ilTimeZoneException $e)
188 {
189 // Shouldn't happen since this has been checked during initialisation
190 $this->log->write(__METHOD__.': Unsupported timezone given: Timzone: '.$this->timezone);
191 return false;
192 }
193 }
194
201 public function validateTZ()
202 {
203 // this is done by switching to the current tz
204 if($this->switchTZ() and $this->restoreTZ())
205 {
206 return true;
207 }
208 return false;
209 }
210
218 protected static function _switchTimeZone($a_timezone)
219 {
220 global $ilLog;
221
222 if(self::$current_timezone == $a_timezone)
223 {
224 #$ilLog->write(__METHOD__.': Do not switch to active timezone: '.$a_timezone);
225 return true;
226 }
227
228 // PHP >= 5.2.0
229 if(function_exists('date_default_timezone_set'))
230 {
231 if(!date_default_timezone_set($a_timezone))
232 {
233 $ilLog->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_timezone);
234 throw new ilTimeZoneException('Invalid timezone given');
235 }
236 #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
237 self::$current_timezone = $a_timezone;
238 return true;
239 }
240 if(!putenv('TZ='.$a_timezone))
241 {
242 $ilLog->write(__METHOD__.': Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
243 throw new ilTimeZoneException('Cannot set TZ environment variable.');
244 }
245 self::$current_timezone = $a_timezone;
246 return true;
247 }
248
257 public static function _setDefaultTimeZone($a_tz)
258 {
259 // Save the server timezone, since there is no way to read later.
260 if(!self::$server_timezone)
261 {
262 self::$server_timezone = self::_getDefaultTimeZone();
263 }
264
265 self::$default_timezone = $a_tz;
266 }
267
276 public static function _restoreDefaultTimeZone()
277 {
278 self::$default_timezone = self::$server_timezone;
279 self::_switchTimeZone(self::$default_timezone);
280 }
281
289 public static function _getDefaultTimeZone()
290 {
291 if(strlen(self::$default_timezone))
292 {
294 }
295 // PHP >= 5.2.0
296 // php throws a warning date_default_timezone_get relies on os determination. There is no way to check if this could happen.
297 if(function_exists('date_default_timezone_get') and $tz = @date_default_timezone_get())
298 {
299 return self::$default_timezone = $tz;
300 }
301 // PHP ini option (PHP >= 5.1.0)
302 if($tz = ini_get('date.timezone'))
303 {
304 return self::$default_timezone = $tz;
305 }
306 // is $_ENV['PHP_TZ'] set ?
307 if($tz = getenv('PHP_TZ'))
308 {
309 return self::$default_timezone = $tz;
310 }
311 // is $_ENV['TZ'] set ?
312 if($tz = getenv('TZ'))
313 {
314 return self::$default_timezone = $tz;
315 }
316 if(strlen($tz = date('T')))
317 {
318 return self::$default_timezone = $tz;
319 }
320 return self::$default_timezone = self::UTC;
321 }
322
327 public static function initDefaultTimeZone(ilIniFile $ini)
328 {
329 $tz = $ini->readVariable('server','timezone');
330 if(!strlen($tz))
331 {
333 }
334 if(!strlen($tz))
335 {
336 $tz = 'UTC';
337 }
338 date_default_timezone_set($tz);
339 return $tz;
340 }
341}
342?>
INIFile Parser.
Class for TimeZone exceptions.
This class offers methods for timezone handling.
restoreTZ()
Restore default timezone.
static initDefaultTimeZone(ilIniFile $ini)
Initialize default timezone from system settings.
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
$ini