ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilTimeZone.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
33 {
34  public const UTC = 'UTC';
35 
36  public static array $instances = array();
37  public static array $valid_tz = array();
38 
39  protected static string $default_timezone = '';
40  protected static string $current_timezone = '';
41  protected static string $server_timezone = '';
42 
43  protected ilLogger $log;
44  protected string $timezone = "UTC";
45 
50  private function __construct(string $a_timezone)
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  }
70 
71  public function __sleep()
72  {
73  return array('timezone');
74  }
75 
76  public function __wakeup()
77  {
78  global $DIC;
79  $this->log = $DIC->logger()->cal();
80  }
81 
82  public function getIdentifier(): string
83  {
84  return $this->timezone;
85  }
86 
91  public static function _getInstance(string $a_tz = ''): 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  }
110 
114  public function switchTZ(): 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  }
125 
129  public function restoreTZ(): 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  }
140 
141  public function validateTZ(): 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  }
149 
150  protected static function _switchTimeZone(string $a_timezone): 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  }
176 
177  public static function _setDefaultTimeZone(string $a_tz): 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  }
185 
186  public static function _restoreDefaultTimeZone(): void
187  {
188  self::$default_timezone = self::$server_timezone;
189  self::_switchTimeZone(self::$default_timezone);
190  }
191 
195  public static function _getDefaultTimeZone(): 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  }
222 
226  public static function initDefaultTimeZone(ilIniFile $ini): 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  }
238 }
static _getDefaultTimeZone()
Calculate and set default time zone.
static _getInstance(string $a_tz='')
get instance by timezone
switchTZ()
Switch timezone to given timezone.
static initDefaultTimeZone(ilIniFile $ini)
Initialize default timezone from system settings.
static array $instances
restoreTZ()
Restore default timezone.
static _restoreDefaultTimeZone()
static _setDefaultTimeZone(string $a_tz)
static string $default_timezone
Class for TimeZone exceptions.
global $DIC
Definition: shib_login.php:22
static string $current_timezone
static array $valid_tz
readVariable(string $a_group, string $a_var_name)
reads a single variable from a group
static _switchTimeZone(string $a_timezone)
$ini
Definition: raiseError.php:20
static string $server_timezone
__construct(string $a_timezone)
Create new timezone object If no timezone is given, the default server timezone is chosen...