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