2 require_once 'Pman.php';
4 class Pman_Core_TimeZone extends Pman
10 if (!$this->getAuthUser()) {
11 $this->jerr("Not authenticated", array('authFailure' => true));
17 function get($base, $opts=array())
19 $this->lang = !empty($_REQUEST['lang']) && in_array($_REQUEST['lang'], array('en', 'zh_CN')) ? $_REQUEST['lang'] : 'en';
20 self::getTimezones($this->lang);
24 foreach(self::$timezones as $tz => $o) {
25 if(!empty($_REQUEST['region']) && $_REQUEST['region'] != $o['region']) {
30 !empty($_REQUEST['query']['area_start'])
32 substr(strtolower($o['area']), 0, strlen($_REQUEST['query']['area_start'])) != strtolower($_REQUEST['query']['area_start'])
37 'region' => $o['region'],
39 'displayRegion' => $o['displayRegion'],
40 'displayArea' => $o['displayArea']
44 echo json_encode(array(
48 'successProperty' => 'success',
49 'totalProperty' => 'total',
58 'total' => count($data),
64 function post($base) {
68 static $timezones = array();
70 static function getTimezones($lang)
72 if(!empty(self::$timezones)) {
73 return self::$timezones;
75 $ce = DB_DataObject::factory('core_enum');
78 *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, 'UTC')), '%H:%i') as timeOffset
86 Name NOT LIKE 'right%'
88 Name NOT LIKE 'posix%'
92 SUBSTRING_INDEX(Name, '/', 1) ASC,
97 $regions = DB_DataObject::factory('core_enum');
98 $regions->setFrom(array(
99 'etype' => 'Timezone.Region',
102 $regionIds = $regions->fetchAll('display_name', 'id');
104 $areas = DB_DataObject::factory('core_enum');
105 $areas->setFrom(array(
106 'etype' => 'Timezone.Area',
109 $areaIds = $areas->fetchAll('display_name', 'id');
112 $ct = DB_DataObject::factory('core_templatestr');
114 $ct->on_table = 'core_enum';
116 $translations = array();
117 foreach($ct->fetchAll() as $t) {
121 $translations[$t->on_id][$t->on_col] = $t->txt;
124 while($ce->fetch()) {
125 // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
128 $ar = explode('/', $ce->Name);
129 // ignore timezone such as 'Etc/GMT+8'
131 $region = $displayRegion = $ar[0];
133 if(!empty($translations[$regionIds[$region]]['display_name'])) {
134 $displayRegion = $translations[$regionIds[$region]]['display_name'];
138 $displayArea = str_replace('_', ' ', $area);
140 if(!empty($translations[$areaIds[$displayArea]]['display_name'])) {
141 $displayArea = $translations[$areaIds[$displayArea]]['display_name'];
144 $timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
145 $displayOffset = '(GMT ' . $timeOffset . ')';
147 self::$timezones[$ce->Name] = array(
150 'displayName' => $ar[0] . '/' . $displayArea . ' ' . $displayOffset,
151 'displayRegion' => $displayRegion,
152 'displayArea' => $displayArea . ' ' . $displayOffset
156 return self::$timezones;
159 static function isValidTimeZone($tz) {
161 new DateTimeZone($tz);
163 catch (Exception $e) {
170 static function toRegion($tz)
172 if(!self::isValidTimeZone($tz)) {
176 return explode('/', $tz)[0];
179 static function toArea($tz)
181 if(!self::isValidTimeZone($tz)) {
185 return explode('/', $tz)[1];
188 static function toTimeOffset($dt, $tz)
190 if(!self::isValidTimeZone($tz)) {
194 if($dt == '0000-00-00 00:00:00' || $dt == '') {
198 $date = new DateTime($dt, new DateTimeZone($tz));
199 return $date->format('P');
202 static function toDisplayRegion($lang, $tz)
204 $region = explode('/', $tz)[0];
206 $ce = DB_DataObject::factory('core_enum');
208 'etype' => 'Timezone.Region',
211 'display_name' => $region
213 if(!$ce->find(true)) {
217 $ct = DB_DataObject::factory('core_templatestr');
220 'on_table' => 'core_enum',
222 'on_col' => 'display_name',
225 if(!$ct->find(true) || empty($ct->txt)) {
231 static function toDisplayArea($lang, $dt, $tz)
233 $displayArea = str_replace('_', ' ', self::toArea($tz));
234 $displayOffset = '(GMT ' . self::toTimeOffset($dt,$tz) . ')';
236 $ce = DB_DataObject::factory('core_enum');
238 'etype' => 'Timezone.Area',
240 'name' => $displayArea,
241 'display_name' => $displayArea
244 if(!$ce->find(true)) {
245 return $displayArea . ' ' . $displayOffset;
248 $ct = DB_DataObject::factory('core_templatestr');
251 'on_table' => 'core_enum',
253 'on_col' => 'display_name',
256 if(!$ct->find(true) || empty($ct->txt)) {
257 return $displayArea . ' ' . $displayOffset;
260 return $ct->txt . ' ' . $displayOffset;
264 static function toDisplayName($lang, $dt, $tz)
266 return self::toDisplayRegion($lang, $tz) . '/' . self::toDisplayArea($lang, $dt, $tz);