ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTimeZone Class Reference

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

+ Collaboration diagram for ilTimeZone:

Public Member Functions

 __sleep ()
 
 __wakeup ()
 
 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...
 
static initDefaultTimeZone (ilIniFile $ini)
 Initialize default timezone from system settings. 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 $DIC.

66  {
67  global $DIC;
68 
69  $this->log = $DIC->logger()->cal();
70 
71  if ($a_timezone) {
72  $this->timezone = $a_timezone;
73  } else {
74  $this->timezone = self::_getDefaultTimeZone();
75  }
76 
77  if (!self::$server_timezone) {
78  self::$server_timezone = self::_getDefaultTimeZone();
79  }
80 
81  if (!self::$default_timezone) {
82  self::_getDefaultTimeZone();
83  }
84  }
global $DIC
Definition: saml.php:7

Member Function Documentation

◆ __sleep()

ilTimeZone::__sleep ( )

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

87  {
88  return array('timezone');
89  }

◆ __wakeup()

ilTimeZone::__wakeup ( )

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

References $DIC.

92  {
93  global $DIC;
94 
95  $this->log = $DIC->logger()->cal();
96  }
global $DIC
Definition: saml.php:7

◆ _getDefaultTimeZone()

static ilTimeZone::_getDefaultTimeZone ( )
static

Calculate and set default time zone.

public

Returns
time zone string

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

References $tz.

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

268  {
269  if (strlen(self::$default_timezone)) {
270  return self::$default_timezone;
271  }
272  // PHP >= 5.2.0
273  // php throws a warning date_default_timezone_get relies on os determination. There is no way to check if this could happen.
274  if (function_exists('date_default_timezone_get') and $tz = @date_default_timezone_get()) {
275  return self::$default_timezone = $tz;
276  }
277  // PHP ini option (PHP >= 5.1.0)
278  if ($tz = ini_get('date.timezone')) {
279  return self::$default_timezone = $tz;
280  }
281  // is $_ENV['PHP_TZ'] set ?
282  if ($tz = getenv('PHP_TZ')) {
283  return self::$default_timezone = $tz;
284  }
285  // is $_ENV['TZ'] set ?
286  if ($tz = getenv('TZ')) {
287  return self::$default_timezone = $tz;
288  }
289  if (strlen($tz = date('T'))) {
290  return self::$default_timezone = $tz;
291  }
292  return self::$default_timezone = self::UTC;
293  }
+ 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 118 of file class.ilTimeZone.php.

References $DIC.

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

119  {
120  global $DIC;
121 
122  if (!$a_tz) {
123  $a_tz = self::_getDefaultTimeZone();
124  }
125 
126  if (isset(self::$instances[$a_tz])) {
127  $instance = self::$instances[$a_tz];
128  } else {
129  $instance = self::$instances[$a_tz] = new ilTimeZone($a_tz);
130  }
131 
132  // Validate timezone if it is not validated before
133  if (!array_key_exists($instance->getIdentifier(), self::$valid_tz)) {
134  if (!$instance->validateTZ()) {
135  throw new ilTimeZoneException('Unsupported timezone given.');
136  }
137  self::$valid_tz[$instance->getIdentifier()] = true;
138  }
139 
140  // now validate timezone setting
141  return $instance;
142  }
This class offers methods for timezone handling.
global $DIC
Definition: saml.php:7
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 254 of file class.ilTimeZone.php.

Referenced by ilCalendarRecurrenceCalculator\calculateDateList().

255  {
256  self::$default_timezone = self::$server_timezone;
257  self::_switchTimeZone(self::$default_timezone);
258  }
+ Here is the caller graph for this function:

◆ _setDefaultTimeZone()

static ilTimeZone::_setDefaultTimeZone (   $a_tz)
static

set default timezone

public

Parameters

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

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

237  {
238  // Save the server timezone, since there is no way to read later.
239  if (!self::$server_timezone) {
240  self::$server_timezone = self::_getDefaultTimeZone();
241  }
242 
243  self::$default_timezone = $a_tz;
244  }
+ Here is the caller graph for this function:

◆ _switchTimeZone()

static ilTimeZone::_switchTimeZone (   $a_timezone)
staticprotected

Switch tz.

public

Exceptions
ilTimeZoneException

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

References $DIC.

201  {
202  global $DIC;
203 
204  $logger = $DIC->logger()->cal();
205 
206  if (self::$current_timezone == $a_timezone) {
207  return true;
208  }
209 
210  // PHP >= 5.2.0
211  if (function_exists('date_default_timezone_set')) {
212  if (!date_default_timezone_set($a_timezone)) {
213  $logger->info('Invalid timezone given. Timezone: ' . $a_timezone);
214  throw new ilTimeZoneException('Invalid timezone given');
215  }
216  #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
217  self::$current_timezone = $a_timezone;
218  return true;
219  }
220  if (!putenv('TZ=' . $a_timezone)) {
221  $logger->warning('Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
222  throw new ilTimeZoneException('Cannot set TZ environment variable.');
223  }
224  self::$current_timezone = $a_timezone;
225  return true;
226  }
global $DIC
Definition: saml.php:7
Class for TimeZone exceptions.

◆ getIdentifier()

ilTimeZone::getIdentifier ( )

get identifier

public

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

References $timezone.

105  {
106  return $this->timezone;
107  }

◆ initDefaultTimeZone()

static ilTimeZone::initDefaultTimeZone ( ilIniFile  $ini)
static

Initialize default timezone from system settings.

Returns
bool

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

References $tz, and ilIniFile\readVariable().

Referenced by ilInitialisation\initIliasIniFile().

300  {
301  $tz = $ini->readVariable('server', 'timezone');
302  if (!strlen($tz)) {
303  $tz = self::_getDefaultTimeZone();
304  }
305  if (!strlen($tz)) {
306  $tz = 'UTC';
307  }
308  date_default_timezone_set($tz);
309  return $tz;
310  }
readVariable($a_group, $a_var_name)
reads a single variable from a group public
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ restoreTZ()

ilTimeZone::restoreTZ ( )

Restore default timezone.

public

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

Referenced by validateTZ().

167  {
168  try {
169  self::_switchTimeZone(self::$default_timezone);
170  return true;
171  } catch (ilTimeZoneException $e) {
172  // Shouldn't happen since this has been checked during initialisation
173  $this->log->write(__METHOD__ . ': Unsupported timezone given: Timzone: ' . $this->timezone);
174  return false;
175  }
176  }
Class for TimeZone exceptions.
+ Here is the caller graph for this function:

◆ switchTZ()

ilTimeZone::switchTZ ( )

Switch timezone to given timezone.

public

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

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

150  {
151  try {
152  self::_switchTimeZone($this->timezone);
153  return true;
154  } catch (ilTimeZoneException $exc) {
155  // Shouldn't happen since this has been checked during initialisation
156  $this->log->write(__METHOD__ . ': Unsupported timezone given: Timzone: ' . $this->timezone);
157  return false;
158  }
159  }
Class for TimeZone exceptions.
+ Here is the caller graph for this function:

◆ validateTZ()

ilTimeZone::validateTZ ( )

validate timezone

public

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

References restoreTZ(), and switchTZ().

185  {
186  // this is done by switching to the current tz
187  if ($this->switchTZ() and $this->restoreTZ()) {
188  return true;
189  }
190  return false;
191  }
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: