ILIAS  trunk Revision v11.0_alpha-1862-g4e205cb56d4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator 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 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.

References $DIC.

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) {
67  self::_getDefaultTimeZone();
68  }
69  }
global $DIC
Definition: shib_login.php:22

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.

References $DIC.

77  {
78  global $DIC;
79  $this->log = $DIC->logger()->cal();
80  }
global $DIC
Definition: shib_login.php:22

◆ _getDefaultTimeZone()

static ilTimeZone::_getDefaultTimeZone ( )
static

Calculate and set default time zone.

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

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

195  : string
196  {
197  if (strlen(self::$default_timezone)) {
198  return 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  }
+ 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.

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

91  : ilTimeZone
92  {
93  if (!$a_tz) {
94  $a_tz = self::_getDefaultTimeZone();
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.
+ Here is the caller graph for this function:

◆ _restoreDefaultTimeZone()

static ilTimeZone::_restoreDefaultTimeZone ( )
static

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

Referenced by ilCalendarRecurrenceCalculator\calculateDateList().

186  : void
187  {
188  self::$default_timezone = self::$server_timezone;
189  self::_switchTimeZone(self::$default_timezone);
190  }
+ 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.

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

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

References $DIC.

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

◆ getIdentifier()

ilTimeZone::getIdentifier ( )

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

References $timezone.

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

82  : string
83  {
84  return $this->timezone;
85  }
+ 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.

References ilIniFile\readVariable().

Referenced by ilInitialisation\initIliasIniFile().

226  : string
227  {
228  $tz = $ini->readVariable('server', 'timezone');
229  if (!strlen($tz)) {
230  $tz = self::_getDefaultTimeZone();
231  }
232  if (!strlen($tz)) {
233  $tz = 'UTC';
234  }
235  date_default_timezone_set($tz);
236  return $tz;
237  }
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 129 of file class.ilTimeZone.php.

References Vendor\Package\$e.

Referenced by validateTZ().

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

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

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

◆ validateTZ()

ilTimeZone::validateTZ ( )

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

References restoreTZ(), and switchTZ().

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

◆ $default_timezone

string ilTimeZone::$default_timezone = ''
staticprotected

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

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

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

const ilTimeZone::UTC = 'UTC'

Definition at line 34 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\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: