ILIAS  release_8 Revision v8.19
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

 __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.

References $DIC.

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) {
73  self::_getDefaultTimeZone();
74  }
75  }
global $DIC
Definition: feed.php:28

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.

References $DIC.

83  {
84  global $DIC;
85  $this->log = $DIC->logger()->cal();
86  }
global $DIC
Definition: feed.php:28

◆ _getDefaultTimeZone()

static ilTimeZone::_getDefaultTimeZone ( )
static

Calculate and set default time zone.

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

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

201  : string
202  {
203  if (strlen(self::$default_timezone)) {
204  return 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  }
+ 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.

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

97  : ilTimeZone
98  {
99  if (!$a_tz) {
100  $a_tz = self::_getDefaultTimeZone();
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.
+ Here is the caller graph for this function:

◆ _restoreDefaultTimeZone()

static ilTimeZone::_restoreDefaultTimeZone ( )
static

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

Referenced by ilCalendarRecurrenceCalculator\calculateDateList().

192  : void
193  {
194  self::$default_timezone = self::$server_timezone;
195  self::_switchTimeZone(self::$default_timezone);
196  }
+ 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.

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

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  }
+ 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.

References $DIC.

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  }
global $DIC
Definition: feed.php:28
Class for TimeZone exceptions.

◆ getIdentifier()

ilTimeZone::getIdentifier ( )

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

References $timezone.

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

88  : string
89  {
90  return $this->timezone;
91  }
+ 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.

References ilIniFile\readVariable().

Referenced by ilInitialisation\initIliasIniFile().

232  : string
233  {
234  $tz = $ini->readVariable('server', 'timezone');
235  if (!strlen($tz)) {
236  $tz = self::_getDefaultTimeZone();
237  }
238  if (!strlen($tz)) {
239  $tz = 'UTC';
240  }
241  date_default_timezone_set($tz);
242  return $tz;
243  }
readVariable(string $a_group, string $a_var_name)
reads a single variable from a group
+ 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.

References Vendor\Package\$e.

Referenced by validateTZ().

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

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

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

◆ validateTZ()

ilTimeZone::validateTZ ( )

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

References restoreTZ(), and switchTZ().

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  }
switchTZ()
Switch timezone to given timezone.
restoreTZ()
Restore default timezone.
+ 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.

◆ $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.

◆ $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

const ilTimeZone::UTC = 'UTC'

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

Referenced by ilDate\__construct(), ilObjCourse\__readSettings(), ilCalendarCategory\add(), ilCalendarExport\addCategories(), ilCalendarRecurrenceCalculator\adjustTimeZones(), ilCalendarUtil\convertDateToUtcDBTimestamp(), ilObjEmployeeTalkSeriesGUI\copyTemplateValues(), ilExportFileInfo\create(), ilCalendarExport\createVEVENT(), ilEmployeeTalkAppointmentGUI\deleteTalks(), ilEmployeeTalkAppointmentGUI\editAppointment(), ilConsultationHourUtils\findCalendarAppointmentsForBooking(), ilObjCourse\findCoursesWithNotEnoughMembers(), ilObjGroup\findGroupsWithNotEnoughMembers(), ilCalendarUserSettings\getExportTimeZone(), ilAdvancedMDFieldDefinitionDateTime\getLuceneSearchString(), ilTrQuery\getSessionData(), ilECSTimePlace\getUTBegin(), ilECSTimePlace\getUTEnd(), ilObjEmployeeTalkSeriesGUI\loadRecurrenceSettings(), ilEmployeeTalkAppointmentGUI\loadRecurrenceSettings(), ilBookingEntry\lookupBookingsForObject(), ilObjectCustomUserFieldHistory\lookupEntriesByObjectId(), ilSCTasks\lookupLastUpdate(), ilObjGroupAccess\lookupPeriodInfo(), ilObjCourseAccess\lookupPeriodInfo(), ILIAS\Modules\EmployeeTalk\Talk\Repository\IliasDBEmployeeTalkRepository\parseFromStdClass(), ilContainerXmlParser\parseTiming(), ilSCGroup\read(), ilSCTask\read(), ilObjectCustomUserFieldHistory\read(), ilExportFileInfo\read(), ilObjGroup\read(), ilDateList\removeByDAY(), ilConsultationHourCron\run(), ilObjectCustomUserFieldHistory\save(), ilCalendarEntry\save(), ilECSTimePlace\setBegin(), ilECSTimePlace\setEnd(), ilCalendarRecurrenceExclusion\toICal(), ilSCTask\update(), ilCalendarCategory\update(), ilCalendarEntry\update(), and ilContainerXmlWriter\writeCourseItemInformation().


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