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