如何在uitableview中获取这种类型的时区列表?
How to get this type of timezone list in uitableview?
我正在处理一个应用程序,我必须在 tableview
中显示这种类型的 timezone
列表。
我已经试过了:-
NSMutableArray *timeZoneArray = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init];
for (int count = 0; count < [timeZoneArray count] - 1; count = count + 1) {
NSString *timeZoneStr = [timeZoneArray objectAtIndex:count];
[abbreviationArray addObject: [timeZoneStr stringByAppendingFormat:@"(%@)",[[NSTimeZone timeZoneWithName: timeZoneStr] abbreviation]]];
}
NSLog(@"Abbreviation : %@",abbreviationArray);
我得到了这种类型的列表
但我想要这种类型的列表
请帮帮我
NSMutableArray *timeZoneArray = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init];
for (int count = 0; count < [timeZoneArray count] - 1; count = count + 1) {
NSString *timeZoneStr = [timeZoneArray objectAtIndex:count];
[abbreviationArray addObject: [timeZoneStr stringByAppendingFormat:@"(%@)",[[NSTimeZone timeZoneWithName: timeZoneStr] abbreviation]]];
}
NSLog(@"Abbreviation : %@",abbreviationArray);
参考Link:
Web service - current time zone for a city?
iOS 框架中没有任何方法可以直接为您提供此类列表。
这两个选项可能是:
你必须维护一个plist。 (但它会随着白天的变化而失败)。
您的 API 提供商向您提供此列表。
或者你直接用这个作为 API geoname.
更多info
另一种方式google earth tools
Google API documentation
NSTimeZone
class会提供很多你需要的,这就是Apple documentation,或者阅读Xcode.
class 方法 + knownTimeZoneNames
将提供系统的已知时区名称列表 - 您会在该列表中看到类似 "America/New_York" 的名称。
使用这些名称,您可以为每个名称创建一个 NSTimeZone
实例。
这些实例有一个 属性 secondsFromGMT
,它将为您提供格式化 GMT 偏移量所需的数据。
有一种方法 localizedName:locale:
可以为您提供比 "America/New_York" 更漂亮的多种样式的时区名称。如果 none 这些样式适合您,那么您必须为名称编写自己的漂亮打印机。
以上将为您提供一个 NSTimeZone
实例数组,您可以使用 NSComparator
块对其进行排序(请参阅 NSArray
documentation)。您有双键排序,主键是 GMT 偏移量,次键是名称(使用本地化比较)。
将以上内容放在一起,您就有了区域列表。
HTH
试试这个,它返回的结果类似于
"(GMT+00:00)Africa/Abidjan"
NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate: myDate];
NSMutableString *mu = [NSMutableString stringWithString:dateString];
[mu insertString:@":" atIndex:3];
NSString *strResult = [NSString stringWithFormat:@"(GMT%@)%@",mu,obj];
[arrResult addObject:strResult];
}];
NSLog(@"%@", arrResult);
试试这个代码:
NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate: myDate];
NSMutableString *mu = [NSMutableString stringWithString:dateString];
[mu insertString:@":" atIndex:3];
NSString *strResult = [NSString stringWithFormat:@"(GMT%@)%@",mu,obj];
[arrResult addObject:strResult];
}];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[arrResult sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]];
NSMutableArray *sortArr = [[NSMutableArray alloc] init];
for (int i = 0 ; i < arr.count; i++) {
NSString *str = [arr objectAtIndex:i];
NSRange range = [str rangeOfString:@"+"];
if (range.location == NSNotFound)
{
[sortArr addObject:str];
}
}
for (int i = arr.count -1 ; i >= 0; i--) {
NSString *str = [arr objectAtIndex:i];
NSRange range = [str rangeOfString:@"+"];
if (range.location != NSNotFound)
{
[sortArr addObject:str];
}
}
NSLog(@"%@",sortArr);
输出:
2016-05-26 12:13:26.480 test[2574:69176] (
"(GMT-11:00)Pacific/Pago_Pago",
"(GMT-11:00)Pacific/Niue",
"(GMT-11:00)Pacific/Midway",
"(GMT-10:00)Pacific/Tahiti",
"(GMT-10:00)Pacific/Rarotonga",
"(GMT-10:00)Pacific/Johnston",
"(GMT-10:00)Pacific/Honolulu",
"(GMT-09:30)Pacific/Marquesas",
"(GMT-09:00)Pacific/Gambier",
"(GMT-09:00)America/Adak",
"(GMT-08:00)Pacific/Pitcairn",
"(GMT-08:00)America/Yakutat",
"(GMT-08:00)America/Sitka",
"(GMT-08:00)America/Nome",
"(GMT-08:00)America/Metlakatla",
"(GMT-08:00)America/Juneau",
"(GMT-08:00)America/Anchorage",
"(GMT-07:00)America/Whitehorse",
"(GMT-07:00)America/Vancouver",
"(GMT-07:00)America/Tijuana",
"(GMT-07:00)America/Santa_Isabel",
"(GMT-07:00)America/Phoenix",
"(GMT-07:00)America/Los_Angeles",
"(GMT-07:00)America/Hermosillo",
"(GMT-07:00)America/Fort_Nelson",
"(GMT-07:00)America/Dawson_Creek",
"(GMT-07:00)America/Dawson",
"(GMT-07:00)America/Creston",
"(GMT-06:00)Pacific/Galapagos",
"(GMT-06:00)America/Yellowknife",
"(GMT-06:00)America/Tegucigalpa",
"(GMT-06:00)America/Swift_Current",
"(GMT-06:00)America/Shiprock",
"(GMT-06:00)America/Regina",
"(GMT-06:00)America/Ojinaga",
"(GMT-06:00)America/Mazatlan",
"(GMT-06:00)America/Managua",
"(GMT-06:00)America/Inuvik",
"(GMT-06:00)America/Guatemala",
"(GMT-06:00)America/El_Salvador",
"(GMT-06:00)America/Edmonton",
"(GMT-06:00)America/Denver",
"(GMT-06:00)America/Costa_Rica",
"(GMT-06:00)America/Chihuahua",
"(GMT-06:00)America/Cambridge_Bay",
"(GMT-06:00)America/Boise",
"(GMT-06:00)America/Belize",
"(GMT-05:00)Pacific/Easter",
"(GMT-05:00)America/Winnipeg",
"(GMT-05:00)America/Rio_Branco",
"(GMT-05:00)America/Resolute",
"(GMT-05:00)America/Rankin_Inlet",
"(GMT-05:00)America/Rainy_River",
"(GMT-05:00)America/Panama",
"(GMT-05:00)America/North_Dakota/New_Salem",
"(GMT-05:00)America/North_Dakota/Center",
"(GMT-05:00)America/North_Dakota/Beulah",
"(GMT-05:00)America/Monterrey",
"(GMT-05:00)America/Mexico_City",
"(GMT-05:00)America/Merida",
"(GMT-05:00)America/Menominee",
"(GMT-05:00)America/Matamoros",
"(GMT-05:00)America/Lima",
"(GMT-05:00)America/Jamaica",
"(GMT-05:00)America/Indiana/Tell_City",
"(GMT-05:00)America/Indiana/Knox",
"(GMT-05:00)America/Guayaquil",
"(GMT-05:00)America/Eirunepe",
"(GMT-05:00)America/Chicago",
"(GMT-05:00)America/Cayman",
"(GMT-05:00)America/Cancun",
"(GMT-05:00)America/Bogota",
"(GMT-05:00)America/Bahia_Banderas",
"(GMT-05:00)America/Atikokan",
"(GMT-04:30)America/Caracas",
"(GMT-04:00)America/Tortola",
"(GMT-04:00)America/Toronto",
"(GMT-04:00)America/Thunder_Bay",
"(GMT-04:00)America/St_Vincent",
"(GMT-04:00)America/St_Thomas",
"(GMT-04:00)America/St_Lucia",
"(GMT-04:00)America/St_Kitts",
"(GMT-04:00)America/St_Barthelemy",
"(GMT-04:00)America/Santo_Domingo",
"(GMT-04:00)America/Puerto_Rico",
"(GMT-04:00)America/Porto_Velho",
"(GMT-04:00)America/Port_of_Spain",
"(GMT-04:00)America/Port-au-Prince",
"(GMT-04:00)America/Pangnirtung",
"(GMT-04:00)America/Nipigon",
"(GMT-04:00)America/New_York",
"(GMT-04:00)America/Nassau",
"(GMT-04:00)America/Montserrat",
"(GMT-04:00)America/Montreal",
"(GMT-04:00)America/Martinique",
"(GMT-04:00)America/Marigot",
"(GMT-04:00)America/Manaus",
"(GMT-04:00)America/Lower_Princes",
"(GMT-04:00)America/La_Paz",
"(GMT-04:00)America/Kralendijk",
"(GMT-04:00)America/Kentucky/Monticello",
"(GMT-04:00)America/Kentucky/Louisville",
"(GMT-04:00)America/Iqaluit",
"(GMT-04:00)America/Indiana/Winamac",
"(GMT-04:00)America/Indiana/Vincennes",
"(GMT-04:00)America/Indiana/Vevay",
"(GMT-04:00)America/Indiana/Petersburg",
"(GMT-04:00)America/Indiana/Marengo",
"(GMT-04:00)America/Indiana/Indianapolis",
"(GMT-04:00)America/Havana",
"(GMT-04:00)America/Guyana",
"(GMT-04:00)America/Guadeloupe",
"(GMT-04:00)America/Grenada",
"(GMT-04:00)America/Grand_Turk",
"(GMT-04:00)America/Dominica",
"(GMT-04:00)America/Detroit",
"(GMT-04:00)America/Curacao",
"(GMT-04:00)America/Cuiaba",
"(GMT-04:00)America/Campo_Grande",
"(GMT-04:00)America/Boa_Vista",
"(GMT-04:00)America/Blanc-Sablon",
"(GMT-04:00)America/Barbados",
"(GMT-04:00)America/Asuncion",
"(GMT-04:00)America/Aruba",
"(GMT-04:00)America/Antigua",
"(GMT-04:00)America/Anguilla",
"(GMT-03:00)Atlantic/Stanley",
"(GMT-03:00)Atlantic/Bermuda",
"(GMT-03:00)Antarctica/Rothera",
"(GMT-03:00)Antarctica/Palmer",
"(GMT-03:00)America/Thule",
"(GMT-03:00)America/Sao_Paulo",
"(GMT-03:00)America/Santiago",
"(GMT-03:00)America/Santarem",
"(GMT-03:00)America/Recife",
"(GMT-03:00)America/Paramaribo",
"(GMT-03:00)America/Montevideo",
"(GMT-03:00)America/Moncton",
"(GMT-03:00)America/Maceio",
"(GMT-03:00)America/Halifax",
"(GMT-03:00)America/Goose_Bay",
"(GMT-03:00)America/Glace_Bay",
"(GMT-03:00)America/Fortaleza",
"(GMT-03:00)America/Cayenne",
"(GMT-03:00)America/Belem",
"(GMT-03:00)America/Bahia",
"(GMT-03:00)America/Argentina/Ushuaia",
"(GMT-03:00)America/Argentina/Tucuman",
"(GMT-03:00)America/Argentina/San_Luis",
"(GMT-03:00)America/Argentina/San_Juan",
"(GMT-03:00)America/Argentina/Salta",
"(GMT-03:00)America/Argentina/Rio_Gallegos",
"(GMT-03:00)America/Argentina/Mendoza",
"(GMT-03:00)America/Argentina/La_Rioja",
"(GMT-03:00)America/Argentina/Jujuy",
"(GMT-03:00)America/Argentina/Cordoba",
"(GMT-03:00)America/Argentina/Catamarca",
"(GMT-03:00)America/Argentina/Buenos_Aires",
"(GMT-03:00)America/Araguaina",
"(GMT-02:30)America/St_Johns",
"(GMT-02:00)Atlantic/South_Georgia",
"(GMT-02:00)America/Noronha",
"(GMT-02:00)America/Miquelon",
"(GMT-02:00)America/Godthab",
"(GMT-01:00)Atlantic/Cape_Verde",
"(GMT+00:00)Africa/Abidjan",
"(GMT+00:00)Africa/Accra",
"(GMT+00:00)Africa/Bamako",
"(GMT+00:00)Africa/Banjul",
"(GMT+00:00)Africa/Bissau",
"(GMT+00:00)Africa/Conakry",
"(GMT+00:00)Africa/Dakar",
"(GMT+00:00)Africa/Freetown",
"(GMT+00:00)Africa/Lome",
"(GMT+00:00)Africa/Monrovia",
"(GMT+00:00)Africa/Nouakchott",
"(GMT+00:00)Africa/Ouagadougou",
"(GMT+00:00)Africa/Sao_Tome",
"(GMT+00:00)America/Danmarkshavn",
"(GMT+00:00)America/Scoresbysund",
"(GMT+00:00)Atlantic/Azores",
"(GMT+00:00)Atlantic/Reykjavik",
"(GMT+00:00)Atlantic/St_Helena",
"(GMT+00:00)GMT",
"(GMT+01:00)Africa/Algiers",
"(GMT+01:00)Africa/Bangui",
"(GMT+01:00)Africa/Brazzaville",
"(GMT+01:00)Africa/Casablanca",
"(GMT+01:00)Africa/Douala",
"(GMT+01:00)Africa/El_Aaiun",
"(GMT+01:00)Africa/Kinshasa",
"(GMT+01:00)Africa/Lagos",
"(GMT+01:00)Africa/Libreville",
"(GMT+01:00)Africa/Luanda",
"(GMT+01:00)Africa/Malabo",
"(GMT+01:00)Africa/Ndjamena",
"(GMT+01:00)Africa/Niamey",
"(GMT+01:00)Africa/Porto-Novo",
"(GMT+01:00)Africa/Tunis",
"(GMT+01:00)Africa/Windhoek",
"(GMT+01:00)Atlantic/Canary",
"(GMT+01:00)Atlantic/Faroe",
"(GMT+01:00)Atlantic/Madeira",
"(GMT+01:00)Europe/Dublin",
"(GMT+01:00)Europe/Guernsey",
"(GMT+01:00)Europe/Isle_of_Man",
"(GMT+01:00)Europe/Jersey",
"(GMT+01:00)Europe/Lisbon",
"(GMT+01:00)Europe/London",
"(GMT+02:00)Africa/Blantyre",
"(GMT+02:00)Africa/Bujumbura",
"(GMT+02:00)Africa/Cairo",
"(GMT+02:00)Africa/Ceuta",
"(GMT+02:00)Africa/Gaborone",
"(GMT+02:00)Africa/Harare",
"(GMT+02:00)Africa/Johannesburg",
"(GMT+02:00)Africa/Kigali",
"(GMT+02:00)Africa/Lubumbashi",
"(GMT+02:00)Africa/Lusaka",
"(GMT+02:00)Africa/Maputo",
"(GMT+02:00)Africa/Maseru",
"(GMT+02:00)Africa/Mbabane",
"(GMT+02:00)Africa/Tripoli",
"(GMT+02:00)Antarctica/Troll",
"(GMT+02:00)Arctic/Longyearbyen",
"(GMT+02:00)Europe/Amsterdam",
"(GMT+02:00)Europe/Andorra",
"(GMT+02:00)Europe/Belgrade",
"(GMT+02:00)Europe/Berlin",
"(GMT+02:00)Europe/Bratislava",
"(GMT+02:00)Europe/Brussels",
"(GMT+02:00)Europe/Budapest",
"(GMT+02:00)Europe/Busingen",
"(GMT+02:00)Europe/Copenhagen",
"(GMT+02:00)Europe/Gibraltar",
"(GMT+02:00)Europe/Kaliningrad",
"(GMT+02:00)Europe/Ljubljana",
"(GMT+02:00)Europe/Luxembourg",
"(GMT+02:00)Europe/Madrid",
"(GMT+02:00)Europe/Malta",
"(GMT+02:00)Europe/Monaco",
"(GMT+02:00)Europe/Oslo",
"(GMT+02:00)Europe/Paris",
"(GMT+02:00)Europe/Podgorica",
"(GMT+02:00)Europe/Prague",
"(GMT+02:00)Europe/Rome",
"(GMT+02:00)Europe/San_Marino",
"(GMT+02:00)Europe/Sarajevo",
"(GMT+02:00)Europe/Skopje",
"(GMT+02:00)Europe/Stockholm",
"(GMT+02:00)Europe/Tirane",
"(GMT+02:00)Europe/Vaduz",
"(GMT+02:00)Europe/Vatican",
"(GMT+02:00)Europe/Vienna",
"(GMT+02:00)Europe/Warsaw",
"(GMT+02:00)Europe/Zagreb",
"(GMT+02:00)Europe/Zurich",
"(GMT+03:00)Africa/Addis_Ababa",
"(GMT+03:00)Africa/Asmara",
"(GMT+03:00)Africa/Dar_es_Salaam",
"(GMT+03:00)Africa/Djibouti",
"(GMT+03:00)Africa/Juba",
"(GMT+03:00)Africa/Kampala",
"(GMT+03:00)Africa/Khartoum",
"(GMT+03:00)Africa/Mogadishu",
"(GMT+03:00)Africa/Nairobi",
"(GMT+03:00)Antarctica/Syowa",
"(GMT+03:00)Asia/Aden",
"(GMT+03:00)Asia/Amman",
"(GMT+03:00)Asia/Baghdad",
"(GMT+03:00)Asia/Bahrain",
"(GMT+03:00)Asia/Beirut",
"(GMT+03:00)Asia/Damascus",
"(GMT+03:00)Asia/Gaza",
"(GMT+03:00)Asia/Hebron",
"(GMT+03:00)Asia/Jerusalem",
"(GMT+03:00)Asia/Kuwait",
"(GMT+03:00)Asia/Nicosia",
"(GMT+03:00)Asia/Qatar",
"(GMT+03:00)Asia/Riyadh",
"(GMT+03:00)Europe/Athens",
"(GMT+03:00)Europe/Bucharest",
"(GMT+03:00)Europe/Chisinau",
"(GMT+03:00)Europe/Helsinki",
"(GMT+03:00)Europe/Istanbul",
"(GMT+03:00)Europe/Kiev",
"(GMT+03:00)Europe/Mariehamn",
"(GMT+03:00)Europe/Minsk",
"(GMT+03:00)Europe/Moscow",
"(GMT+03:00)Europe/Riga",
"(GMT+03:00)Europe/Simferopol",
"(GMT+03:00)Europe/Sofia",
"(GMT+03:00)Europe/Tallinn",
"(GMT+03:00)Europe/Uzhgorod",
"(GMT+03:00)Europe/Vilnius",
"(GMT+03:00)Europe/Volgograd",
"(GMT+03:00)Europe/Zaporozhye",
"(GMT+03:00)Indian/Antananarivo",
"(GMT+03:00)Indian/Comoro",
"(GMT+03:00)Indian/Mayotte",
"(GMT+04:00)Asia/Dubai",
"(GMT+04:00)Asia/Muscat",
"(GMT+04:00)Asia/Tbilisi",
"(GMT+04:00)Asia/Yerevan",
"(GMT+04:00)Europe/Samara",
"(GMT+04:00)Indian/Mahe",
"(GMT+04:00)Indian/Mauritius",
"(GMT+04:00)Indian/Reunion",
"(GMT+04:30)Asia/Kabul",
"(GMT+04:30)Asia/Tehran",
"(GMT+05:00)Antarctica/Mawson",
"(GMT+05:00)Asia/Aqtau",
"(GMT+05:00)Asia/Aqtobe",
"(GMT+05:00)Asia/Ashgabat",
"(GMT+05:00)Asia/Baku",
"(GMT+05:00)Asia/Dushanbe",
"(GMT+05:00)Asia/Karachi",
"(GMT+05:00)Asia/Oral",
"(GMT+05:00)Asia/Samarkand",
"(GMT+05:00)Asia/Tashkent",
"(GMT+05:00)Asia/Yekaterinburg",
"(GMT+05:00)Indian/Kerguelen",
"(GMT+05:00)Indian/Maldives",
"(GMT+05:30)Asia/Colombo",
"(GMT+05:30)Asia/Kolkata",
"(GMT+05:45)Asia/Kathmandu",
"(GMT+05:45)Asia/Katmandu",
"(GMT+06:00)Antarctica/Vostok",
"(GMT+06:00)Asia/Almaty",
"(GMT+06:00)Asia/Bishkek",
"(GMT+06:00)Asia/Dhaka",
"(GMT+06:00)Asia/Kashgar",
"(GMT+06:00)Asia/Novosibirsk",
"(GMT+06:00)Asia/Omsk",
"(GMT+06:00)Asia/Qyzylorda",
"(GMT+06:00)Asia/Thimphu",
"(GMT+06:00)Asia/Urumqi",
"(GMT+06:00)Indian/Chagos",
"(GMT+06:30)Asia/Rangoon",
"(GMT+06:30)Indian/Cocos",
"(GMT+07:00)Antarctica/Davis",
"(GMT+07:00)Asia/Bangkok",
"(GMT+07:00)Asia/Ho_Chi_Minh",
"(GMT+07:00)Asia/Jakarta",
"(GMT+07:00)Asia/Krasnoyarsk",
"(GMT+07:00)Asia/Novokuznetsk",
"(GMT+07:00)Asia/Phnom_Penh",
"(GMT+07:00)Asia/Pontianak",
"(GMT+07:00)Asia/Vientiane",
"(GMT+07:00)Indian/Christmas",
"(GMT+08:00)Antarctica/Casey",
"(GMT+08:00)Asia/Brunei",
"(GMT+08:00)Asia/Chongqing",
"(GMT+08:00)Asia/Harbin",
"(GMT+08:00)Asia/Hong_Kong",
"(GMT+08:00)Asia/Hovd",
"(GMT+08:00)Asia/Irkutsk",
"(GMT+08:00)Asia/Kuala_Lumpur",
"(GMT+08:00)Asia/Kuching",
"(GMT+08:00)Asia/Macau",
"(GMT+08:00)Asia/Makassar",
"(GMT+08:00)Asia/Manila",
"(GMT+08:00)Asia/Shanghai",
"(GMT+08:00)Asia/Singapore",
"(GMT+08:00)Asia/Taipei",
"(GMT+08:00)Australia/Perth",
"(GMT+08:30)Asia/Pyongyang",
"(GMT+08:45)Australia/Eucla",
"(GMT+09:00)Asia/Chita",
"(GMT+09:00)Asia/Choibalsan",
"(GMT+09:00)Asia/Dili",
"(GMT+09:00)Asia/Jayapura",
"(GMT+09:00)Asia/Khandyga",
"(GMT+09:00)Asia/Seoul",
"(GMT+09:00)Asia/Tokyo",
"(GMT+09:00)Asia/Ulaanbaatar",
"(GMT+09:00)Asia/Yakutsk",
"(GMT+09:00)Pacific/Palau",
"(GMT+09:30)Australia/Adelaide",
"(GMT+09:30)Australia/Broken_Hill",
"(GMT+09:30)Australia/Darwin",
"(GMT+10:00)Antarctica/DumontDUrville",
"(GMT+10:00)Asia/Magadan",
"(GMT+10:00)Asia/Sakhalin",
"(GMT+10:00)Asia/Ust-Nera",
"(GMT+10:00)Asia/Vladivostok",
"(GMT+10:00)Australia/Brisbane",
"(GMT+10:00)Australia/Currie",
"(GMT+10:00)Australia/Hobart",
"(GMT+10:00)Australia/Lindeman",
"(GMT+10:00)Australia/Melbourne",
"(GMT+10:00)Australia/Sydney",
"(GMT+10:00)Pacific/Chuuk",
"(GMT+10:00)Pacific/Guam",
"(GMT+10:00)Pacific/Port_Moresby",
"(GMT+10:00)Pacific/Saipan",
"(GMT+10:00)Pacific/Truk",
"(GMT+10:30)Australia/Lord_Howe",
"(GMT+11:00)Antarctica/Macquarie",
"(GMT+11:00)Asia/Srednekolymsk",
"(GMT+11:00)Pacific/Bougainville",
"(GMT+11:00)Pacific/Efate",
"(GMT+11:00)Pacific/Guadalcanal",
"(GMT+11:00)Pacific/Kosrae",
"(GMT+11:00)Pacific/Norfolk",
"(GMT+11:00)Pacific/Noumea",
"(GMT+11:00)Pacific/Pohnpei",
"(GMT+11:00)Pacific/Ponape",
"(GMT+12:00)Antarctica/McMurdo",
"(GMT+12:00)Antarctica/South_Pole",
"(GMT+12:00)Asia/Anadyr",
"(GMT+12:00)Asia/Kamchatka",
"(GMT+12:00)Pacific/Auckland",
"(GMT+12:00)Pacific/Fiji",
"(GMT+12:00)Pacific/Funafuti",
"(GMT+12:00)Pacific/Kwajalein",
"(GMT+12:00)Pacific/Majuro",
"(GMT+12:00)Pacific/Nauru",
"(GMT+12:00)Pacific/Tarawa",
"(GMT+12:00)Pacific/Wake",
"(GMT+12:00)Pacific/Wallis",
"(GMT+12:45)Pacific/Chatham",
"(GMT+13:00)Pacific/Apia",
"(GMT+13:00)Pacific/Enderbury",
"(GMT+13:00)Pacific/Fakaofo",
"(GMT+13:00)Pacific/Tongatapu",
"(GMT+14:00)Pacific/Kiritimati"
)
首先获取所有可用时区列表,并从 GMT 获取它们各自的时间间隔。将它们添加到字典中并对字典进行排序并打印它们:
-(void) printTimeZones
{
NSLog(@"\n-------Time Zone Test Started-----------");
NSMutableDictionary *timeZoneValuesDict = [[NSMutableDictionary alloc] init];
// getting all timezones dict
NSDictionary *dict = [NSTimeZone abbreviationDictionary];
for (NSString *key in [dict allKeys])
{
// getting (gmt_diff + values) in a dict
NSTimeZone *timeZK = [NSTimeZone timeZoneWithAbbreviation:key];
if(timeZK)
{
NSInteger secFromGmt = [timeZK secondsFromGMT];
NSNumber *number = [NSNumber numberWithInteger:secFromGmt];
// getting gmt time and values in a timeZoneValuesDict
[timeZoneValuesDict setObject:number forKey:key];
}
}
NSLog(@"\n--------Available Time Zones----------");
// sorting the timeZoneValuesDict
[timeZoneValuesDict keysSortedByValueUsingComparator:^NSComparisonResult(NSNumber *number1, NSNumber *number2) {
NSInteger num1 = [number1 integerValue];
NSInteger num2 = [number2 integerValue];
if(num1 > num2)
return NSOrderedAscending;
else if(num1 < num2)
return NSOrderedDescending;
return NSOrderedSame;
}];
// printing the time zones
for (NSString *key in [timeZoneValuesDict allKeys])
{
NSNumber *number = [timeZoneValuesDict valueForKey:key];
NSInteger secFromGmt = [number integerValue];
NSString *value = [dict valueForKey:key];
NSString *timeZoneStr = [self convertToStringSeconds:secFromGmt andTimeZoneKey:value];
NSLog(@"%@", timeZoneStr);
}
NSLog(@"\n-------Time Zone Test End-----------");
}
-(NSString *) convertToStringSeconds:(NSInteger)timeInseconds andTimeZoneKey:(NSString *) key {
// converting time
bool isTimeZoneNegative = FALSE;
if(timeInseconds < 0) {
isTimeZoneNegative = TRUE;
timeInseconds = -1 * timeInseconds; // converting to positive value
}
NSInteger time_ = timeInseconds;
// convert seconds to hours and minutes
int seconds = time_ % 60;
time_ /= 60;
int minutes = time_ % 60;
time_ /= 60;
int hours = time_ % 24;
time_ /= 24;
NSString *strToRet = @"(GMT ";
if(isTimeZoneNegative)
strToRet = [strToRet stringByAppendingString:@"-"];
else
strToRet = [strToRet stringByAppendingString:@" "];
if(hours <= 9)
strToRet = [strToRet stringByAppendingFormat:@"0%d",hours];
else
strToRet = [strToRet stringByAppendingFormat:@"%d",hours];
if(minutes <= 9)
strToRet = [strToRet stringByAppendingFormat:@":0%d",minutes];
else
strToRet = [strToRet stringByAppendingFormat:@":%d",minutes];
strToRet = [strToRet stringByAppendingString:@")"];
// adding time zone name
NSRange range = [key rangeOfString:@"/"];
if(range.location != NSNotFound)
{
NSString *tzName = [key substringFromIndex:(range.location + 1)];
strToRet = [strToRet stringByAppendingFormat:@" %@",tzName];
}
return strToRet;
}
输出:
--------时区测试开始------------
--------可用时区----------
(格林威治标准时间-04:00)New_York
(格林威治标准时间 00:00)
(格林威治标准时间 -03:00) 哈利法克斯
(格林威治标准时间 07:00) 曼谷
(格林威治标准时间 -05:00) 利马
(格林威治标准时间 -03:00) 圣地亚哥
(格林威治标准时间 09:00) 首尔
(格林威治标准时间-07:00)Los_Angeles
(格林威治标准时间 -08:00) 朱诺
(格林威治标准时间 -05:00) 芝加哥
(格林威治标准时间 -08:00) 朱诺
(格林威治标准时间-03:00)Sao_Paulo
(格林威治标准时间 07:00) 雅加达
(格林威治标准时间02:00)巴黎
(格林威治标准时间 -06:00) 丹佛
(格林威治标准时间 08:00) Hong_Kong
(格林威治标准时间 05:30) 加尔各答
(格林威治标准时间 03:00) Addis_Ababa
(格林威治标准时间 -03:00) 哈利法克斯
(格林威治标准时间 04:30) 德黑兰
(格林威治标准时间01:00)伦敦
(格林威治标准时间-03:00)Argentina/Buenos_Aires
(格林威治标准时间 02:00) 哈拉雷
(格林威治标准时间 04:00) 迪拜
(格林威治标准时间-07:00)Los_Angeles
(格林威治标准时间12:00)奥克兰
(格林威治标准时间 01:00) 里斯本
(格林威治标准时间 -05:00) 波哥大
(格林威治标准时间12:00)奥克兰
(格林威治标准时间 05:00) 卡拉奇
(格林威治标准时间 03:00) 伊斯坦布尔
(格林威治标准时间 03:00) 伊斯坦布尔
(格林威治标准时间 08:00) 新加坡
(格林威治标准时间 00:00)
(格林威治标准时间 01:00) 拉各斯
(格林威治标准时间-04:00)New_York
(格林威治标准时间 09:00) 东京
(格林威治标准时间02:00)巴黎
(格林威治标准时间 06:00) 达卡
(格林威治标准时间 03:00) 莫斯科
(格林威治标准时间 -03:00) 圣地亚哥
(格林威治标准时间 08:00) 马尼拉
(格林威治标准时间 -05:00) 芝加哥
(格林威治标准时间-03:00)Sao_Paulo
(GMT -10:00) 火奴鲁鲁
(格林威治标准时间 03:00) 莫斯科
(格林威治标准时间 -06:00) 丹佛
(格林威治标准时间 01:00) 里斯本
--------时区测试结束------------
斯威夫特5
TimeZone.knownTimeZoneIdentifiers
.map { TimeZone(identifier: [=10=]) }
.compactMap{ [=10=] }.map { timeZone -> String in
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.dateFormat = "VVV"
formatter.timeZone = timeZone
formatter.locale = Locale(identifier: "zh")
return "(\(timeZone.abbreviation() ?? "")) \(formatter.string(from: Date()))"
}.forEach {
print([=10=])
}
输出
(GMT) 阿比让
(GMT) 阿克拉
(GMT+3) 亚的斯亚贝巴
(GMT+1) 阿尔及尔
(GMT+3) 阿斯马拉
(GMT) 巴马科
(GMT+1) 班吉
我正在处理一个应用程序,我必须在 tableview
中显示这种类型的 timezone
列表。
我已经试过了:-
NSMutableArray *timeZoneArray = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init];
for (int count = 0; count < [timeZoneArray count] - 1; count = count + 1) {
NSString *timeZoneStr = [timeZoneArray objectAtIndex:count];
[abbreviationArray addObject: [timeZoneStr stringByAppendingFormat:@"(%@)",[[NSTimeZone timeZoneWithName: timeZoneStr] abbreviation]]];
}
NSLog(@"Abbreviation : %@",abbreviationArray);
我得到了这种类型的列表
但我想要这种类型的列表
请帮帮我
NSMutableArray *timeZoneArray = [[NSMutableArray alloc] initWithArray:[NSTimeZone knownTimeZoneNames]];
NSMutableArray *abbreviationArray = [[NSMutableArray alloc] init];
for (int count = 0; count < [timeZoneArray count] - 1; count = count + 1) {
NSString *timeZoneStr = [timeZoneArray objectAtIndex:count];
[abbreviationArray addObject: [timeZoneStr stringByAppendingFormat:@"(%@)",[[NSTimeZone timeZoneWithName: timeZoneStr] abbreviation]]];
}
NSLog(@"Abbreviation : %@",abbreviationArray);
参考Link:
Web service - current time zone for a city?
iOS 框架中没有任何方法可以直接为您提供此类列表。
这两个选项可能是: 你必须维护一个plist。 (但它会随着白天的变化而失败)。
您的 API 提供商向您提供此列表。
或者你直接用这个作为 API geoname.
更多info
另一种方式google earth tools
Google API documentation
NSTimeZone
class会提供很多你需要的,这就是Apple documentation,或者阅读Xcode.
class 方法 + knownTimeZoneNames
将提供系统的已知时区名称列表 - 您会在该列表中看到类似 "America/New_York" 的名称。
使用这些名称,您可以为每个名称创建一个 NSTimeZone
实例。
这些实例有一个 属性 secondsFromGMT
,它将为您提供格式化 GMT 偏移量所需的数据。
有一种方法 localizedName:locale:
可以为您提供比 "America/New_York" 更漂亮的多种样式的时区名称。如果 none 这些样式适合您,那么您必须为名称编写自己的漂亮打印机。
以上将为您提供一个 NSTimeZone
实例数组,您可以使用 NSComparator
块对其进行排序(请参阅 NSArray
documentation)。您有双键排序,主键是 GMT 偏移量,次键是名称(使用本地化比较)。
将以上内容放在一起,您就有了区域列表。
HTH
试试这个,它返回的结果类似于
"(GMT+00:00)Africa/Abidjan"
NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate: myDate];
NSMutableString *mu = [NSMutableString stringWithString:dateString];
[mu insertString:@":" atIndex:3];
NSString *strResult = [NSString stringWithFormat:@"(GMT%@)%@",mu,obj];
[arrResult addObject:strResult];
}];
NSLog(@"%@", arrResult);
试试这个代码:
NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:@"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate: myDate];
NSMutableString *mu = [NSMutableString stringWithString:dateString];
[mu insertString:@":" atIndex:3];
NSString *strResult = [NSString stringWithFormat:@"(GMT%@)%@",mu,obj];
[arrResult addObject:strResult];
}];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[arrResult sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]];
NSMutableArray *sortArr = [[NSMutableArray alloc] init];
for (int i = 0 ; i < arr.count; i++) {
NSString *str = [arr objectAtIndex:i];
NSRange range = [str rangeOfString:@"+"];
if (range.location == NSNotFound)
{
[sortArr addObject:str];
}
}
for (int i = arr.count -1 ; i >= 0; i--) {
NSString *str = [arr objectAtIndex:i];
NSRange range = [str rangeOfString:@"+"];
if (range.location != NSNotFound)
{
[sortArr addObject:str];
}
}
NSLog(@"%@",sortArr);
输出:
2016-05-26 12:13:26.480 test[2574:69176] (
"(GMT-11:00)Pacific/Pago_Pago",
"(GMT-11:00)Pacific/Niue",
"(GMT-11:00)Pacific/Midway",
"(GMT-10:00)Pacific/Tahiti",
"(GMT-10:00)Pacific/Rarotonga",
"(GMT-10:00)Pacific/Johnston",
"(GMT-10:00)Pacific/Honolulu",
"(GMT-09:30)Pacific/Marquesas",
"(GMT-09:00)Pacific/Gambier",
"(GMT-09:00)America/Adak",
"(GMT-08:00)Pacific/Pitcairn",
"(GMT-08:00)America/Yakutat",
"(GMT-08:00)America/Sitka",
"(GMT-08:00)America/Nome",
"(GMT-08:00)America/Metlakatla",
"(GMT-08:00)America/Juneau",
"(GMT-08:00)America/Anchorage",
"(GMT-07:00)America/Whitehorse",
"(GMT-07:00)America/Vancouver",
"(GMT-07:00)America/Tijuana",
"(GMT-07:00)America/Santa_Isabel",
"(GMT-07:00)America/Phoenix",
"(GMT-07:00)America/Los_Angeles",
"(GMT-07:00)America/Hermosillo",
"(GMT-07:00)America/Fort_Nelson",
"(GMT-07:00)America/Dawson_Creek",
"(GMT-07:00)America/Dawson",
"(GMT-07:00)America/Creston",
"(GMT-06:00)Pacific/Galapagos",
"(GMT-06:00)America/Yellowknife",
"(GMT-06:00)America/Tegucigalpa",
"(GMT-06:00)America/Swift_Current",
"(GMT-06:00)America/Shiprock",
"(GMT-06:00)America/Regina",
"(GMT-06:00)America/Ojinaga",
"(GMT-06:00)America/Mazatlan",
"(GMT-06:00)America/Managua",
"(GMT-06:00)America/Inuvik",
"(GMT-06:00)America/Guatemala",
"(GMT-06:00)America/El_Salvador",
"(GMT-06:00)America/Edmonton",
"(GMT-06:00)America/Denver",
"(GMT-06:00)America/Costa_Rica",
"(GMT-06:00)America/Chihuahua",
"(GMT-06:00)America/Cambridge_Bay",
"(GMT-06:00)America/Boise",
"(GMT-06:00)America/Belize",
"(GMT-05:00)Pacific/Easter",
"(GMT-05:00)America/Winnipeg",
"(GMT-05:00)America/Rio_Branco",
"(GMT-05:00)America/Resolute",
"(GMT-05:00)America/Rankin_Inlet",
"(GMT-05:00)America/Rainy_River",
"(GMT-05:00)America/Panama",
"(GMT-05:00)America/North_Dakota/New_Salem",
"(GMT-05:00)America/North_Dakota/Center",
"(GMT-05:00)America/North_Dakota/Beulah",
"(GMT-05:00)America/Monterrey",
"(GMT-05:00)America/Mexico_City",
"(GMT-05:00)America/Merida",
"(GMT-05:00)America/Menominee",
"(GMT-05:00)America/Matamoros",
"(GMT-05:00)America/Lima",
"(GMT-05:00)America/Jamaica",
"(GMT-05:00)America/Indiana/Tell_City",
"(GMT-05:00)America/Indiana/Knox",
"(GMT-05:00)America/Guayaquil",
"(GMT-05:00)America/Eirunepe",
"(GMT-05:00)America/Chicago",
"(GMT-05:00)America/Cayman",
"(GMT-05:00)America/Cancun",
"(GMT-05:00)America/Bogota",
"(GMT-05:00)America/Bahia_Banderas",
"(GMT-05:00)America/Atikokan",
"(GMT-04:30)America/Caracas",
"(GMT-04:00)America/Tortola",
"(GMT-04:00)America/Toronto",
"(GMT-04:00)America/Thunder_Bay",
"(GMT-04:00)America/St_Vincent",
"(GMT-04:00)America/St_Thomas",
"(GMT-04:00)America/St_Lucia",
"(GMT-04:00)America/St_Kitts",
"(GMT-04:00)America/St_Barthelemy",
"(GMT-04:00)America/Santo_Domingo",
"(GMT-04:00)America/Puerto_Rico",
"(GMT-04:00)America/Porto_Velho",
"(GMT-04:00)America/Port_of_Spain",
"(GMT-04:00)America/Port-au-Prince",
"(GMT-04:00)America/Pangnirtung",
"(GMT-04:00)America/Nipigon",
"(GMT-04:00)America/New_York",
"(GMT-04:00)America/Nassau",
"(GMT-04:00)America/Montserrat",
"(GMT-04:00)America/Montreal",
"(GMT-04:00)America/Martinique",
"(GMT-04:00)America/Marigot",
"(GMT-04:00)America/Manaus",
"(GMT-04:00)America/Lower_Princes",
"(GMT-04:00)America/La_Paz",
"(GMT-04:00)America/Kralendijk",
"(GMT-04:00)America/Kentucky/Monticello",
"(GMT-04:00)America/Kentucky/Louisville",
"(GMT-04:00)America/Iqaluit",
"(GMT-04:00)America/Indiana/Winamac",
"(GMT-04:00)America/Indiana/Vincennes",
"(GMT-04:00)America/Indiana/Vevay",
"(GMT-04:00)America/Indiana/Petersburg",
"(GMT-04:00)America/Indiana/Marengo",
"(GMT-04:00)America/Indiana/Indianapolis",
"(GMT-04:00)America/Havana",
"(GMT-04:00)America/Guyana",
"(GMT-04:00)America/Guadeloupe",
"(GMT-04:00)America/Grenada",
"(GMT-04:00)America/Grand_Turk",
"(GMT-04:00)America/Dominica",
"(GMT-04:00)America/Detroit",
"(GMT-04:00)America/Curacao",
"(GMT-04:00)America/Cuiaba",
"(GMT-04:00)America/Campo_Grande",
"(GMT-04:00)America/Boa_Vista",
"(GMT-04:00)America/Blanc-Sablon",
"(GMT-04:00)America/Barbados",
"(GMT-04:00)America/Asuncion",
"(GMT-04:00)America/Aruba",
"(GMT-04:00)America/Antigua",
"(GMT-04:00)America/Anguilla",
"(GMT-03:00)Atlantic/Stanley",
"(GMT-03:00)Atlantic/Bermuda",
"(GMT-03:00)Antarctica/Rothera",
"(GMT-03:00)Antarctica/Palmer",
"(GMT-03:00)America/Thule",
"(GMT-03:00)America/Sao_Paulo",
"(GMT-03:00)America/Santiago",
"(GMT-03:00)America/Santarem",
"(GMT-03:00)America/Recife",
"(GMT-03:00)America/Paramaribo",
"(GMT-03:00)America/Montevideo",
"(GMT-03:00)America/Moncton",
"(GMT-03:00)America/Maceio",
"(GMT-03:00)America/Halifax",
"(GMT-03:00)America/Goose_Bay",
"(GMT-03:00)America/Glace_Bay",
"(GMT-03:00)America/Fortaleza",
"(GMT-03:00)America/Cayenne",
"(GMT-03:00)America/Belem",
"(GMT-03:00)America/Bahia",
"(GMT-03:00)America/Argentina/Ushuaia",
"(GMT-03:00)America/Argentina/Tucuman",
"(GMT-03:00)America/Argentina/San_Luis",
"(GMT-03:00)America/Argentina/San_Juan",
"(GMT-03:00)America/Argentina/Salta",
"(GMT-03:00)America/Argentina/Rio_Gallegos",
"(GMT-03:00)America/Argentina/Mendoza",
"(GMT-03:00)America/Argentina/La_Rioja",
"(GMT-03:00)America/Argentina/Jujuy",
"(GMT-03:00)America/Argentina/Cordoba",
"(GMT-03:00)America/Argentina/Catamarca",
"(GMT-03:00)America/Argentina/Buenos_Aires",
"(GMT-03:00)America/Araguaina",
"(GMT-02:30)America/St_Johns",
"(GMT-02:00)Atlantic/South_Georgia",
"(GMT-02:00)America/Noronha",
"(GMT-02:00)America/Miquelon",
"(GMT-02:00)America/Godthab",
"(GMT-01:00)Atlantic/Cape_Verde",
"(GMT+00:00)Africa/Abidjan",
"(GMT+00:00)Africa/Accra",
"(GMT+00:00)Africa/Bamako",
"(GMT+00:00)Africa/Banjul",
"(GMT+00:00)Africa/Bissau",
"(GMT+00:00)Africa/Conakry",
"(GMT+00:00)Africa/Dakar",
"(GMT+00:00)Africa/Freetown",
"(GMT+00:00)Africa/Lome",
"(GMT+00:00)Africa/Monrovia",
"(GMT+00:00)Africa/Nouakchott",
"(GMT+00:00)Africa/Ouagadougou",
"(GMT+00:00)Africa/Sao_Tome",
"(GMT+00:00)America/Danmarkshavn",
"(GMT+00:00)America/Scoresbysund",
"(GMT+00:00)Atlantic/Azores",
"(GMT+00:00)Atlantic/Reykjavik",
"(GMT+00:00)Atlantic/St_Helena",
"(GMT+00:00)GMT",
"(GMT+01:00)Africa/Algiers",
"(GMT+01:00)Africa/Bangui",
"(GMT+01:00)Africa/Brazzaville",
"(GMT+01:00)Africa/Casablanca",
"(GMT+01:00)Africa/Douala",
"(GMT+01:00)Africa/El_Aaiun",
"(GMT+01:00)Africa/Kinshasa",
"(GMT+01:00)Africa/Lagos",
"(GMT+01:00)Africa/Libreville",
"(GMT+01:00)Africa/Luanda",
"(GMT+01:00)Africa/Malabo",
"(GMT+01:00)Africa/Ndjamena",
"(GMT+01:00)Africa/Niamey",
"(GMT+01:00)Africa/Porto-Novo",
"(GMT+01:00)Africa/Tunis",
"(GMT+01:00)Africa/Windhoek",
"(GMT+01:00)Atlantic/Canary",
"(GMT+01:00)Atlantic/Faroe",
"(GMT+01:00)Atlantic/Madeira",
"(GMT+01:00)Europe/Dublin",
"(GMT+01:00)Europe/Guernsey",
"(GMT+01:00)Europe/Isle_of_Man",
"(GMT+01:00)Europe/Jersey",
"(GMT+01:00)Europe/Lisbon",
"(GMT+01:00)Europe/London",
"(GMT+02:00)Africa/Blantyre",
"(GMT+02:00)Africa/Bujumbura",
"(GMT+02:00)Africa/Cairo",
"(GMT+02:00)Africa/Ceuta",
"(GMT+02:00)Africa/Gaborone",
"(GMT+02:00)Africa/Harare",
"(GMT+02:00)Africa/Johannesburg",
"(GMT+02:00)Africa/Kigali",
"(GMT+02:00)Africa/Lubumbashi",
"(GMT+02:00)Africa/Lusaka",
"(GMT+02:00)Africa/Maputo",
"(GMT+02:00)Africa/Maseru",
"(GMT+02:00)Africa/Mbabane",
"(GMT+02:00)Africa/Tripoli",
"(GMT+02:00)Antarctica/Troll",
"(GMT+02:00)Arctic/Longyearbyen",
"(GMT+02:00)Europe/Amsterdam",
"(GMT+02:00)Europe/Andorra",
"(GMT+02:00)Europe/Belgrade",
"(GMT+02:00)Europe/Berlin",
"(GMT+02:00)Europe/Bratislava",
"(GMT+02:00)Europe/Brussels",
"(GMT+02:00)Europe/Budapest",
"(GMT+02:00)Europe/Busingen",
"(GMT+02:00)Europe/Copenhagen",
"(GMT+02:00)Europe/Gibraltar",
"(GMT+02:00)Europe/Kaliningrad",
"(GMT+02:00)Europe/Ljubljana",
"(GMT+02:00)Europe/Luxembourg",
"(GMT+02:00)Europe/Madrid",
"(GMT+02:00)Europe/Malta",
"(GMT+02:00)Europe/Monaco",
"(GMT+02:00)Europe/Oslo",
"(GMT+02:00)Europe/Paris",
"(GMT+02:00)Europe/Podgorica",
"(GMT+02:00)Europe/Prague",
"(GMT+02:00)Europe/Rome",
"(GMT+02:00)Europe/San_Marino",
"(GMT+02:00)Europe/Sarajevo",
"(GMT+02:00)Europe/Skopje",
"(GMT+02:00)Europe/Stockholm",
"(GMT+02:00)Europe/Tirane",
"(GMT+02:00)Europe/Vaduz",
"(GMT+02:00)Europe/Vatican",
"(GMT+02:00)Europe/Vienna",
"(GMT+02:00)Europe/Warsaw",
"(GMT+02:00)Europe/Zagreb",
"(GMT+02:00)Europe/Zurich",
"(GMT+03:00)Africa/Addis_Ababa",
"(GMT+03:00)Africa/Asmara",
"(GMT+03:00)Africa/Dar_es_Salaam",
"(GMT+03:00)Africa/Djibouti",
"(GMT+03:00)Africa/Juba",
"(GMT+03:00)Africa/Kampala",
"(GMT+03:00)Africa/Khartoum",
"(GMT+03:00)Africa/Mogadishu",
"(GMT+03:00)Africa/Nairobi",
"(GMT+03:00)Antarctica/Syowa",
"(GMT+03:00)Asia/Aden",
"(GMT+03:00)Asia/Amman",
"(GMT+03:00)Asia/Baghdad",
"(GMT+03:00)Asia/Bahrain",
"(GMT+03:00)Asia/Beirut",
"(GMT+03:00)Asia/Damascus",
"(GMT+03:00)Asia/Gaza",
"(GMT+03:00)Asia/Hebron",
"(GMT+03:00)Asia/Jerusalem",
"(GMT+03:00)Asia/Kuwait",
"(GMT+03:00)Asia/Nicosia",
"(GMT+03:00)Asia/Qatar",
"(GMT+03:00)Asia/Riyadh",
"(GMT+03:00)Europe/Athens",
"(GMT+03:00)Europe/Bucharest",
"(GMT+03:00)Europe/Chisinau",
"(GMT+03:00)Europe/Helsinki",
"(GMT+03:00)Europe/Istanbul",
"(GMT+03:00)Europe/Kiev",
"(GMT+03:00)Europe/Mariehamn",
"(GMT+03:00)Europe/Minsk",
"(GMT+03:00)Europe/Moscow",
"(GMT+03:00)Europe/Riga",
"(GMT+03:00)Europe/Simferopol",
"(GMT+03:00)Europe/Sofia",
"(GMT+03:00)Europe/Tallinn",
"(GMT+03:00)Europe/Uzhgorod",
"(GMT+03:00)Europe/Vilnius",
"(GMT+03:00)Europe/Volgograd",
"(GMT+03:00)Europe/Zaporozhye",
"(GMT+03:00)Indian/Antananarivo",
"(GMT+03:00)Indian/Comoro",
"(GMT+03:00)Indian/Mayotte",
"(GMT+04:00)Asia/Dubai",
"(GMT+04:00)Asia/Muscat",
"(GMT+04:00)Asia/Tbilisi",
"(GMT+04:00)Asia/Yerevan",
"(GMT+04:00)Europe/Samara",
"(GMT+04:00)Indian/Mahe",
"(GMT+04:00)Indian/Mauritius",
"(GMT+04:00)Indian/Reunion",
"(GMT+04:30)Asia/Kabul",
"(GMT+04:30)Asia/Tehran",
"(GMT+05:00)Antarctica/Mawson",
"(GMT+05:00)Asia/Aqtau",
"(GMT+05:00)Asia/Aqtobe",
"(GMT+05:00)Asia/Ashgabat",
"(GMT+05:00)Asia/Baku",
"(GMT+05:00)Asia/Dushanbe",
"(GMT+05:00)Asia/Karachi",
"(GMT+05:00)Asia/Oral",
"(GMT+05:00)Asia/Samarkand",
"(GMT+05:00)Asia/Tashkent",
"(GMT+05:00)Asia/Yekaterinburg",
"(GMT+05:00)Indian/Kerguelen",
"(GMT+05:00)Indian/Maldives",
"(GMT+05:30)Asia/Colombo",
"(GMT+05:30)Asia/Kolkata",
"(GMT+05:45)Asia/Kathmandu",
"(GMT+05:45)Asia/Katmandu",
"(GMT+06:00)Antarctica/Vostok",
"(GMT+06:00)Asia/Almaty",
"(GMT+06:00)Asia/Bishkek",
"(GMT+06:00)Asia/Dhaka",
"(GMT+06:00)Asia/Kashgar",
"(GMT+06:00)Asia/Novosibirsk",
"(GMT+06:00)Asia/Omsk",
"(GMT+06:00)Asia/Qyzylorda",
"(GMT+06:00)Asia/Thimphu",
"(GMT+06:00)Asia/Urumqi",
"(GMT+06:00)Indian/Chagos",
"(GMT+06:30)Asia/Rangoon",
"(GMT+06:30)Indian/Cocos",
"(GMT+07:00)Antarctica/Davis",
"(GMT+07:00)Asia/Bangkok",
"(GMT+07:00)Asia/Ho_Chi_Minh",
"(GMT+07:00)Asia/Jakarta",
"(GMT+07:00)Asia/Krasnoyarsk",
"(GMT+07:00)Asia/Novokuznetsk",
"(GMT+07:00)Asia/Phnom_Penh",
"(GMT+07:00)Asia/Pontianak",
"(GMT+07:00)Asia/Vientiane",
"(GMT+07:00)Indian/Christmas",
"(GMT+08:00)Antarctica/Casey",
"(GMT+08:00)Asia/Brunei",
"(GMT+08:00)Asia/Chongqing",
"(GMT+08:00)Asia/Harbin",
"(GMT+08:00)Asia/Hong_Kong",
"(GMT+08:00)Asia/Hovd",
"(GMT+08:00)Asia/Irkutsk",
"(GMT+08:00)Asia/Kuala_Lumpur",
"(GMT+08:00)Asia/Kuching",
"(GMT+08:00)Asia/Macau",
"(GMT+08:00)Asia/Makassar",
"(GMT+08:00)Asia/Manila",
"(GMT+08:00)Asia/Shanghai",
"(GMT+08:00)Asia/Singapore",
"(GMT+08:00)Asia/Taipei",
"(GMT+08:00)Australia/Perth",
"(GMT+08:30)Asia/Pyongyang",
"(GMT+08:45)Australia/Eucla",
"(GMT+09:00)Asia/Chita",
"(GMT+09:00)Asia/Choibalsan",
"(GMT+09:00)Asia/Dili",
"(GMT+09:00)Asia/Jayapura",
"(GMT+09:00)Asia/Khandyga",
"(GMT+09:00)Asia/Seoul",
"(GMT+09:00)Asia/Tokyo",
"(GMT+09:00)Asia/Ulaanbaatar",
"(GMT+09:00)Asia/Yakutsk",
"(GMT+09:00)Pacific/Palau",
"(GMT+09:30)Australia/Adelaide",
"(GMT+09:30)Australia/Broken_Hill",
"(GMT+09:30)Australia/Darwin",
"(GMT+10:00)Antarctica/DumontDUrville",
"(GMT+10:00)Asia/Magadan",
"(GMT+10:00)Asia/Sakhalin",
"(GMT+10:00)Asia/Ust-Nera",
"(GMT+10:00)Asia/Vladivostok",
"(GMT+10:00)Australia/Brisbane",
"(GMT+10:00)Australia/Currie",
"(GMT+10:00)Australia/Hobart",
"(GMT+10:00)Australia/Lindeman",
"(GMT+10:00)Australia/Melbourne",
"(GMT+10:00)Australia/Sydney",
"(GMT+10:00)Pacific/Chuuk",
"(GMT+10:00)Pacific/Guam",
"(GMT+10:00)Pacific/Port_Moresby",
"(GMT+10:00)Pacific/Saipan",
"(GMT+10:00)Pacific/Truk",
"(GMT+10:30)Australia/Lord_Howe",
"(GMT+11:00)Antarctica/Macquarie",
"(GMT+11:00)Asia/Srednekolymsk",
"(GMT+11:00)Pacific/Bougainville",
"(GMT+11:00)Pacific/Efate",
"(GMT+11:00)Pacific/Guadalcanal",
"(GMT+11:00)Pacific/Kosrae",
"(GMT+11:00)Pacific/Norfolk",
"(GMT+11:00)Pacific/Noumea",
"(GMT+11:00)Pacific/Pohnpei",
"(GMT+11:00)Pacific/Ponape",
"(GMT+12:00)Antarctica/McMurdo",
"(GMT+12:00)Antarctica/South_Pole",
"(GMT+12:00)Asia/Anadyr",
"(GMT+12:00)Asia/Kamchatka",
"(GMT+12:00)Pacific/Auckland",
"(GMT+12:00)Pacific/Fiji",
"(GMT+12:00)Pacific/Funafuti",
"(GMT+12:00)Pacific/Kwajalein",
"(GMT+12:00)Pacific/Majuro",
"(GMT+12:00)Pacific/Nauru",
"(GMT+12:00)Pacific/Tarawa",
"(GMT+12:00)Pacific/Wake",
"(GMT+12:00)Pacific/Wallis",
"(GMT+12:45)Pacific/Chatham",
"(GMT+13:00)Pacific/Apia",
"(GMT+13:00)Pacific/Enderbury",
"(GMT+13:00)Pacific/Fakaofo",
"(GMT+13:00)Pacific/Tongatapu",
"(GMT+14:00)Pacific/Kiritimati"
)
首先获取所有可用时区列表,并从 GMT 获取它们各自的时间间隔。将它们添加到字典中并对字典进行排序并打印它们:
-(void) printTimeZones
{
NSLog(@"\n-------Time Zone Test Started-----------");
NSMutableDictionary *timeZoneValuesDict = [[NSMutableDictionary alloc] init];
// getting all timezones dict
NSDictionary *dict = [NSTimeZone abbreviationDictionary];
for (NSString *key in [dict allKeys])
{
// getting (gmt_diff + values) in a dict
NSTimeZone *timeZK = [NSTimeZone timeZoneWithAbbreviation:key];
if(timeZK)
{
NSInteger secFromGmt = [timeZK secondsFromGMT];
NSNumber *number = [NSNumber numberWithInteger:secFromGmt];
// getting gmt time and values in a timeZoneValuesDict
[timeZoneValuesDict setObject:number forKey:key];
}
}
NSLog(@"\n--------Available Time Zones----------");
// sorting the timeZoneValuesDict
[timeZoneValuesDict keysSortedByValueUsingComparator:^NSComparisonResult(NSNumber *number1, NSNumber *number2) {
NSInteger num1 = [number1 integerValue];
NSInteger num2 = [number2 integerValue];
if(num1 > num2)
return NSOrderedAscending;
else if(num1 < num2)
return NSOrderedDescending;
return NSOrderedSame;
}];
// printing the time zones
for (NSString *key in [timeZoneValuesDict allKeys])
{
NSNumber *number = [timeZoneValuesDict valueForKey:key];
NSInteger secFromGmt = [number integerValue];
NSString *value = [dict valueForKey:key];
NSString *timeZoneStr = [self convertToStringSeconds:secFromGmt andTimeZoneKey:value];
NSLog(@"%@", timeZoneStr);
}
NSLog(@"\n-------Time Zone Test End-----------");
}
-(NSString *) convertToStringSeconds:(NSInteger)timeInseconds andTimeZoneKey:(NSString *) key {
// converting time
bool isTimeZoneNegative = FALSE;
if(timeInseconds < 0) {
isTimeZoneNegative = TRUE;
timeInseconds = -1 * timeInseconds; // converting to positive value
}
NSInteger time_ = timeInseconds;
// convert seconds to hours and minutes
int seconds = time_ % 60;
time_ /= 60;
int minutes = time_ % 60;
time_ /= 60;
int hours = time_ % 24;
time_ /= 24;
NSString *strToRet = @"(GMT ";
if(isTimeZoneNegative)
strToRet = [strToRet stringByAppendingString:@"-"];
else
strToRet = [strToRet stringByAppendingString:@" "];
if(hours <= 9)
strToRet = [strToRet stringByAppendingFormat:@"0%d",hours];
else
strToRet = [strToRet stringByAppendingFormat:@"%d",hours];
if(minutes <= 9)
strToRet = [strToRet stringByAppendingFormat:@":0%d",minutes];
else
strToRet = [strToRet stringByAppendingFormat:@":%d",minutes];
strToRet = [strToRet stringByAppendingString:@")"];
// adding time zone name
NSRange range = [key rangeOfString:@"/"];
if(range.location != NSNotFound)
{
NSString *tzName = [key substringFromIndex:(range.location + 1)];
strToRet = [strToRet stringByAppendingFormat:@" %@",tzName];
}
return strToRet;
}
输出:
--------时区测试开始------------
--------可用时区----------
(格林威治标准时间-04:00)New_York
(格林威治标准时间 00:00)
(格林威治标准时间 -03:00) 哈利法克斯
(格林威治标准时间 07:00) 曼谷
(格林威治标准时间 -05:00) 利马
(格林威治标准时间 -03:00) 圣地亚哥
(格林威治标准时间 09:00) 首尔
(格林威治标准时间-07:00)Los_Angeles
(格林威治标准时间 -08:00) 朱诺
(格林威治标准时间 -05:00) 芝加哥
(格林威治标准时间 -08:00) 朱诺
(格林威治标准时间-03:00)Sao_Paulo
(格林威治标准时间 07:00) 雅加达
(格林威治标准时间02:00)巴黎
(格林威治标准时间 -06:00) 丹佛
(格林威治标准时间 08:00) Hong_Kong
(格林威治标准时间 05:30) 加尔各答
(格林威治标准时间 03:00) Addis_Ababa
(格林威治标准时间 -03:00) 哈利法克斯
(格林威治标准时间 04:30) 德黑兰
(格林威治标准时间01:00)伦敦
(格林威治标准时间-03:00)Argentina/Buenos_Aires
(格林威治标准时间 02:00) 哈拉雷
(格林威治标准时间 04:00) 迪拜
(格林威治标准时间-07:00)Los_Angeles
(格林威治标准时间12:00)奥克兰
(格林威治标准时间 01:00) 里斯本
(格林威治标准时间 -05:00) 波哥大
(格林威治标准时间12:00)奥克兰
(格林威治标准时间 05:00) 卡拉奇
(格林威治标准时间 03:00) 伊斯坦布尔
(格林威治标准时间 03:00) 伊斯坦布尔
(格林威治标准时间 08:00) 新加坡 (格林威治标准时间 00:00) (格林威治标准时间 01:00) 拉各斯
(格林威治标准时间-04:00)New_York
(格林威治标准时间 09:00) 东京
(格林威治标准时间02:00)巴黎
(格林威治标准时间 06:00) 达卡
(格林威治标准时间 03:00) 莫斯科
(格林威治标准时间 -03:00) 圣地亚哥
(格林威治标准时间 08:00) 马尼拉
(格林威治标准时间 -05:00) 芝加哥
(格林威治标准时间-03:00)Sao_Paulo
(GMT -10:00) 火奴鲁鲁
(格林威治标准时间 03:00) 莫斯科
(格林威治标准时间 -06:00) 丹佛
(格林威治标准时间 01:00) 里斯本
--------时区测试结束------------
斯威夫特5
TimeZone.knownTimeZoneIdentifiers
.map { TimeZone(identifier: [=10=]) }
.compactMap{ [=10=] }.map { timeZone -> String in
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.dateFormat = "VVV"
formatter.timeZone = timeZone
formatter.locale = Locale(identifier: "zh")
return "(\(timeZone.abbreviation() ?? "")) \(formatter.string(from: Date()))"
}.forEach {
print([=10=])
}
输出
(GMT) 阿比让
(GMT) 阿克拉
(GMT+3) 亚的斯亚贝巴
(GMT+1) 阿尔及尔
(GMT+3) 阿斯马拉
(GMT) 巴马科
(GMT+1) 班吉