ILIAS  release_8 Revision v8.24
ilTimeZone Class Reference

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

+ Collaboration diagram for ilTimeZone:

Public Member Functions

 __sleep ()
 
 __wakeup ()
 
 getIdentifier ()
 
 switchTZ ()
 Switch timezone to given timezone. More...
 
 restoreTZ ()
 Restore default timezone. More...
 
 validateTZ ()
 

Static Public Member Functions

static _getInstance (string $a_tz='')
 get instance by timezone More...
 
static _setDefaultTimeZone (string $a_tz)
 
static _restoreDefaultTimeZone ()
 
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 array $instances = array()
 
static array $valid_tz = array()
 

Static Protected Member Functions

static _switchTimeZone (string $a_timezone)
 

Protected Attributes

ilLogger $log
 
string $timezone = "UTC"
 

Static Protected Attributes

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

Private Member Functions

 __construct (string $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

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

Constructor & Destructor Documentation

◆ __construct()

ilTimeZone::__construct ( string  $a_timezone)
private

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

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

57 {
58 global $DIC;
59
60 $this->log = $DIC->logger()->cal();
61
62 if ($a_timezone) {
63 $this->timezone = $a_timezone;
64 } else {
65 $this->timezone = self::_getDefaultTimeZone();
66 }
67
68 if (!self::$server_timezone) {
69 self::$server_timezone = self::_getDefaultTimeZone();
70 }
71
72 if (!self::$default_timezone) {
74 }
75 }
static _getDefaultTimeZone()
Calculate and set default time zone.
global $DIC
Definition: feed.php:28

References $DIC, and _getDefaultTimeZone().

+ Here is the call graph for this function:

Member Function Documentation

◆ __sleep()

ilTimeZone::__sleep ( )

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

78 {
79 return array('timezone');
80 }

◆ __wakeup()

ilTimeZone::__wakeup ( )

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

83 {
84 global $DIC;
85 $this->log = $DIC->logger()->cal();
86 }

References $DIC.

◆ _getDefaultTimeZone()

static ilTimeZone::_getDefaultTimeZone ( )
static

Calculate and set default time zone.

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

201 : string
202 {
203 if (strlen(self::$default_timezone)) {
205 }
206 // PHP >= 5.2.0
207 // php throws a warning date_default_timezone_get relies on os determination. There is no way to check if this could happen.
208 if (function_exists('date_default_timezone_get') and $tz = date_default_timezone_get()) {
209 return self::$default_timezone = $tz;
210 }
211 // PHP ini option (PHP >= 5.1.0)
212 if ($tz = ini_get('date.timezone')) {
213 return self::$default_timezone = $tz;
214 }
215 // is $_ENV['PHP_TZ'] set ?
216 if ($tz = getenv('PHP_TZ')) {
217 return self::$default_timezone = $tz;
218 }
219 // is $_ENV['TZ'] set ?
220 if ($tz = getenv('TZ')) {
221 return self::$default_timezone = $tz;
222 }
223 if (strlen($tz = date('T'))) {
224 return self::$default_timezone = $tz;
225 }
226 return self::$default_timezone = self::UTC;
227 }
static string $default_timezone

References $default_timezone, and UTC.

Referenced by __construct(), _getInstance(), _setDefaultTimeZone(), initDefaultTimeZone(), ilObjCalendarSettingsGUI\initFormSettings(), and ilCalendarSettings\read().

+ Here is the caller graph for this function:

◆ _getInstance()

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

get instance by timezone

Exceptions
ilTimeZoneException

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

98 {
99 if (!$a_tz) {
101 }
102
103 $instance = self::$instances[$a_tz] ?? (self::$instances[$a_tz] = new ilTimeZone($a_tz));
104
105 // Validate timezone if it is not validated before
106 if (!array_key_exists($instance->getIdentifier(), self::$valid_tz)) {
107 if (!$instance->validateTZ()) {
108 throw new ilTimeZoneException('Unsupported timezone given.');
109 }
110 self::$valid_tz[$instance->getIdentifier()] = true;
111 }
112
113 // now validate timezone setting
114 return $instance;
115 }
Class for TimeZone exceptions.
This class offers methods for timezone handling.

References _getDefaultTimeZone().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _restoreDefaultTimeZone()

static ilTimeZone::_restoreDefaultTimeZone ( )
static

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

192 : void
193 {
194 self::$default_timezone = self::$server_timezone;
195 self::_switchTimeZone(self::$default_timezone);
196 }
static _switchTimeZone(string $a_timezone)
static string $server_timezone

References $server_timezone, and _switchTimeZone().

Referenced by ilCalendarRecurrenceCalculator\calculateDateList().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _setDefaultTimeZone()

static ilTimeZone::_setDefaultTimeZone ( string  $a_tz)
static

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

183 : void
184 {
185 // Save the server timezone, since there is no way to read later.
186 if (!self::$server_timezone) {
187 self::$server_timezone = self::_getDefaultTimeZone();
188 }
189 self::$default_timezone = $a_tz;
190 }

References _getDefaultTimeZone().

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _switchTimeZone()

static ilTimeZone::_switchTimeZone ( string  $a_timezone)
staticprotected

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

156 : bool
157 {
158 global $DIC;
159
160 $logger = $DIC->logger()->cal();
161 if (self::$current_timezone == $a_timezone) {
162 return true;
163 }
164
165 // PHP >= 5.2.0
166 if (function_exists('date_default_timezone_set')) {
167 if (!date_default_timezone_set($a_timezone)) {
168 $logger->info('Invalid timezone given. Timezone: ' . $a_timezone);
169 throw new ilTimeZoneException('Invalid timezone given');
170 }
171 #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
172 self::$current_timezone = $a_timezone;
173 return true;
174 }
175 if (!putenv('TZ=' . $a_timezone)) {
176 $logger->warning('Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
177 throw new ilTimeZoneException('Cannot set TZ environment variable.');
178 }
179 self::$current_timezone = $a_timezone;
180 return true;
181 }

References $DIC.

Referenced by _restoreDefaultTimeZone(), restoreTZ(), and switchTZ().

+ Here is the caller graph for this function:

◆ getIdentifier()

ilTimeZone::getIdentifier ( )

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

88 : string
89 {
90 return $this->timezone;
91 }

References $timezone.

Referenced by ilDateTime\get(), and ilICalParser\switchTZ().

+ Here is the caller graph for this function:

◆ initDefaultTimeZone()

static ilTimeZone::initDefaultTimeZone ( ilIniFile  $ini)
static

Initialize default timezone from system settings.

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

232 : string
233 {
234 $tz = $ini->readVariable('server', 'timezone');
235 if (!strlen($tz)) {
237 }
238 if (!strlen($tz)) {
239 $tz = 'UTC';
240 }
241 date_default_timezone_set($tz);
242 return $tz;
243 }
$ini
Definition: raiseError.php:4

References $ini, and _getDefaultTimeZone().

Referenced by ilInitialisation\initIliasIniFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ restoreTZ()

ilTimeZone::restoreTZ ( )

Restore default timezone.

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

135 : bool
136 {
137 try {
138 self::_switchTimeZone(self::$default_timezone);
139 return true;
140 } catch (ilTimeZoneException $e) {
141 // Shouldn't happen since this has been checked during initialisation
142 $this->log->warning(': Unsupported timezone given: Timzone: ' . $this->timezone);
143 return false;
144 }
145 }

References Vendor\Package\$e, and _switchTimeZone().

Referenced by validateTZ().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ switchTZ()

ilTimeZone::switchTZ ( )

Switch timezone to given timezone.

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

120 : bool
121 {
122 try {
123 self::_switchTimeZone($this->timezone);
124 return true;
125 } catch (ilTimeZoneException $exc) {
126 // Shouldn't happen since this has been checked during initialisation
127 $this->log->warning(': Unsupported timezone given: Timzone: ' . $this->timezone);
128 return false;
129 }
130 }

References _switchTimeZone().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validateTZ()

ilTimeZone::validateTZ ( )

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

147 : bool
148 {
149 // this is done by switching to the current tz
150 if ($this->switchTZ() and $this->restoreTZ()) {
151 return true;
152 }
153 return false;
154 }
restoreTZ()
Restore default timezone.
switchTZ()
Switch timezone to given timezone.

References restoreTZ(), and switchTZ().

+ Here is the call graph for this function:

Field Documentation

◆ $current_timezone

string ilTimeZone::$current_timezone = ''
staticprotected

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

◆ $default_timezone

string ilTimeZone::$default_timezone = ''
staticprotected

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

Referenced by _getDefaultTimeZone().

◆ $instances

array ilTimeZone::$instances = array()
static

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

◆ $log

ilLogger ilTimeZone::$log
protected

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

◆ $server_timezone

string ilTimeZone::$server_timezone = ''
staticprotected

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

Referenced by _restoreDefaultTimeZone().

◆ $timezone

string ilTimeZone::$timezone = "UTC"
protected

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

Referenced by getIdentifier().

◆ $valid_tz

array ilTimeZone::$valid_tz = array()
static

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

◆ UTC


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