iPhone:Find the current timezone offset in HOURS

后端 未结 8 698
萌比男神i
萌比男神i 2021-02-05 03:11

How to find out the current timezone offset on the device in HOURS ?

相关标签:
8条回答
  • 2021-02-05 03:38

    Use NSTimeZone's localTimeZone

    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    NSInteger seconds = [timeZone secondsFromGMT];
    NSLog(@"TZ : %@ : Seconds %ld",[timeZone abbreviation],(long)seconds);
    
    int h = (int)seconds / 3600;
    int m = (int)seconds / 60 % 60;
    
    NSString *strGMT = @"";
    if (h>=0)
        strGMT = [NSString stringWithFormat:@"+%02d:%02d", h, m];
    else
        strGMT = [NSString stringWithFormat:@"%03d:%02d", h, m];
    
    NSLog(@"GMT -/+ HH:mm: %@",strGMT);
    
    0 讨论(0)
  • 2021-02-05 03:42

    For anyone looking for the current time zone offset as a string, try:

    NSDateFormatter *localTimeZoneFormatter = [NSDateFormatter new];
    localTimeZoneFormatter.timeZone = [NSTimeZone localTimeZone];
    localTimeZoneFormatter.dateFormat = @"Z";
    NSString *localTimeZoneOffset = [localTimeZoneFormatter stringFromDate:[NSDate date]];
    

    In my time zone, localTimeZoneOffset contains -0700

    0 讨论(0)
  • 2021-02-05 03:43

    There is a very nice NSTimeZone object which will give you the timezone offset from GMT in seconds:

    float timezoneoffset = ([[NSTimeZone systemTimeZone] secondsFromGMT] / 3600.0);
    

    For more see blog post

    http://blog.indieiphonedev.com/2011/01/09/getting-the-timezone-on-the-device/

    0 讨论(0)
  • 2021-02-05 03:46
    -(void)getDeviceUTCOffset{
    
        NSDate *date = [NSDate date];
        NSTimeZone* deviceTimeZone_ = [NSTimeZone systemTimeZone];
        float offset = [deviceTimeZone_ secondsFromGMTForDate:date] / 3600.0;   
    }
    

    To test code - test on a device

    change device Timezone in

    Settings > General > Date Time
    Turn off SET AUTOMATICALLY
    Then tap TimeZone and choose major cities east/west of you
    Run code and check float offset
    

    Compare value to cities on this site

    e.g. heres Helsinki

    Standard time zone: UTC/GMT +2 hours - as of Nov

    http://www.timeanddate.com/worldclock/city.html?n=101

    Check against countries with British Summer Time or their equivalent.

    0 讨论(0)
  • 2021-02-05 03:53

    Swift 4 extension method that returns timezone offset as a string

    //: Playground - noun: a place where people can play
    
    import Foundation
    
    extension TimeZone {
        func offsetFromGMT() -> String
        {
            let localTimeZoneFormatter = DateFormatter()
            localTimeZoneFormatter.timeZone = self
            localTimeZoneFormatter.dateFormat = "Z"
            return localTimeZoneFormatter.string(from: Date())
        }
    }
    
    // Test: Print all timezones
    TimeZone.knownTimeZoneIdentifiers.forEach({ timeZoneIdentifier in
        if let timezone = TimeZone(identifier: timeZoneIdentifier)
        {
            print("\(timezone.identifier) \(timezone.offsetFromGMT())")
        }
    })
    

    Test Output:

    Africa/Abidjan +0000
    Africa/Accra +0000
    Africa/Addis_Ababa +0300
    Africa/Algiers +0100
    Africa/Asmara +0300
    Africa/Bamako +0000
    Africa/Bangui +0100
    Africa/Banjul +0000
    Africa/Bissau +0000
    Africa/Blantyre +0200
    Africa/Brazzaville +0100
    Africa/Bujumbura +0200
    Africa/Cairo +0200
    Africa/Casablanca +0100
    Africa/Ceuta +0200
    Africa/Conakry +0000
    Africa/Dakar +0000
    Africa/Dar_es_Salaam +0300
    Africa/Djibouti +0300
    Africa/Douala +0100
    Africa/El_Aaiun +0100
    Africa/Freetown +0000
    Africa/Gaborone +0200
    Africa/Harare +0200
    Africa/Johannesburg +0200
    Africa/Juba +0300
    Africa/Kampala +0300
    Africa/Khartoum +0200
    Africa/Kigali +0200
    Africa/Kinshasa +0100
    Africa/Lagos +0100
    Africa/Libreville +0100
    Africa/Lome +0000
    Africa/Luanda +0100
    Africa/Lubumbashi +0200
    Africa/Lusaka +0200
    Africa/Malabo +0100
    Africa/Maputo +0200
    Africa/Maseru +0200
    Africa/Mbabane +0200
    Africa/Mogadishu +0300
    Africa/Monrovia +0000
    Africa/Nairobi +0300
    Africa/Ndjamena +0100
    Africa/Niamey +0100
    Africa/Nouakchott +0000
    Africa/Ouagadougou +0000
    Africa/Porto-Novo +0100
    Africa/Sao_Tome +0100
    Africa/Tripoli +0200
    Africa/Tunis +0100
    Africa/Windhoek +0200
    America/Adak -0900
    America/Anchorage -0800
    America/Anguilla -0400
    America/Antigua -0400
    America/Araguaina -0300
    America/Argentina/Buenos_Aires -0300
    America/Argentina/Catamarca -0300
    America/Argentina/Cordoba -0300
    America/Argentina/Jujuy -0300
    America/Argentina/La_Rioja -0300
    America/Argentina/Mendoza -0300
    America/Argentina/Rio_Gallegos -0300
    America/Argentina/Salta -0300
    America/Argentina/San_Juan -0300
    America/Argentina/San_Luis -0300
    America/Argentina/Tucuman -0300
    America/Argentina/Ushuaia -0300
    America/Aruba -0400
    America/Asuncion -0400
    America/Atikokan -0500
    America/Bahia -0300
    America/Bahia_Banderas -0500
    America/Barbados -0400
    America/Belem -0300
    America/Belize -0600
    America/Blanc-Sablon -0400
    America/Boa_Vista -0400
    America/Bogota -0500
    America/Boise -0600
    America/Cambridge_Bay -0600
    America/Campo_Grande -0400
    America/Cancun -0500
    America/Caracas -0400
    America/Cayenne -0300
    America/Cayman -0500
    America/Chicago -0500
    America/Chihuahua -0600
    America/Costa_Rica -0600
    America/Creston -0700
    America/Cuiaba -0400
    America/Curacao -0400
    America/Danmarkshavn +0000
    America/Dawson -0700
    America/Dawson_Creek -0700
    America/Denver -0600
    America/Detroit -0400
    America/Dominica -0400
    America/Edmonton -0600
    America/Eirunepe -0500
    America/El_Salvador -0600
    America/Fort_Nelson -0700
    America/Fortaleza -0300
    America/Glace_Bay -0300
    America/Godthab -0200
    America/Goose_Bay -0300
    America/Grand_Turk -0400
    America/Grenada -0400
    America/Guadeloupe -0400
    America/Guatemala -0600
    America/Guayaquil -0500
    America/Guyana -0400
    America/Halifax -0300
    America/Havana -0400
    America/Hermosillo -0700
    America/Indiana/Indianapolis -0400
    America/Indiana/Knox -0500
    America/Indiana/Marengo -0400
    America/Indiana/Petersburg -0400
    America/Indiana/Tell_City -0500
    America/Indiana/Vevay -0400
    America/Indiana/Vincennes -0400
    America/Indiana/Winamac -0400
    America/Inuvik -0600
    America/Iqaluit -0400
    America/Jamaica -0500
    America/Juneau -0800
    America/Kentucky/Louisville -0400
    America/Kentucky/Monticello -0400
    America/Kralendijk -0400
    America/La_Paz -0400
    America/Lima -0500
    America/Los_Angeles -0700
    America/Lower_Princes -0400
    America/Maceio -0300
    America/Managua -0600
    America/Manaus -0400
    America/Marigot -0400
    America/Martinique -0400
    America/Matamoros -0500
    America/Mazatlan -0600
    America/Menominee -0500
    America/Merida -0500
    America/Metlakatla -0800
    America/Mexico_City -0500
    America/Miquelon -0200
    America/Moncton -0300
    America/Monterrey -0500
    America/Montevideo -0300
    America/Montreal -0400
    America/Montserrat -0400
    America/Nassau -0400
    America/New_York -0400
    America/Nipigon -0400
    America/Nome -0800
    America/Noronha -0200
    America/North_Dakota/Beulah -0500
    America/North_Dakota/Center -0500
    America/North_Dakota/New_Salem -0500
    America/Ojinaga -0600
    America/Panama -0500
    America/Pangnirtung -0400
    America/Paramaribo -0300
    America/Phoenix -0700
    America/Port-au-Prince -0400
    America/Port_of_Spain -0400
    America/Porto_Velho -0400
    America/Puerto_Rico -0400
    America/Punta_Arenas -0300
    America/Rainy_River -0500
    America/Rankin_Inlet -0500
    America/Recife -0300
    America/Regina -0600
    America/Resolute -0500
    America/Rio_Branco -0500
    America/Santa_Isabel -0700
    America/Santarem -0300
    America/Santiago -0300
    America/Santo_Domingo -0400
    America/Sao_Paulo -0300
    America/Scoresbysund +0000
    America/Shiprock -0600
    America/Sitka -0800
    America/St_Barthelemy -0400
    America/St_Johns -0230
    America/St_Kitts -0400
    America/St_Lucia -0400
    America/St_Thomas -0400
    America/St_Vincent -0400
    America/Swift_Current -0600
    America/Tegucigalpa -0600
    America/Thule -0300
    America/Thunder_Bay -0400
    America/Tijuana -0700
    America/Toronto -0400
    America/Tortola -0400
    America/Vancouver -0700
    America/Whitehorse -0700
    America/Winnipeg -0500
    America/Yakutat -0800
    America/Yellowknife -0600
    Antarctica/Casey +1100
    Antarctica/Davis +0700
    Antarctica/DumontDUrville +1000
    Antarctica/Macquarie +1100
    Antarctica/Mawson +0500
    Antarctica/McMurdo +1200
    Antarctica/Palmer -0300
    Antarctica/Rothera -0300
    Antarctica/South_Pole +1200
    Antarctica/Syowa +0300
    Antarctica/Troll +0200
    Antarctica/Vostok +0600
    Arctic/Longyearbyen +0200
    Asia/Aden +0300
    Asia/Almaty +0600
    Asia/Amman +0300
    Asia/Anadyr +1200
    Asia/Aqtau +0500
    Asia/Aqtobe +0500
    Asia/Ashgabat +0500
    Asia/Atyrau +0500
    Asia/Baghdad +0300
    Asia/Bahrain +0300
    Asia/Baku +0400
    Asia/Bangkok +0700
    Asia/Barnaul +0700
    Asia/Beirut +0300
    Asia/Bishkek +0600
    Asia/Brunei +0800
    Asia/Calcutta +0530
    Asia/Chita +0900
    Asia/Choibalsan +0800
    Asia/Chongqing +0800
    Asia/Colombo +0530
    Asia/Damascus +0300
    Asia/Dhaka +0600
    Asia/Dili +0900
    Asia/Dubai +0400
    Asia/Dushanbe +0500
    Asia/Famagusta +0300
    Asia/Gaza +0300
    Asia/Harbin +0800
    Asia/Hebron +0300
    Asia/Ho_Chi_Minh +0700
    Asia/Hong_Kong +0800
    Asia/Hovd +0700
    Asia/Irkutsk +0800
    Asia/Jakarta +0700
    Asia/Jayapura +0900
    Asia/Jerusalem +0300
    Asia/Kabul +0430
    Asia/Kamchatka +1200
    Asia/Karachi +0500
    Asia/Kashgar +0600
    Asia/Kathmandu +0545
    Asia/Katmandu +0545
    Asia/Khandyga +0900
    Asia/Krasnoyarsk +0700
    Asia/Kuala_Lumpur +0800
    Asia/Kuching +0800
    Asia/Kuwait +0300
    Asia/Macau +0800
    Asia/Magadan +1100
    Asia/Makassar +0800
    Asia/Manila +0800
    Asia/Muscat +0400
    Asia/Nicosia +0300
    Asia/Novokuznetsk +0700
    Asia/Novosibirsk +0700
    Asia/Omsk +0600
    Asia/Oral +0500
    Asia/Phnom_Penh +0700
    Asia/Pontianak +0700
    Asia/Pyongyang +0830
    Asia/Qatar +0300
    Asia/Qyzylorda +0600
    Asia/Rangoon +0630
    Asia/Riyadh +0300
    Asia/Sakhalin +1100
    Asia/Samarkand +0500
    Asia/Seoul +0900
    Asia/Shanghai +0800
    Asia/Singapore +0800
    Asia/Srednekolymsk +1100
    Asia/Taipei +0800
    Asia/Tashkent +0500
    Asia/Tbilisi +0400
    Asia/Tehran +0430
    Asia/Thimphu +0600
    Asia/Tokyo +0900
    Asia/Tomsk +0700
    Asia/Ulaanbaatar +0800
    Asia/Urumqi +0600
    Asia/Ust-Nera +1000
    Asia/Vientiane +0700
    Asia/Vladivostok +1000
    Asia/Yakutsk +0900
    Asia/Yangon +0630
    Asia/Yekaterinburg +0500
    Asia/Yerevan +0400
    Atlantic/Azores +0000
    Atlantic/Bermuda -0300
    Atlantic/Canary +0100
    Atlantic/Cape_Verde -0100
    Atlantic/Faroe +0100
    Atlantic/Madeira +0100
    Atlantic/Reykjavik +0000
    Atlantic/South_Georgia -0200
    Atlantic/St_Helena +0000
    Atlantic/Stanley -0300
    Australia/Adelaide +0930
    Australia/Brisbane +1000
    Australia/Broken_Hill +0930
    Australia/Currie +1000
    Australia/Darwin +0930
    Australia/Eucla +0845
    Australia/Hobart +1000
    Australia/Lindeman +1000
    Australia/Lord_Howe +1030
    Australia/Melbourne +1000
    Australia/Perth +0800
    Australia/Sydney +1000
    Europe/Amsterdam +0200
    Europe/Andorra +0200
    Europe/Astrakhan +0400
    Europe/Athens +0300
    Europe/Belgrade +0200
    Europe/Berlin +0200
    Europe/Bratislava +0200
    Europe/Brussels +0200
    Europe/Bucharest +0300
    Europe/Budapest +0200
    Europe/Busingen +0200
    Europe/Chisinau +0300
    Europe/Copenhagen +0200
    Europe/Dublin +0100
    Europe/Gibraltar +0200
    Europe/Guernsey +0100
    Europe/Helsinki +0300
    Europe/Isle_of_Man +0100
    Europe/Istanbul +0300
    Europe/Jersey +0100
    Europe/Kaliningrad +0200
    Europe/Kiev +0300
    Europe/Kirov +0300
    Europe/Lisbon +0100
    Europe/Ljubljana +0200
    Europe/London +0100
    Europe/Luxembourg +0200
    Europe/Madrid +0200
    Europe/Malta +0200
    Europe/Mariehamn +0300
    Europe/Minsk +0300
    Europe/Monaco +0200
    Europe/Moscow +0300
    Europe/Oslo +0200
    Europe/Paris +0200
    Europe/Podgorica +0200
    Europe/Prague +0200
    Europe/Riga +0300
    Europe/Rome +0200
    Europe/Samara +0400
    Europe/San_Marino +0200
    Europe/Sarajevo +0200
    Europe/Saratov +0400
    Europe/Simferopol +0300
    Europe/Skopje +0200
    Europe/Sofia +0300
    Europe/Stockholm +0200
    Europe/Tallinn +0300
    Europe/Tirane +0200
    Europe/Ulyanovsk +0400
    Europe/Uzhgorod +0300
    Europe/Vaduz +0200
    Europe/Vatican +0200
    Europe/Vienna +0200
    Europe/Vilnius +0300
    Europe/Volgograd +0300
    Europe/Warsaw +0200
    Europe/Zagreb +0200
    Europe/Zaporozhye +0300
    Europe/Zurich +0200
    GMT +0000
    Indian/Antananarivo +0300
    Indian/Chagos +0600
    Indian/Christmas +0700
    Indian/Cocos +0630
    Indian/Comoro +0300
    Indian/Kerguelen +0500
    Indian/Mahe +0400
    Indian/Maldives +0500
    Indian/Mauritius +0400
    Indian/Mayotte +0300
    Indian/Reunion +0400
    Pacific/Apia +1300
    Pacific/Auckland +1200
    Pacific/Bougainville +1100
    Pacific/Chatham +1245
    Pacific/Chuuk +1000
    Pacific/Easter -0500
    Pacific/Efate +1100
    Pacific/Enderbury +1300
    Pacific/Fakaofo +1300
    Pacific/Fiji +1200
    Pacific/Funafuti +1200
    Pacific/Galapagos -0600
    Pacific/Gambier -0900
    Pacific/Guadalcanal +1100
    Pacific/Guam +1000
    Pacific/Honolulu -1000
    Pacific/Johnston -1000
    Pacific/Kiritimati +1400
    Pacific/Kosrae +1100
    Pacific/Kwajalein +1200
    Pacific/Majuro +1200
    Pacific/Marquesas -0930
    Pacific/Midway -1100
    Pacific/Nauru +1200
    Pacific/Niue -1100
    Pacific/Norfolk +1100
    Pacific/Noumea +1100
    Pacific/Pago_Pago -1100
    Pacific/Palau +0900
    Pacific/Pitcairn -0800
    Pacific/Pohnpei +1100
    Pacific/Ponape +1100
    Pacific/Port_Moresby +1000
    Pacific/Rarotonga -1000
    Pacific/Saipan +1000
    Pacific/Tahiti -1000
    Pacific/Tarawa +1200
    Pacific/Tongatapu +1300
    Pacific/Truk +1000
    Pacific/Wake +1200
    Pacific/Wallis +1200
    
    0 讨论(0)
  • 2021-02-05 03:55

    Here's an easy way to do this in Swift 3:

    let offsetInHours = Double(TimeZone.current.secondsFromGMT()) / 3600.0

    0 讨论(0)
提交回复
热议问题