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