Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class tz {
- public static $detection_failed = false;
- private static $timezone = 'UTC';
- private static $default_format = 'Y-m-d H:i:s';
- public static function zone()
- {
- return TzDate::default_timezone();
- }
- public static function date($timestamp = null)
- {
- if ($timestamp == null)
- {
- $timestamp = 'now';
- }
- else if (is_numeric($timestamp) and $timestamp > 0)
- {
- $timestamp = '@'.$timestamp;
- }
- return new TzDate($timestamp);
- }
- public static function utc($timestamp = null)
- {
- if ($timestamp == null)
- {
- $timestamp = 'now';
- }
- else if (is_numeric($timestamp) and $timestamp > 0)
- {
- $timestamp = '@'.$timestamp;
- }
- $dt = new TzDate($timestamp, TzDate::default_timezone());
- $dt->timezone('UTC');
- return $dt;
- }
- public static function set_default_timezone($timezone, $save = true)
- {
- $timezones = timezone_identifiers_list();
- if (! in_array($timezone, $timezones))
- {
- throw new Kohana_Exception('Attempted to set invalid timezone :tz: as default timezone', array(':tz:'=>$timezone));
- }
- TzDate::default_timezone($timezone);
- TzDate::default_format(self::$default_format);
- if ($save)
- {
- $sess = Session::instance();
- $sess->set('fmt_tz', $timezone);
- }
- }
- public static function get_timezone_by_offset($offset)
- {
- $zone = self::get_zone($offset);
- return $zone['timezone'];
- }
- public static function get_default_timezone()
- {
- $sess = Session::instance();
- $ex_tz = $sess->get('fmt_tz', null);
- if ($ex_tz === null)
- {
- $ex_tz = self::get_ip_default_timezone();
- if ($ex_tz === null)
- {
- self::$detection_failed = true;
- return 'UTC';
- }
- }
- return $ex_tz;
- }
- public static function get_ip_default_timezone()
- {
- return self::get_default_timezone_for_ip(Input::instance()->ip_address());
- }
- public static function get_default_timezone_for_ip($ip_address)
- {
- return KoGeoIP::timezone($ip_address);
- }
- public static function get_zones()
- {
- return self::$zones;
- }
- public static function get_zone($offset = 0)
- {
- $name = 'GMT'.sprintf('%+d', $offset);
- if (isset(self::$zones[$name]))
- {
- $zone = self::$zones[$name][0];
- $cities = self::$zones[$name][1];
- sort($cities);
- return array('timezone' => $zone, 'cities' => implode(', ', $cities), 'name' => $name, 'offset' => $offset);
- }
- }
- private static $zones = array(
- 'GMT-11' => array(
- 'Pacific/Niue',
- array('Niue', 'Midway', 'Pago Pago',),
- ),
- 'GMT-10' => array(
- 'Pacific/Tahiti',
- array('Hawaii', 'Tahiti', 'Adak', 'Honolulu', 'Rarotonga'),
- ),
- 'GMT-9' => array(
- 'Pacific/Gambier',
- array('Juneau', 'Yakutat', 'Nome', 'Anchorage', 'Gambier'),
- ),
- 'GMT-8' => array(
- 'Pacific/Pitcairn',
- array('Los Angeles', 'Vancouver', 'Whitehorse', 'Dawson', 'Tijuana'),
- ),
- 'GMT-7' => array(
- 'America/Dawson_Creek',
- array('Denver', 'Phoenix', 'Chiuaua', 'Ojinaga', 'Edmonton'),
- ),
- 'GMT-6' => array(
- 'America/Costa_Rica',
- array('Chicago', 'Mexico City', 'Winnipeg', 'Galapagos', 'Guatemala'),
- ),
- 'GMT-5' => array(
- 'America/Bogota',
- array('New York', 'Detroit', 'Toronto', 'Havana', 'Jamaica'),
- ),
- 'GMT-4' => array(
- 'America/Barbados',
- array('Barbados', 'Bermuda', 'Santiago', 'Puerto Rico', 'Caracas'),
- ),
- 'GMT-3' => array(
- 'America/Argentina/San_Juan',
- array('San Luis', 'Catamarca', 'Bahia', 'Cayenne', 'Montevideo'),
- ),
- 'GMT-2' => array(
- 'America/Noronha',
- array('South Georgia', 'Noronha'),
- ),
- 'GMT-1' => array(
- 'Atlantic/Cape_Verde',
- array('Cape Verde', 'Scoresbysund', 'Azores'),
- ),
- 'GMT+0' => array(
- 'Africa/Abidjan',
- array('London', 'Casablanca', 'Reykjavik', 'Lisbon', 'Dakar'),
- ),
- 'GMT+1' => array(
- 'Africa/Ndjamena',
- array('Berlin', 'Vienna', 'Douala', 'Tunis', 'Copenhagen', 'Zurich', 'Prague', 'Algiers', 'Madrid', 'Gibraltar', 'Rome', 'Monaco', 'Amsterdam', 'Oslo'),
- ),
- 'GMT+2' => array(
- 'Africa/Johannesburg',
- array('Johannesburg', 'Sofia', 'Minsk', 'Cairo', 'Helsinki', 'Beirut', 'Riga', 'Jerusalem', 'Gaza', 'Bucharest', 'Damascus', 'Kiev'),
- ),
- 'GMT+3' => array(
- 'Asia/Bahrain',
- array('Bahrain', 'Djibouti', 'Addis Ababa', 'Baghdad', 'Nairobi', 'Kuwait', 'Qatar', 'Moscow', 'Volgograd'),
- ),
- 'GMT+4' => array(
- 'Asia/Tbilisi',
- array('Dubai', 'Mauritius', 'Muscat', 'Mahe', 'Baku'),
- ),
- 'GMT+5' => array(
- 'Asia/Aqtobe',
- array('Oral', 'Maldives', 'Ashgabat', 'Dushanbe', 'Tashkent'),
- ),
- 'GMT+6' => array(
- 'Asia/Dhaka',
- array('Dhaka', 'Thimphu', 'Omsk', 'Novosibirsk', 'Chagos'),
- ),
- 'GMT+7' => array(
- 'Asia/Jakarta',
- array('Jakarta', 'Pontianak', 'Krasnoyarsk', 'Bangkok', 'Ho Chi Minh'),
- ),
- 'GMT+8' => array(
- 'Asia/Ulaanbaatar',
- array('Ulaanbaatar', 'Macau', 'Kuala Lumpur', 'Irkutsk', 'Singapore', 'Taipei', 'Perth', 'Brunei', 'Shanghai', 'Kashgar', 'Hong Kong'),
- ),
- 'GMT+9' => array(
- 'Pacific/Palau',
- array('Tokyo', 'Pyongyang', 'Seoul', 'Jayapura', 'Yakutsk'),
- ),
- 'GMT+10' => array(
- 'Australia/Brisbane',
- array('Brisbane', 'Hobart', 'Sydney', 'Truk', 'Guam', 'Vladivostok', 'Melbourne'),
- ),
- 'GMT+11' => array(
- 'Pacific/Ponape',
- array('Kamchatka', 'Magadan', 'Guadalcanal', 'Ponape', 'Noumea'),
- ),
- 'GMT+12' => array(
- 'Pacific/Funafuti',
- array('Wake', 'Wallis', 'Tarawa', 'Majuro', 'Nauru'),
- ),
- 'GMT+13' => array(
- 'Pacific/Enderbury',
- array('Enderbury', 'Tongatapu'),
- ),
- 'GMT+14' => array(
- 'Pacific/Kiritimati',
- array('Christmas Island'),
- ),
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement