ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
ilTimeZone Class Reference

This class offers methods for timezone handling. More...

+ Collaboration diagram for ilTimeZone:

Public Member Functions

 getIdentifier ()
 get identifier More...
 
 switchTZ ()
 Switch timezone to given timezone. More...
 
 restoreTZ ()
 Restore default timezone. More...
 
 validateTZ ()
 validate timezone More...
 

Static Public Member Functions

static _getInstance ($a_tz='')
 get instance by timezone More...
 
static _setDefaultTimeZone ($a_tz)
 set default timezone More...
 
static _restoreDefaultTimeZone ()
 restore default timezone to server timezone More...
 
static _getDefaultTimeZone ()
 Calculate and set default time zone. More...
 

Data Fields

const UTC = 'UTC'
 

Static Public Attributes

static $instances = array()
 
static $valid_tz = array()
 

Static Protected Member Functions

static _switchTimeZone ($a_timezone)
 Switch tz. More...
 

Protected Attributes

 $log
 
 $timezone = "UTC"
 

Static Protected Attributes

static $default_timezone = ''
 
static $current_timezone = ''
 
static $server_timezone = ''
 

Private Member Functions

 __construct ($a_timezone)
 Create new timezone object If no timezone is given, the default server timezone is chosen. More...
 

Detailed Description

This class offers methods for timezone handling.

ilTimeZone::_getDefault tries to "guess" the server timezone in the following manner: 1) PHP >= 5.2.0 use date_default_timezone_get 2) Read ini option date.timezone if available 3) Read environment PHP_TZ 4) Read environment TZ 5) Use date('T') 6) Use UTC

Author
Stefan Meyer smeye.nosp@m.r.il.nosp@m.ias@g.nosp@m.mx.d.nosp@m.e
Version
$Id$

Definition at line 43 of file class.ilTimeZone.php.

Constructor & Destructor Documentation

◆ __construct()

ilTimeZone::__construct (   $a_timezone)
private

Create new timezone object If no timezone is given, the default server timezone is chosen.

private

Parameters
stringvalid timezone

Definition at line 65 of file class.ilTimeZone.php.

References $ilLog.

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  {
87  self::_getDefaultTimeZone();
88  }
89  }

Member Function Documentation

◆ _getDefaultTimeZone()

static ilTimeZone::_getDefaultTimeZone ( )
static

Calculate and set default time zone.

public

Returns
time zone string

Definition at line 277 of file class.ilTimeZone.php.

Referenced by ilSoapInstallationInfoXMLWriter\__buildClient(), ilObjCalendarSettingsGUI\initFormSettings(), and ilCalendarSettings\read().

278  {
279  if(strlen(self::$default_timezone))
280  {
281  return self::$default_timezone;
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  }
+ Here is the caller graph for this function:

◆ _getInstance()

static ilTimeZone::_getInstance (   $a_tz = '')
static

get instance by timezone

public

Parameters
stringvalid php timezone
Exceptions
ilTimeZoneException

Definition at line 111 of file class.ilTimeZone.php.

References $ilLog.

Referenced by ilDate\__construct(), ilDateTime\__construct(), ilCalendarRecurrenceCalculator\applyBYMONTHDAYRules(), ilDateTime\get(), ilICalParser\getTZ(), ilICalParser\parse(), ilDateTime\setDate(), ilDateTime\switchTimeZone(), and ilUserImportParser\verifyPref().

112  {
113  global $ilLog;
114 
115  if(!$a_tz)
116  {
117  $a_tz = self::_getDefaultTimeZone();
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  }
This class offers methods for timezone handling.
Class for TimeZone exceptions.
+ Here is the caller graph for this function:

◆ _restoreDefaultTimeZone()

static ilTimeZone::_restoreDefaultTimeZone ( )
static

restore default timezone to server timezone

public

Parameters

Definition at line 264 of file class.ilTimeZone.php.

Referenced by ilCalendarRecurrenceCalculator\calculateDateList().

265  {
266  self::$default_timezone = self::$server_timezone;
267  self::_switchTimeZone(self::$default_timezone);
268  }
+ Here is the caller graph for this function:

◆ _setDefaultTimeZone()

static ilTimeZone::_setDefaultTimeZone (   $a_tz)
static

set default timezone

public

Parameters

Definition at line 245 of file class.ilTimeZone.php.

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

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  }
+ Here is the caller graph for this function:

◆ _switchTimeZone()

static ilTimeZone::_switchTimeZone (   $a_timezone)
staticprotected

Switch tz.

public

Exceptions
ilTimeZoneException

Definition at line 206 of file class.ilTimeZone.php.

References $ilLog.

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  }
Class for TimeZone exceptions.

◆ getIdentifier()

ilTimeZone::getIdentifier ( )

get identifier

public

Definition at line 97 of file class.ilTimeZone.php.

References $timezone.

98  {
99  return $this->timezone;
100  }

◆ restoreTZ()

ilTimeZone::restoreTZ ( )

Restore default timezone.

public

Definition at line 168 of file class.ilTimeZone.php.

Referenced by validateTZ().

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  }
Class for TimeZone exceptions.
+ Here is the caller graph for this function:

◆ switchTZ()

ilTimeZone::switchTZ ( )

Switch timezone to given timezone.

public

Definition at line 148 of file class.ilTimeZone.php.

Referenced by ilICalParser\switchTZ(), and validateTZ().

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  }
Class for TimeZone exceptions.
+ Here is the caller graph for this function:

◆ validateTZ()

ilTimeZone::validateTZ ( )

validate timezone

public

Definition at line 189 of file class.ilTimeZone.php.

References restoreTZ(), and switchTZ().

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  }
switchTZ()
Switch timezone to given timezone.
restoreTZ()
Restore default timezone.
+ Here is the call graph for this function:

Field Documentation

◆ $current_timezone

ilTimeZone::$current_timezone = ''
staticprotected

Definition at line 51 of file class.ilTimeZone.php.

◆ $default_timezone

ilTimeZone::$default_timezone = ''
staticprotected

Definition at line 50 of file class.ilTimeZone.php.

◆ $instances

ilTimeZone::$instances = array()
static

Definition at line 47 of file class.ilTimeZone.php.

◆ $log

ilTimeZone::$log
protected

Definition at line 54 of file class.ilTimeZone.php.

◆ $server_timezone

ilTimeZone::$server_timezone = ''
staticprotected

Definition at line 52 of file class.ilTimeZone.php.

◆ $timezone

ilTimeZone::$timezone = "UTC"
protected

Definition at line 55 of file class.ilTimeZone.php.

Referenced by getIdentifier().

◆ $valid_tz

ilTimeZone::$valid_tz = array()
static

Definition at line 48 of file class.ilTimeZone.php.

◆ UTC


The documentation for this class was generated from the following file: