将时区名称转换为时间ID:s

时间:2014-08-08 21:48:22

标签: ios objective-c nstimezone

我发现iOS框架无法理解正确的时区名称,例如“东部标准时间”。如果您输入:

NSLog(@"EST: %@", [NSTimeZone timeZoneWithName:@"Eastern Standard Time"]);

...你得到:

EST :( null)

相反,您必须使用时间ID:s(较窄的区域),例如“America / New_York”:

NSLog(@"EST: %@", [NSTimeZone timeZoneWithName:@"America/New_York"]);

...然后控制台友好地回应:

EST:America / New_York(GMT-4)偏移-14400(日光)

有没有办法将时区名称转换为时间ID:s?因为我使用的web-API给了我时区名称,而不是时间ID:s。

2 个答案:

答案 0 :(得分:7)

这就是我做的。 (愚蠢的解决方案,但它的工作原理。)我在unicode.org使用了Windows / TZID时区映射,围绕一本大字典制作了一个方法。

- (NSString*) translateFromWindowsTimezone: (NSString*) timezoneName{
    NSDictionary *timezoneDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"Australia/Darwin", @"AUS Central Standard Time",
                                @"Asia/Kabul", @"Afghanistan Standard Time",
                                @"America/Anchorage", @"Alaskan Standard Time",
                                @"Asia/Riyadh", @"Arab Standard Time",
                                @"Asia/Baghdad", @"Arabic Standard Time",
                                @"America/Buenos_Aires", @"Argentina Standard Time",
                                @"America/Halifax", @"Atlantic Standard Time",
                                @"Asia/Baku", @"Azerbaijan Standard Time",
                                @"Atlantic/Azores",@"Azores Standard Time",
                                @"America/Bahia", @"Bahia Standard Time",
                                @"Asia/Dhaka", @"Bangladesh Standard Time",
                                @"America/Regina", @"Canada Central Standard Time",
                                @"Atlantic/Cape_Verde", @"Cape Verde Standard Time",
                                @"Asia/Yerevan", @"Caucasus Standard Time",
                                @"Australia/Adelaide", @"Cen. Australia Standard Time",
                                @"America/Guatemala", @"Central America Standard Time",
                                @"Asia/Almaty", @"Central Asia Standard Time",
                                @"America/Cuiaba", @"Central Brazilian Standard Time",
                                @"Europe/Budapest", @"Central Europe Standard Time",
                                @"Europe/Warsaw", @"Central European Standard Time",
                                @"Pacific/Guadalcanal", @"Central Pacific Standard Time",
                                @"America/Chicago", @"Central Standard Time",
                                @"America/Mexico_City", @"Central Standard Time (Mexico)",
                                @"Asia/Shanghai", @"China Standard Time",
                                @"Etc/GMT+12", @"Dateline Standard Time",
                                @"Africa/Nairobi", @"E. Africa Standard Time",
                                @"Australia/Brisbane", @"E. Australia Standard Time",
                                @"Asia/Nicosia", @"E. Europe Standard Time",
                                @"America/Sao_Paulo", @"E. South America Standard Time",
                                @"America/New_York", @"Eastern Standard Time",
                                @"Africa/Cairo", @"Egypt Standard Time",
                                @"Asia/Yekaterinburg", @"Ekaterinburg Standard Time",
                                @"Europe/Kiev", @"FLE Standard Time",
                                @"Pacific/Fiji", @"Fiji Standard Time",
                                @"Europe/London", @"GMT Standard Time",
                                @"Europe/Bucharest", @"GTB Standard Time",
                                @"Asia/Tbilisi", @"Georgian Standard Time",
                                @"America/Godthab", @"Greenland Standard Time",
                                @"Atlantic/Reykjavik", @"Greenwich Standard Time",
                                @"Pacific/Honolulu", @"Hawaiian Standard Time",
                                @"Asia/Calcutta", @"India Standard Time",
                                @"Asia/Tehran", @"Iran Standard Time",
                                @"Asia/Jerusalem", @"Israel Standard Time",
                                @"Asia/Amman", @"Jordan Standard Time",
                                @"Europe/Kaliningrad", @"Kaliningrad Standard Time",
                                @"Asia/Seoul", @"Korea Standard Time",
                                @"Indian/Mauritius", @"Mauritius Standard Time",
                                @"Asia/Beirut", @"Middle East Standard Time",
                                @"America/Montevideo", @"Montevideo Standard Time",
                                @"Africa/Casablanca", @"Morocco Standard Time",
                                @"America/Denver", @"Mountain Standard Time",
                                @"America/Chihuahua", @"Mountain Standard Time (Mexico)",
                                @"Asia/Rangoon", @"Myanmar Standard Time",
                                @"Asia/Novosibirsk", @"N. Central Asia Standard Time",
                                @"Africa/Windhoek", @"Namibia Standard Time",
                                @"Asia/Katmandu", @"Nepal Standard Time",
                                @"Pacific/Auckland", @"New Zealand Standard Time",
                                @"America/St_Johns", @"Newfoundland Standard Time",
                                @"Asia/Irkutsk", @"North Asia East Standard Time",
                                @"Asia/Krasnoyarsk", @"North Asia Standard Time",
                                @"America/Santiago", @"Pacific SA Standard Time",
                                @"America/Los_Angeles", @"Pacific Standard Time",
                                @"America/Santa_Isabel", @"Pacific Standard Time (Mexico)",
                                @"Asia/Karachi", @"Pakistan Standard Time",
                                @"America/Asuncion", @"Paraguay Standard Time",
                                @"Europe/Paris", @"Romance Standard Time",
                                @"Europe/Moscow", @"Russian Standard Time",
                                @"America/Cayenne", @"SA Eastern Standard Time",
                                @"America/Bogota", @"SA Pacific Standard Time",
                                @"America/La_Paz", @"SA Western Standard Time",
                                @"Asia/Bangkok", @"SE Asia Standard Time",
                                @"Pacific/Apia", @"Samoa Standard Time",
                                @"Asia/Singapore", @"Singapore Standard Time",
                                @"Africa/Johannesburg", @"South Africa Standard Time",
                                @"Asia/Colombo", @"Sri Lanka Standard Time",
                                @"Asia/Damascus", @"Syria Standard Time",
                                @"Asia/Taipei", @"Taipei Standard Time",
                                @"Australia/Hobart", @"Tasmania Standard Time",
                                @"Asia/Tokyo", @"Tokyo Standard Time",
                                @"Pacific/Tongatapu", @"Tonga Standard Time",
                                @"Europe/Istanbul", @"Turkey Standard Time",
                                @"America/Indianapolis", @"US Eastern Standard Time",
                                @"America/Phoenix", @"US Mountain Standard Time",
                                @"Etc/GMT", @"UTC",
                                @"Etc/GMT-12", @"UTC+12",
                                @"Etc/GMT+2", @"UTC-02",
                                @"Etc/GMT+11", @"UTC-11",
                                @"Asia/Ulaanbaatar", @"Ulaanbaatar Standard Time",
                                @"America/Caracas", @"Venezuela Standard Time",
                                @"Asia/Vladivostok", @"Vladivostok Standard Time",
                                @"Australia/Perth", @"W. Australia Standard Time",
                                @"Africa/Lagos", @"W. Central Africa Standard Time",
                                @"Europe/Berlin", @"W. Europe Standard Time",
                                @"Asia/Tashkent", @"West Asia Standard Time",
                                @"Pacific/Port_Moresby", @"West Pacific Standard Time",
                                @"Asia/Yakutsk", @"Yakutsk Standard Time", nil];
   return [timezoneDictionary objectForKey:timezoneName];
}

要使用它,只需输入:

NSString *tzidTimezoneName = [self translateFromWindowsTimezone: someWindowsTimezoneName];

如果您使用此代码,请在发现错误或缺少时区时发表评论!

答案 1 :(得分:0)

我知道没有标准会使用您所说的"正确的时区名称"。这些是依赖于语言环境的,夏季和冬季之间的变化等。据说这段代码将按照您的描述进行:

- (NSTimeZone *)timeZoneForLongName:(NSString *)longName {

    NSLocale    *locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];

    for(NSString *name in NSTimeZone.knownTimeZoneNames)
    {
        NSTimeZone *tz=[NSTimeZone timeZoneWithName:name];

        if([[tz localizedName:NSTimeZoneNameStyleStandard locale:locale]isEqualToString:longName]
           || [[tz localizedName:NSTimeZoneNameStyleDaylightSaving locale:locale]isEqualToString:longName]
           )
        {
            return tz;
        }
    }

    return nil;
}