ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
41 include_once('Services/Calendar/classes/class.ilTimeZoneException.php');
42 
44 {
45  const UTC = 'UTC';
46 
47  public static $instances = array();
48 
49  protected static $default_timezone = '';
50  protected static $current_timezone = '';
51  protected static $server_timezone = '';
52 
53  protected $log;
54  protected $timezone = "UTC";
55 
64  private function __construct($a_timezone)
65  {
66  global $ilLog;
67 
68  $this->log = $ilLog;
69 
70  if($a_timezone)
71  {
72  $this->timezone = $a_timezone;
73  }
74  else
75  {
76  $this->timezone = self::_getDefaultTimeZone();
77  }
78 
79  if(!self::$server_timezone)
80  {
81  self::$server_timezone = self::_getDefaultTimeZone();
82  }
83 
84  if(!self::$default_timezone)
85  {
87  }
88  }
89 
96  public function getIdentifier()
97  {
98  return $this->timezone;
99  }
100 
110  public static function _getInstance($a_tz = '')
111  {
112  global $ilLog;
113 
114  if(!$a_tz)
115  {
116  $a_tz = self::_getDefaultTimeZone();
117  }
118 
119  if(isset(self::$instances[$a_tz]))
120  {
121  $instance = self::$instances[$a_tz];
122  }
123  else
124  {
125  $instance = self::$instances[$a_tz] = new ilTimeZone($a_tz);
126  }
127 
128  // now validate timezone setting
129  if(!$instance->validateTZ())
130  {
131  throw new ilTimeZoneException('Unsupported timezone given.');
132  }
133  return $instance;
134  }
135 
141  public function switchTZ()
142  {
143  try
144  {
145  self::_switchTimeZone($this->timezone);
146  return true;
147  }
148  catch(ilTimeZoneException $exc)
149  {
150  // Shouldn't happen since this has been checked during initialisation
151  $this->log->write(__METHOD__.': Unsupported timezone given: Timzone: '.$this->timezone);
152  return false;
153  }
154  }
155 
161  public function restoreTZ()
162  {
163  try
164  {
165  self::_switchTimeZone(self::$default_timezone);
166  return true;
167  }
168  catch(ilTimeZoneException $e)
169  {
170  // Shouldn't happen since this has been checked during initialisation
171  $this->log->write(__METHOD__.': Unsupported timezone given: Timzone: '.$this->timezone);
172  return false;
173  }
174  }
175 
182  public function validateTZ()
183  {
184  // this is done by switching to the current tz
185  if($this->switchTZ() and $this->restoreTZ())
186  {
187  return true;
188  }
189  return false;
190  }
191 
199  protected static function _switchTimeZone($a_timezone)
200  {
201  global $ilLog;
202 
203  if(self::$current_timezone == $a_timezone)
204  {
205  #$ilLog->write(__METHOD__.': Do not switch to active timezone: '.$a_timezone);
206  return true;
207  }
208 
209  // PHP >= 5.2.0
210  if(function_exists('date_default_timezone_set'))
211  {
212  if(!date_default_timezone_set($a_timezone))
213  {
214  $ilLog->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_timezone);
215  throw new ilTimeZoneException('Invalid timezone given');
216  }
217  #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
218  self::$current_timezone = $a_timezone;
219  return true;
220  }
221  if(!putenv('TZ='.$a_timezone))
222  {
223  $ilLog->write(__METHOD__.': Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
224  throw new ilTimeZoneException('Cannot set TZ environment variable.');
225  }
226  self::$current_timezone = $a_timezone;
227  return true;
228  }
229 
238  public static function _setDefaultTimeZone($a_tz)
239  {
240  // Save the server timezone, since there is no way to read later.
241  if(!self::$server_timezone)
242  {
243  self::$server_timezone = self::_getDefaultTimeZone();
244  }
245 
246  self::$default_timezone = $a_tz;
247  }
248 
257  public static function _restoreDefaultTimeZone()
258  {
259  self::$default_timezone = self::$server_timezone;
260  self::_switchTimeZone(self::$default_timezone);
261  }
262 
270  public static function _getDefaultTimeZone()
271  {
272  if(strlen(self::$default_timezone))
273  {
275  }
276  // PHP >= 5.2.0
277  if(function_exists('date_default_timezone_get') and $tz = @date_default_timezone_get())
278  {
279  return self::$default_timezone = $tz;
280  }
281  // PHP ini option (PHP >= 5.1.0)
282  if($tz = ini_get('date.timezone'))
283  {
284  return self::$default_timezone = $tz;
285  }
286  // is $_ENV['PHP_TZ'] set ?
287  if($tz = getenv('PHP_TZ'))
288  {
289  return self::$default_timezone = $tz;
290  }
291  // is $_ENV['TZ'] set ?
292  if($tz = getenv('TZ'))
293  {
294  return self::$default_timezone = $tz;
295  }
296  if(strlen($tz = date('T')))
297  {
298  return self::$default_timezone = $tz;
299  }
300  return self::$default_timezone = self::UTC;
301  }
302 }
303 ?>