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