DataObjects/Images.php
[Pman.Core] / TimeZone.php
index 8a7fe08..72a629f 100644 (file)
@@ -20,27 +20,35 @@ class Pman_Core_TimeZone extends Pman
 
         $data = array();
 
-        foreach(self::$timezones as $tz => $offset) {
-            $arr = explode('/', $tz);
+        foreach(self::$timezones as $tz => $o) {
+            if(!empty($_REQUEST['region']) && $_REQUEST['region'] != $o['region']) {
+                continue;
+            }
+
+            if(
+                !empty($_REQUEST['query']['area_start']) 
+                && 
+                substr(strtolower($o['area']), 0, strlen($_REQUEST['query']['area_start'])) != strtolower($_REQUEST['query']['area_start'])
+            ){
+                continue;
+            }
             $data[] = array(
-                'region' => $arr[0],
-                'area' => $arr[1],
-                'offset' => $offset
-                'displayArea' => $
-            )
+                'region' => $o['region'],
+                'area' => $o['area'],
+                'displayArea' => $o['displayArea']
+            );
         }
 
         echo json_encode(array(
             'data' => $data,
             'metaData' => array(
-                'id' => 'id',
                 'root' => 'data',
                 'successProperty' => 'success',
                 'totalProperty' => 'total',
                 'fields' => array(
                     'region',
                     'area',
-                    'offset'
+                    'displayArea'
                 )
             ),
             'success' => true,
@@ -61,48 +69,103 @@ class Pman_Core_TimeZone extends Pman
         if(!empty(self::$timezones)) {
             return self::$timezones;
         }
-
-        $ce = DB_DataObject::factory('core_enum');
-        $ce->query('
+         $ce = DB_DataObject::factory('core_enum');
+        $ce->query("
             SELECT
-                *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as offset
+                *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, 'UTC')), '%H:%i') as timeOffset
             FROM
                 mysql.time_zone_name
+            WHERE
+                Name LIKE '%/%'
+                AND
+                Name NOT LIKE '%/%/%'
+                AND
+                Name NOT LIKE 'right%'
+                AND
+                Name NOT LIKE 'posix%'
+                AND
+                Name NOT LIKE 'Etc%'
             ORDER BY
-                offset DESC,
-                Name DESC
-        ');
+                SUBSTRING_INDEX(Name, '/', 1) ASC,
+                timeoffset ASC,
+                Name ASC
+        ");
 
         while($ce->fetch()) {
             // ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
-            if(substr_count($ce->Name, '/') != 1) {
-                continue;
-            }
+           
 
             $ar = explode('/', $ce->Name);
             // ignore timezone such as 'Etc/GMT+8'
-            if($ar[0] == 'Etc') {
-                continue;
-            }
+           
 
-            self::$timezones[$ce->Name] = $ce->offset;
+            $displayArea = str_replace('_', ' ', $ar[1]);
+
+            $timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
+            $displayOffset = '(GMT ' . $timeOffset . ')';
+
+            self::$timezones[$ce->Name] = array(
+                'region' => $ar[0],
+                'area' => $ar[1],
+                'displayName' => $ar[0] . '/' . $displayArea . ' ' . $displayOffset,
+                'displayArea' => $displayArea . ' ' . $displayOffset
+            );
         }
 
         return self::$timezones;
     }
 
-    static function getDisplayArea($timezone)
-    {
-        self::getTimezones();
+    static function isValidTimeZone($tz) {
+        try {
+            new DateTimeZone($tz);
+        }
+        catch (Exception $e) {
+            return false;
+        }
 
-        // invalid timezone
-        if(!isset(self::$timezones[$timezone])) {
+        return true;
+    }
+
+    static function toRegion($tz)
+    {
+        if(!self::isValidTimeZone($tz)) {
             return '';
         }
-
-        $ar = explode('/', $timezones);
         
+        return explode('/', $tz)[0];
     }
 
+    static function toArea($tz)
+    {
+        if(!self::isValidTimeZone($tz)) {
+            return '';
+        }
+
+        return explode('/', $tz)[1];
+    }
     
+    static function toTimeOffset($dt, $tz)
+    {
+        if(!self::isValidTimeZone($tz)) {
+            return '';
+        }
+
+        if($dt == '0000-00-00 00:00:00' || $dt == '') {
+            $dt = 'NOW';
+        }
+
+        $date = new DateTime($dt, new DateTimeZone($tz));
+        return $date->format('P');
+    }
+
+    static function toDisplayArea($dt, $tz)
+    {
+        return str_replace('_', ' ', self::toArea($tz)) . ' (GMT ' . self::toTimeOffset($dt,$tz) . ')';
+
+    }
+
+    static function toDisplayName($dt, $tz)
+    {
+        return self::toRegion($tz) . '/' . self::toDisplayArea($dt, $tz);
+    }
 }
\ No newline at end of file