ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $ilLog;
68 
69  $this->log = $ilLog;
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 $ilLog;
94 
95  $this->log = $ilLog;
96  }
97 
104  public function getIdentifier()
105  {
106  return $this->timezone;
107  }
108 
118  public static function _getInstance($a_tz = '')
119  {
120  global $ilLog;
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 $ilLog;
203 
204  if (self::$current_timezone == $a_timezone) {
205  #$ilLog->write(__METHOD__.': Do not switch to active timezone: '.$a_timezone);
206  return true;
207  }
208 
209  // PHP >= 5.2.0
210  if (function_exists('date_default_timezone_set')) {
211  if (!date_default_timezone_set($a_timezone)) {
212  $ilLog->write(__METHOD__ . ': Invalid timezone given. Timezone: ' . $a_timezone);
213  throw new ilTimeZoneException('Invalid timezone given');
214  }
215  #$ilLog->write(__METHOD__.': Switched timezone to: '.$a_timezone);
216  self::$current_timezone = $a_timezone;
217  return true;
218  }
219  if (!putenv('TZ=' . $a_timezone)) {
220  $ilLog->write(__METHOD__ . ': Cannot set TZ environment variable. Please register TZ in php.ini (safe_mode_allowed_env_vars). Timezone');
221  throw new ilTimeZoneException('Cannot set TZ environment variable.');
222  }
223  self::$current_timezone = $a_timezone;
224  return true;
225  }
226 
235  public static function _setDefaultTimeZone($a_tz)
236  {
237  // Save the server timezone, since there is no way to read later.
238  if (!self::$server_timezone) {
239  self::$server_timezone = self::_getDefaultTimeZone();
240  }
241 
242  self::$default_timezone = $a_tz;
243  }
244 
253  public static function _restoreDefaultTimeZone()
254  {
255  self::$default_timezone = self::$server_timezone;
256  self::_switchTimeZone(self::$default_timezone);
257  }
258 
266  public static function _getDefaultTimeZone()
267  {
268  if (strlen(self::$default_timezone)) {
269  return self::$default_timezone;
270  }
271  // PHP >= 5.2.0
272  // php throws a warning date_default_timezone_get relies on os determination. There is no way to check if this could happen.
273  if (function_exists('date_default_timezone_get') and $tz = @date_default_timezone_get()) {
274  return self::$default_timezone = $tz;
275  }
276  // PHP ini option (PHP >= 5.1.0)
277  if ($tz = ini_get('date.timezone')) {
278  return self::$default_timezone = $tz;
279  }
280  // is $_ENV['PHP_TZ'] set ?
281  if ($tz = getenv('PHP_TZ')) {
282  return self::$default_timezone = $tz;
283  }
284  // is $_ENV['TZ'] set ?
285  if ($tz = getenv('TZ')) {
286  return self::$default_timezone = $tz;
287  }
288  if (strlen($tz = date('T'))) {
289  return self::$default_timezone = $tz;
290  }
291  return self::$default_timezone = self::UTC;
292  }
293 
298  public static function initDefaultTimeZone(ilIniFile $ini)
299  {
300  $tz = $ini->readVariable('server', 'timezone');
301  if (!strlen($tz)) {
302  $tz = self::_getDefaultTimeZone();
303  }
304  if (!strlen($tz)) {
305  $tz = 'UTC';
306  }
307  date_default_timezone_set($tz);
308  return $tz;
309  }
310 }
static _getDefaultTimeZone()
Calculate and set default time zone.
Set timezone
validateTZ()
validate timezone
This class offers methods for timezone handling.
readVariable($a_group, $a_var_name)
reads a single variable from a group public
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
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
Class for TimeZone exceptions.
static $current_timezone
Create styles array
The data for the language used.
static _getInstance($a_tz='')
get instance by timezone
static _switchTimeZone($a_timezone)
Switch tz.
INIFile Parser.
$ini
Definition: raiseError.php:4