FusionDirectory
class_timezone.inc
Go to the documentation of this file.
1 <?php
2 
3 /*
4  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
5  Copyright (C) 2003-2010 Cajus Pollmeier
6  Copyright (C) 2011-2016 FusionDirectory
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
32 class timezone
33 {
34  /*
35  * \brief This function sets the default timezone according to fusiondirectory configuration.
36  *
37  * \return TRUE upon success, FALSE otherwise
38  */
39  static public function setDefaultTimezoneFromConfig ()
40  {
41  global $config;
42 
43  /* Is there a timezone set in the fusiondirectory configuration */
44  if ($config->get_cfg_value('timezone') != '') {
45  $tz = $config->get_cfg_value('timezone');
46 
47  if (@date_default_timezone_set($tz)) {
48  return TRUE;
49  } else {
50  $error = new FusionDirectoryError(
51  htmlescape(sprintf(
52  _('The timezone setting "%s" in your configuration is not valid.'),
53  $tz
54  ))
55  );
56  $error->display();
57  }
58  }
59  return FALSE;
60  }
61 
62  /*
63  * \brief This function returns the offset for the default timezone.
64  *
65  * deprecated
66  * \param $stamp is used to detect summer or winter time.
67  */
68  static public function get_default_timezone ($stamp = NULL)
69  {
70  global $config;
71 
72  /* Use current timestamp if $stamp is not set */
73  if ($stamp === NULL) {
74  $stamp = time();
75  }
76 
77  /* Is there a correct timezone set in the fusiondirectory configuration */
78  if (static::setDefaultTimezoneFromConfig()) {
79  $tz = $config->get_cfg_value('timezone');
80  $tz_delta = date('Z', $stamp);
81  $tz_delta = $tz_delta / 3600;
82  return ['name' => $tz, 'value' => $tz_delta];
83  } else {
84  return ['name' => 'unconfigured', 'value' => 0];
85  }
86  }
87 
88  /*
89  * \brief Get the time zone informations
90  *
91  * \return Time zone informations
92  */
93  static public function _get_tz_zones ()
94  {
95  return DateTimeZone::listIdentifiers();
96  }
97 
98  /* \brief Return default timezone as a DateTimeZone object */
99  static public function getDefaultTimeZone ()
100  {
101  return new DateTimeZone(date_default_timezone_get());
102  }
103 
104  /* \brief Return UTC timezone as a DateTimeZone object */
105  static public function utc ()
106  {
107  static $utc;
108  if (!isset($utc)) {
109  $utc = new DateTimeZone('UTC');
110  }
111  return $utc;
112  }
113 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
This class contains all the function needed to manage the timezones.
Parent class for all errors in FusionDirectory.