Advertisement
Guest User

tz: wrapper around TzDate class

a guest
Mar 30th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.89 KB | None | 0 0
  1. <?php
  2.  
  3. class tz {
  4.  
  5.     public static $detection_failed = false;
  6.  
  7.     private static $timezone = 'UTC';
  8.  
  9.     private static $default_format = 'Y-m-d H:i:s';
  10.  
  11.     public static function zone()
  12.     {
  13.         return TzDate::default_timezone();
  14.     }
  15.  
  16.     public static function date($timestamp = null)
  17.     {
  18.         if ($timestamp == null)
  19.         {
  20.             $timestamp = 'now';
  21.         }
  22.         else if (is_numeric($timestamp) and $timestamp > 0)
  23.         {
  24.             $timestamp = '@'.$timestamp;
  25.         }
  26.  
  27.         return new TzDate($timestamp);
  28.     }
  29.  
  30.     public static function utc($timestamp = null)
  31.     {
  32.         if ($timestamp == null)
  33.         {
  34.             $timestamp = 'now';
  35.         }
  36.         else if (is_numeric($timestamp) and $timestamp > 0)
  37.         {
  38.             $timestamp = '@'.$timestamp;
  39.         }
  40.  
  41.         $dt = new TzDate($timestamp, TzDate::default_timezone());
  42.         $dt->timezone('UTC');
  43.         return $dt;
  44.     }
  45.  
  46.     public static function set_default_timezone($timezone, $save = true)
  47.     {
  48.         $timezones = timezone_identifiers_list();
  49.         if (! in_array($timezone, $timezones))
  50.         {
  51.             throw new Kohana_Exception('Attempted to set invalid timezone :tz: as default timezone', array(':tz:'=>$timezone));
  52.         }
  53.  
  54.         TzDate::default_timezone($timezone);
  55.         TzDate::default_format(self::$default_format);
  56.  
  57.         if ($save)
  58.         {
  59.             $sess = Session::instance();
  60.             $sess->set('fmt_tz', $timezone);
  61.         }
  62.     }
  63.  
  64.     public static function get_timezone_by_offset($offset)
  65.     {
  66.         $zone = self::get_zone($offset);
  67.  
  68.         return $zone['timezone'];
  69.     }
  70.  
  71.     public static function get_default_timezone()
  72.     {
  73.         $sess = Session::instance();
  74.  
  75.         $ex_tz = $sess->get('fmt_tz', null);
  76.  
  77.         if ($ex_tz === null)
  78.         {
  79.             $ex_tz = self::get_ip_default_timezone();
  80.  
  81.             if ($ex_tz === null)
  82.             {
  83.                 self::$detection_failed = true;
  84.                 return 'UTC';
  85.             }
  86.         }
  87.  
  88.         return $ex_tz;
  89.     }
  90.  
  91.     public static function get_ip_default_timezone()
  92.     {
  93.         return self::get_default_timezone_for_ip(Input::instance()->ip_address());
  94.     }
  95.    
  96.     public static function get_default_timezone_for_ip($ip_address)
  97.     {
  98.         return KoGeoIP::timezone($ip_address);
  99.     }
  100.  
  101.     public static function get_zones()
  102.     {
  103.         return self::$zones;
  104.     }
  105.  
  106.     public static function get_zone($offset = 0)
  107.     {
  108.         $name = 'GMT'.sprintf('%+d', $offset);
  109.         if (isset(self::$zones[$name]))
  110.         {
  111.             $zone = self::$zones[$name][0];
  112.             $cities = self::$zones[$name][1];
  113.             sort($cities);
  114.             return array('timezone' => $zone, 'cities' => implode(', ', $cities), 'name' => $name, 'offset' => $offset);
  115.         }
  116.     }
  117.  
  118.     private static $zones = array(
  119.         'GMT-11' => array(
  120.             'Pacific/Niue',
  121.             array('Niue', 'Midway', 'Pago Pago',),
  122.         ),
  123.  
  124.         'GMT-10' => array(
  125.             'Pacific/Tahiti',
  126.             array('Hawaii', 'Tahiti', 'Adak', 'Honolulu', 'Rarotonga'),
  127.         ),
  128.  
  129.         'GMT-9' => array(
  130.             'Pacific/Gambier',
  131.             array('Juneau', 'Yakutat', 'Nome', 'Anchorage', 'Gambier'),
  132.         ),
  133.  
  134.         'GMT-8' => array(
  135.             'Pacific/Pitcairn',
  136.             array('Los Angeles', 'Vancouver', 'Whitehorse', 'Dawson', 'Tijuana'),
  137.         ),
  138.  
  139.         'GMT-7' => array(
  140.             'America/Dawson_Creek',
  141.             array('Denver', 'Phoenix', 'Chiuaua', 'Ojinaga', 'Edmonton'),
  142.         ),
  143.  
  144.         'GMT-6' => array(
  145.             'America/Costa_Rica',
  146.             array('Chicago', 'Mexico City', 'Winnipeg', 'Galapagos', 'Guatemala'),
  147.         ),
  148.  
  149.         'GMT-5' => array(
  150.             'America/Bogota',
  151.             array('New York', 'Detroit', 'Toronto', 'Havana', 'Jamaica'),
  152.         ),
  153.  
  154.         'GMT-4' => array(
  155.             'America/Barbados',
  156.             array('Barbados', 'Bermuda', 'Santiago', 'Puerto Rico', 'Caracas'),
  157.         ),
  158.  
  159.         'GMT-3' => array(
  160.             'America/Argentina/San_Juan',
  161.             array('San Luis', 'Catamarca', 'Bahia', 'Cayenne', 'Montevideo'),
  162.         ),
  163.  
  164.         'GMT-2' => array(
  165.             'America/Noronha',
  166.             array('South Georgia', 'Noronha'),
  167.         ),
  168.  
  169.         'GMT-1' => array(
  170.             'Atlantic/Cape_Verde',
  171.             array('Cape Verde', 'Scoresbysund', 'Azores'),
  172.         ),
  173.  
  174.         'GMT+0' => array(
  175.             'Africa/Abidjan',
  176.             array('London', 'Casablanca', 'Reykjavik', 'Lisbon', 'Dakar'),
  177.         ),
  178.  
  179.         'GMT+1' => array(
  180.             'Africa/Ndjamena',
  181.             array('Berlin', 'Vienna', 'Douala', 'Tunis', 'Copenhagen', 'Zurich', 'Prague', 'Algiers', 'Madrid', 'Gibraltar', 'Rome', 'Monaco', 'Amsterdam', 'Oslo'),
  182.         ),
  183.  
  184.         'GMT+2' => array(
  185.             'Africa/Johannesburg',
  186.             array('Johannesburg', 'Sofia', 'Minsk', 'Cairo', 'Helsinki', 'Beirut', 'Riga', 'Jerusalem', 'Gaza', 'Bucharest', 'Damascus', 'Kiev'),
  187.         ),
  188.  
  189.         'GMT+3' => array(
  190.             'Asia/Bahrain',
  191.             array('Bahrain', 'Djibouti', 'Addis Ababa', 'Baghdad', 'Nairobi', 'Kuwait', 'Qatar', 'Moscow', 'Volgograd'),
  192.         ),
  193.  
  194.         'GMT+4' => array(
  195.             'Asia/Tbilisi',
  196.             array('Dubai', 'Mauritius', 'Muscat', 'Mahe', 'Baku'),
  197.         ),
  198.  
  199.         'GMT+5' => array(
  200.             'Asia/Aqtobe',
  201.             array('Oral', 'Maldives', 'Ashgabat', 'Dushanbe', 'Tashkent'),
  202.         ),
  203.  
  204.         'GMT+6' => array(
  205.             'Asia/Dhaka',
  206.             array('Dhaka', 'Thimphu', 'Omsk', 'Novosibirsk', 'Chagos'),
  207.         ),
  208.  
  209.         'GMT+7' => array(
  210.             'Asia/Jakarta',
  211.             array('Jakarta', 'Pontianak', 'Krasnoyarsk', 'Bangkok', 'Ho Chi Minh'),
  212.         ),
  213.  
  214.         'GMT+8' => array(
  215.             'Asia/Ulaanbaatar',
  216.             array('Ulaanbaatar', 'Macau', 'Kuala Lumpur', 'Irkutsk', 'Singapore', 'Taipei', 'Perth', 'Brunei', 'Shanghai', 'Kashgar', 'Hong Kong'),
  217.         ),
  218.  
  219.         'GMT+9' => array(
  220.             'Pacific/Palau',
  221.             array('Tokyo', 'Pyongyang', 'Seoul', 'Jayapura', 'Yakutsk'),
  222.         ),
  223.  
  224.         'GMT+10' => array(
  225.             'Australia/Brisbane',
  226.             array('Brisbane', 'Hobart', 'Sydney', 'Truk', 'Guam', 'Vladivostok', 'Melbourne'),
  227.         ),
  228.  
  229.         'GMT+11' => array(
  230.             'Pacific/Ponape',
  231.             array('Kamchatka', 'Magadan', 'Guadalcanal', 'Ponape', 'Noumea'),
  232.         ),
  233.  
  234.         'GMT+12' => array(
  235.             'Pacific/Funafuti',
  236.             array('Wake', 'Wallis', 'Tarawa', 'Majuro', 'Nauru'),
  237.         ),
  238.  
  239.         'GMT+13' => array(
  240.             'Pacific/Enderbury',
  241.             array('Enderbury', 'Tongatapu'),
  242.         ),
  243.  
  244.         'GMT+14' => array(
  245.             'Pacific/Kiritimati',
  246.             array('Christmas Island'),
  247.         ),
  248.     );
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement