ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTimeZone.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
41 include_once('Services/Calendar/classes/class.ilTimeZoneException.php');
42 
44 {
45  const UTC = 'UTC';
46 
47  public static $instances = array();
48  public static $valid_tz = array();
49 
50  protected static $default_timezone = '';
51  protected static $current_timezone = '';
52  protected static $server_timezone = '';
53 
54  protected $log;
55  protected $timezone = "UTC";
56 
65  private function __construct($a_timezone)
66  {
67  global $DIC;
68 
69  $this->log = $DIC->logger()->cal();
70 
71  if ($a_timezone) {
72  $this->timezone = $a_timezone;
73  } else {
74  $this->timezone = self::_getDefaultTimeZone();
75  }
76 
77  if (!self::$server_timezone) {
78  self::$server_timezone = self::_getDefaultTimeZone();
79  }
80 
81  if (!self::$default_timezone) {
82  self::_getDefaultTimeZone();
83  }
84  }
85 
86  public function __sleep()
87  {
88  return array('timezone');
89  }
90 
91  public function __wakeup()
92  {
93  global $DIC;
94 
95  $this->log = $DIC->logger()->cal();
96  }
97 
104  public function getIdentifier()
105  {
106  return $this->timezone;
107  }
108 
118  public static function _getInstance($a_tz = '')
119  {
120  global $DIC;
121 
122  if (!$a_tz) {
123  $a_tz = self::_getDefaultTimeZone();
124  }
125 
126  if (isset(self::$instances[$a_tz])) {
127  $instance = self::$instances[$a_tz];
128  } else {
129  $instance = self::$instances[$a_tz] = new ilTimeZone($a_tz);
130  }
131 
132  // Validate timezone if it is not validated before
133  if (!array_key_exists($instance->getIdentifier(), self::$valid_tz)) {
134  if (!$instance->validateTZ()) {
135  throw new ilTimeZoneException('Unsupported timezone given.');
136  }
137  self::$valid_tz[$instance->getIdentifier()] = true;
138  }
139 
140  // now validate timezone setting
141  return $instance;
142  }
143 
149  public function switchTZ()
150  {
151  try {
152  self::_switchTimeZone($this->timezone);
153  return true;
154  } catch (ilTimeZoneException $exc) {
155  // Shouldn't happen since this has been checked during initialisation
156  $this->log->write(__METHOD__ . ': Unsupported timezone given: Timzone: ' . $this->timezone);
157  return false;
158  }
159  }
160 
166  public function restoreTZ()
167  {
168  try {
169  self::_switchTimeZone(self::$default_timezone);
170  return true;
171  } catch (ilTimeZoneException $e) {
172  // Shouldn't happen since this has been checked during initialisation
173  $this->log->write(__METHOD__ . ': Unsupported timezone given: Timzone: ' . $this->timezone);
174  return false;
175  }
176  }
177 
184  public function validateTZ()
185  {
186  // this is done by switching to the current tz
187  if ($this->switchTZ() and $this->restoreTZ()) {
188  return true;
189  }
190  return false;
191  }
192 
200  protected static function _switchTimeZone($a_timezone)
201  {
202  global $DIC;
203 
204  $logger = $DIC->logger()->cal();
205 
206  if (self::$current_timezone == $a_timezone) {
207  return true;
208  }
209 
210  // PHP >= 5.2.0
211  if (function_exists('date_default_timezone_set')) {
212  if (!date_default_timezone_set($a_timezone)) {
213  $logger->info('Invalid timezone given. Timezone: ' . $a_timezone);
214  throw new ilTimeZoneException('Invalid timezone given');
215  }
216  #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
217  self::$current_timezone = $a_timezone;
218  return true;
219  }
220  if (!putenv('TZ=' . $a_timezone)) {
221  $logger->warning('Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
222  throw new ilTimeZoneException('Cannot set TZ environment variable.');
223  }
224  self::$current_timezone = $a_timezone;
225  return true;
226  }
227 
236  public static function _setDefaultTimeZone($a_tz)
237  {
238  // Save the server timezone, since there is no way to read later.
239  if (!self::$server_timezone) {
240  self::$server_timezone = self::_getDefaultTimeZone();
241  }
242 
243  self::$default_timezone = $a_tz;
244  }
245 
254  public static function _restoreDefaultTimeZone()
255  {
256  self::$default_timezone = self::$server_timezone;
257  self::_switchTimeZone(self::$default_timezone);
258  }
259 
267  public static function _getDefaultTimeZone()
268  {
269  if (strlen(self::$default_timezone)) {
270  return self::$default_timezone;
271  }
272  // PHP >= 5.2.0
273  // php throws a warning date_default_timezone_get relies on os determination. There is no way to check if this could happen.
274  if (function_exists('date_default_timezone_get') and $tz = @date_default_timezone_get()) {
275  return self::$default_timezone = $tz;
276  }
277  // PHP ini option (PHP >= 5.1.0)
278  if ($tz = ini_get('date.timezone')) {
279  return self::$default_timezone = $tz;
280  }
281  // is $_ENV['PHP_TZ'] set ?
282  if ($tz = getenv('PHP_TZ')) {
283  return self::$default_timezone = $tz;
284  }
285  // is $_ENV['TZ'] set ?
286  if ($tz = getenv('TZ')) {
287  return self::$default_timezone = $tz;
288  }
289  if (strlen($tz = date('T'))) {
290  return self::$default_timezone = $tz;
291  }
292  return self::$default_timezone = self::UTC;
293  }
294 
299  public static function initDefaultTimeZone(ilIniFile $ini)
300  {
301  $tz = $ini->readVariable('server', 'timezone');
302  if (!strlen($tz)) {
303  $tz = self::_getDefaultTimeZone();
304  }
305  if (!strlen($tz)) {
306  $tz = 'UTC';
307  }
308  date_default_timezone_set($tz);
309  return $tz;
310  }
311 }
static _getDefaultTimeZone()
Calculate and set default time zone.
validateTZ()
validate timezone
This class offers methods for timezone handling.
readVariable($a_group, $a_var_name)
reads a single variable from a group public
global $DIC
Definition: saml.php:7
getIdentifier()
get identifier
switchTZ()
Switch timezone to given timezone.
__construct($a_timezone)
Create new timezone object If no timezone is given, the default server timezone is chosen...
static initDefaultTimeZone(ilIniFile $ini)
Initialize default timezone from system settings.
static _setDefaultTimeZone($a_tz)
set default timezone
restoreTZ()
Restore default timezone.
static $server_timezone
static _restoreDefaultTimeZone()
restore default timezone to server timezone
static $default_timezone
Class for TimeZone exceptions.
static $current_timezone
static _getInstance($a_tz='')
get instance by timezone
static _switchTimeZone($a_timezone)
Switch tz.
INIFile Parser.
$ini
Definition: raiseError.php:4