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