TimeZone.php
[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         $timezones = self::getTimezones();
20
21         $data = array();
22
23         foreach($data as $tz => $offset) {
24             $arr = explode('/', $tz);
25             $data[] = array(
26                 'region' => $arr[0],
27                 'area' => $arr[1],
28                 'offset' => $offset
29             )
30         }
31
32         echo json_encode(array(
33             'data' => $data,
34             'metaData' => array(
35                 'id' => 'id',
36                 'root' => 'data',
37                 'successProperty' => 'success',
38                 'totalProperty' => 'total',
39                 'fields' => array(
40                     'region',
41                     'area',
42                     'offset'
43                 )
44             ),
45             'success' => true,
46             'total' => count($data),
47             
48         ));
49         exit;
50     }
51
52     function post($base) {
53         die('Invalid post');
54     }
55
56     static $timezones = array();
57
58     static function getTimezones()
59     {
60         if(!empty(self::$timezones)) {
61             return self::$timezones;
62         }
63
64         $ce = DB_DataObject::factory('core_enum');
65         $ce->query('
66             SELECT
67                 *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as offset
68             FROM
69                 mysql.time_zone_name
70             ORDER BY
71                 offset DESC,
72                 Name DESC
73         ');
74
75         while($ce->fetch()) {
76             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
77             if(substr_count($ce->Name, '/') != 1) {
78                 continue;
79             }
80
81             $ar = explode('/', $ce->Name);
82             // ignore timezone such as 'Etc/GMT+8'
83             if($ar[0] == 'Etc') {
84                 continue;
85             }
86
87             self::$timezones[$ce->Name] = $ce->offset;
88         }
89
90         return self::$timezones;
91     }
92
93     static function getOffset($timezone)
94     {
95         $ce = DB_DataObject::factory('core_enum');
96         $ce->query('
97             SELECT
98                 TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as offset
99             FROM
100                 mysql.time_zone_name
101             WHERE
102                 Name = "' . $ce->escape($timezone) . '"
103         ');
104         $ce->fetch();
105
106         return empty($ce->offset) ? '' : $ce->offset;
107     }
108
109     
110 }