fix #8131 - chinese translations
[Pman.Core] / TimeZone.php
1 <?php
2 require_once 'Pman.php';
3
4 class Pman_Core_TimeZone extends Pman
5 {
6     function getAuth()
7     {
8         parent::getAuth();
9         
10         if (!$this->getAuthUser()) {  
11             $this->jerr("Not authenticated", array('authFailure' => true));
12         }
13         
14         return true;
15     }
16
17     function get($base, $opts=array())
18     {
19         self::getTimezones();
20
21         $data = array();
22
23         foreach(self::$timezones as $tz => $o) {
24             if(!empty($_REQUEST['region']) && $_REQUEST['region'] != $o['region']) {
25                 continue;
26             }
27
28             if(
29                 !empty($_REQUEST['query']['area_start']) 
30                 && 
31                 substr(strtolower($o['area']), 0, strlen($_REQUEST['query']['area_start'])) != strtolower($_REQUEST['query']['area_start'])
32             ){
33                 continue;
34             }
35             $data[] = array(
36                 'region' => $o['region'],
37                 'area' => $o['area'],
38                 'displayArea' => $o['displayArea']
39             );
40         }
41
42         echo json_encode(array(
43             'data' => $data,
44             'metaData' => array(
45                 'root' => 'data',
46                 'successProperty' => 'success',
47                 'totalProperty' => 'total',
48                 'fields' => array(
49                     'region',
50                     'area',
51                     'displayArea'
52                 )
53             ),
54             'success' => true,
55             'total' => count($data),
56             
57         ));
58         exit;
59     }
60
61     function post($base) {
62         die('Invalid post');
63     }
64
65     static $timezones = array();
66
67     static function getTimezones()
68     {
69         if(!empty(self::$timezones)) {
70             return self::$timezones;
71         }
72          $ce = DB_DataObject::factory('core_enum');
73         $ce->query("
74             SELECT
75                 *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, 'UTC')), '%H:%i') as timeOffset
76             FROM
77                 mysql.time_zone_name
78             WHERE
79                 Name LIKE '%/%'
80                 AND
81                 Name NOT LIKE '%/%/%'
82                 AND
83                 Name NOT LIKE 'right%'
84                 AND
85                 Name NOT LIKE 'posix%'
86                 AND
87                 Name NOT LIKE 'Etc%'
88             ORDER BY
89                 SUBSTRING_INDEX(Name, '/', 1) ASC,
90                 timeoffset ASC,
91                 Name ASC
92         ");
93
94         while($ce->fetch()) {
95             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
96            
97
98             $ar = explode('/', $ce->Name);
99             // ignore timezone such as 'Etc/GMT+8'
100            
101
102             $displayArea = str_replace('_', ' ', $ar[1]);
103
104             $timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
105             $displayOffset = '(GMT ' . $timeOffset . ')';
106
107             self::$timezones[$ce->Name] = array(
108                 'region' => $ar[0],
109                 'area' => $ar[1],
110                 'displayName' => $ar[0] . '/' . $displayArea . ' ' . $displayOffset,
111                 'displayArea' => $displayArea . ' ' . $displayOffset
112             );
113         }
114
115         return self::$timezones;
116     }
117
118     static function isValidTimeZone($tz) {
119         try {
120             new DateTimeZone($tz);
121         }
122         catch (Exception $e) {
123             return false;
124         }
125
126         return true;
127     }
128
129     static function toRegion($tz)
130     {
131         if(!self::isValidTimeZone($tz)) {
132             return '';
133         }
134         
135         return explode('/', $tz)[0];
136     }
137
138     static function toArea($tz)
139     {
140         if(!self::isValidTimeZone($tz)) {
141             return '';
142         }
143
144         return explode('/', $tz)[1];
145     }
146     
147     static function toTimeOffset($dt, $tz)
148     {
149         if(!self::isValidTimeZone($tz)) {
150             return '';
151         }
152
153         if($dt == '0000-00-00 00:00:00' || $dt == '') {
154             $dt = 'NOW';
155         }
156
157         $date = new DateTime($dt, new DateTimeZone($tz));
158         return $date->format('P');
159     }
160
161     static function toDisplayArea($dt, $tz)
162     {
163         return str_replace('_', ' ', self::toArea($tz)) . ' (GMT ' . self::toTimeOffset($dt,$tz) . ')';
164
165     }
166
167     static function toDisplayName($dt, $tz)
168     {
169         return self::toRegion($tz) . '/' . self::toDisplayArea($dt, $tz);
170     }
171 }