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