simplify timezone
[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
73         $ce = DB_DataObject::factory('core_enum');
74         $ce->query("
75             SELECT
76                 *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, 'UTC')), '%H:%i') as timeOffset
77             FROM
78                 mysql.time_zone_name
79             WHERE
80                 Name LIKE '%/%'
81                 AND
82                 Name NOT LIKE '%/%/%'
83                 AND
84                 Name NOT LIKE 'Etc%'
85             ORDER BY
86                 timeoffset DESC,
87                 Name DESC
88         ");
89
90         while($ce->fetch()) {
91             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
92            
93
94             $ar = explode('/', $ce->Name);
95             // ignore timezone such as 'Etc/GMT+8'
96            
97
98             $displayArea = str_replace('_', ' ', $ar[1]);
99
100             $timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
101             $displayOffset = '(GMT ' . $timeOffset . ')';
102
103             self::$timezones[$ce->Name] = array(
104                 'region' => $ar[0],
105                 'area' => $ar[1],
106                 'displayName' => $ar[0] . '/' . $displayArea . ' ' . $displayOffset,
107                 'displayArea' => $displayArea . ' ' . $displayOffset
108             );
109         }
110
111         return self::$timezones;
112     }
113
114     static function isValidTimeZone($tz) {
115         try {
116             new DateTimeZone($tz);
117         }
118         catch (Exception $e) {
119             return false;
120         }
121
122         return true;
123     }
124
125     static function toRegion($tz)
126     {
127         if(!self::isValidTimeZone($tz)) {
128             return '';
129         }
130         
131         return explode('/', $tz)[0];
132     }
133
134     static function toArea($tz)
135     {
136         if(!self::isValidTimeZone($tz)) {
137             return '';
138         }
139
140         return explode('/', $tz)[1];
141     }
142     
143     static function toTimeOffset($dt, $tz)
144     {
145         if(!self::isValidTimeZone($tz)) {
146             return '';
147         }
148
149         $date = new DateTime($dt, new DateTimeZone($tz));
150         return $date->format('P');
151     }
152
153     static function toDisplayArea($dt, $tz)
154     {
155         return str_replace('_', ' ', self::toArea($tz)) . ' (GMT ' . self::toTimeOffset($dt,$tz) . ')';
156
157     }
158
159     static function toDisplayName($dt, $tz)
160     {
161         return self::toRegion($tz) . '/' . self::toDisplayArea($dt, $tz);
162     }
163 }