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