ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 32 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 50 of file class.ilTimeZone.php.

51 {
52 global $DIC;
53
54 $this->log = $DIC->logger()->cal();
55
56 if ($a_timezone) {
57 $this->timezone = $a_timezone;
58 } else {
59 $this->timezone = self::_getDefaultTimeZone();
60 }
61
62 if (!self::$server_timezone) {
63 self::$server_timezone = self::_getDefaultTimeZone();
64 }
65
66 if (!self::$default_timezone) {
68 }
69 }
static _getDefaultTimeZone()
Calculate and set default time zone.
global $DIC
Definition: shib_login.php:26

References $DIC, and _getDefaultTimeZone().

+ Here is the call graph for this function:

Member Function Documentation

◆ __sleep()

ilTimeZone::__sleep ( )

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

72 {
73 return array('timezone');
74 }

◆ __wakeup()

ilTimeZone::__wakeup ( )

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

77 {
78 global $DIC;
79 $this->log = $DIC->logger()->cal();
80 }

References $DIC.

◆ _getDefaultTimeZone()

static ilTimeZone::_getDefaultTimeZone ( )
static

Calculate and set default time zone.

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

195 : string
196 {
197 if (strlen(self::$default_timezone)) {
199 }
200 // PHP >= 5.2.0
201 // php throws a warning date_default_timezone_get relies on os determination. There is no way to check if this could happen.
202 if (function_exists('date_default_timezone_get') and $tz = date_default_timezone_get()) {
203 return self::$default_timezone = $tz;
204 }
205 // PHP ini option (PHP >= 5.1.0)
206 if ($tz = ini_get('date.timezone')) {
207 return self::$default_timezone = $tz;
208 }
209 // is $_ENV['PHP_TZ'] set ?
210 if ($tz = getenv('PHP_TZ')) {
211 return self::$default_timezone = $tz;
212 }
213 // is $_ENV['TZ'] set ?
214 if ($tz = getenv('TZ')) {
215 return self::$default_timezone = $tz;
216 }
217 if (strlen($tz = date('T'))) {
218 return self::$default_timezone = $tz;
219 }
220 return self::$default_timezone = self::UTC;
221 }
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 91 of file class.ilTimeZone.php.

92 {
93 if (!$a_tz) {
95 }
96
97 $instance = self::$instances[$a_tz] ?? (self::$instances[$a_tz] = new ilTimeZone($a_tz));
98
99 // Validate timezone if it is not validated before
100 if (!array_key_exists($instance->getIdentifier(), self::$valid_tz)) {
101 if (!$instance->validateTZ()) {
102 throw new ilTimeZoneException('Unsupported timezone given.');
103 }
104 self::$valid_tz[$instance->getIdentifier()] = true;
105 }
106
107 // now validate timezone setting
108 return $instance;
109 }
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 186 of file class.ilTimeZone.php.

186 : void
187 {
188 self::$default_timezone = self::$server_timezone;
189 self::_switchTimeZone(self::$default_timezone);
190 }
static _switchTimeZone(string $a_timezone)
static string $server_timezone

References $server_timezone, and _switchTimeZone().

Referenced by ilCalendarRecurrenceCalculator\calculateDateList(), and ilCertificateDateHelperTest\tearDown().

+ 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 177 of file class.ilTimeZone.php.

177 : void
178 {
179 // Save the server timezone, since there is no way to read later.
180 if (!self::$server_timezone) {
181 self::$server_timezone = self::_getDefaultTimeZone();
182 }
183 self::$default_timezone = $a_tz;
184 }

References _getDefaultTimeZone().

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones(), and ilCertificateDateHelperTest\setUp().

+ 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 150 of file class.ilTimeZone.php.

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

References $DIC.

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

+ Here is the caller graph for this function:

◆ getIdentifier()

ilTimeZone::getIdentifier ( )

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

82 : string
83 {
84 return $this->timezone;
85 }

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 226 of file class.ilTimeZone.php.

226 : string
227 {
228 $tz = $ini->readVariable('server', 'timezone');
229 if (!strlen($tz)) {
231 }
232 if (!strlen($tz)) {
233 $tz = 'UTC';
234 }
235 date_default_timezone_set($tz);
236 return $tz;
237 }
$ini
Definition: raiseError.php:20

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 129 of file class.ilTimeZone.php.

129 : bool
130 {
131 try {
132 self::_switchTimeZone(self::$default_timezone);
133 return true;
134 } catch (ilTimeZoneException $e) {
135 // Shouldn't happen since this has been checked during initialisation
136 $this->log->warning(': Unsupported timezone given: Timzone: ' . $this->timezone);
137 return false;
138 }
139 }

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 114 of file class.ilTimeZone.php.

114 : bool
115 {
116 try {
117 self::_switchTimeZone($this->timezone);
118 return true;
119 } catch (ilTimeZoneException $exc) {
120 // Shouldn't happen since this has been checked during initialisation
121 $this->log->warning(': Unsupported timezone given: Timzone: ' . $this->timezone);
122 return false;
123 }
124 }

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 141 of file class.ilTimeZone.php.

141 : bool
142 {
143 // this is done by switching to the current tz
144 if ($this->switchTZ() and $this->restoreTZ()) {
145 return true;
146 }
147 return false;
148 }
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 40 of file class.ilTimeZone.php.

◆ $default_timezone

string ilTimeZone::$default_timezone = ''
staticprotected

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

Referenced by _getDefaultTimeZone().

◆ $instances

array ilTimeZone::$instances = array()
static

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

◆ $log

ilLogger ilTimeZone::$log
protected

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

◆ $server_timezone

string ilTimeZone::$server_timezone = ''
staticprotected

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

Referenced by _restoreDefaultTimeZone().

◆ $timezone

string ilTimeZone::$timezone = "UTC"
protected

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

Referenced by getIdentifier().

◆ $valid_tz

array ilTimeZone::$valid_tz = array()
static

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

◆ UTC


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