19 declare(strict_types=1);
34 public const UTC =
'UTC';
54 $this->log = $DIC->logger()->cal();
57 $this->timezone = $a_timezone;
59 $this->timezone = self::_getDefaultTimeZone();
62 if (!self::$server_timezone) {
63 self::$server_timezone = self::_getDefaultTimeZone();
66 if (!self::$default_timezone) {
67 self::_getDefaultTimeZone();
73 return array(
'timezone');
79 $this->log = $DIC->logger()->cal();
94 $a_tz = self::_getDefaultTimeZone();
97 $instance = self::$instances[$a_tz] ?? (self::$instances[$a_tz] =
new ilTimeZone($a_tz));
100 if (!array_key_exists($instance->getIdentifier(), self::$valid_tz)) {
101 if (!$instance->validateTZ()) {
104 self::$valid_tz[$instance->getIdentifier()] =
true;
117 self::_switchTimeZone($this->timezone);
121 $this->log->warning(
': Unsupported timezone given: Timzone: ' . $this->timezone);
132 self::_switchTimeZone(self::$default_timezone);
136 $this->log->warning(
': Unsupported timezone given: Timzone: ' . $this->timezone);
154 $logger = $DIC->logger()->cal();
155 if (self::$current_timezone == $a_timezone) {
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);
165 #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone); 166 self::$current_timezone = $a_timezone;
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');
173 self::$current_timezone = $a_timezone;
180 if (!self::$server_timezone) {
181 self::$server_timezone = self::_getDefaultTimeZone();
183 self::$default_timezone = $a_tz;
188 self::$default_timezone = self::$server_timezone;
189 self::_switchTimeZone(self::$default_timezone);
197 if (strlen(self::$default_timezone)) {
198 return self::$default_timezone;
202 if (function_exists(
'date_default_timezone_get') and $tz = date_default_timezone_get()) {
203 return self::$default_timezone = $tz;
206 if ($tz = ini_get(
'date.timezone')) {
207 return self::$default_timezone = $tz;
210 if ($tz = getenv(
'PHP_TZ')) {
211 return self::$default_timezone = $tz;
214 if ($tz = getenv(
'TZ')) {
215 return self::$default_timezone = $tz;
217 if (strlen($tz = date(
'T'))) {
218 return self::$default_timezone = $tz;
220 return self::$default_timezone = self::UTC;
230 $tz = self::_getDefaultTimeZone();
235 date_default_timezone_set($tz);
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.
restoreTZ()
Restore default timezone.
static _restoreDefaultTimeZone()
static _setDefaultTimeZone(string $a_tz)
static string $default_timezone
Class for TimeZone exceptions.
static string $current_timezone
readVariable(string $a_group, string $a_var_name)
reads a single variable from a group
static _switchTimeZone(string $a_timezone)
static string $server_timezone
__construct(string $a_timezone)
Create new timezone object If no timezone is given, the default server timezone is chosen...