如何获取所有区域设置独立的NSTimeZone缩写?

时间:2014-03-11 12:39:13

标签: objective-c nsdateformatter nstimezone nslocale

我试图获取所有现有时区的缩写。 [NSTimeZone abbreviationDictionary]只返回其中一些,但有些像AEST(来自澳大利亚/布里斯班)没有出现。

我发现这个代码

NSDate *myDate = [NSDate date];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateStyle:NSDateFormatterLongStyle];
[f setTimeStyle:NSDateFormatterLongStyle];
[f setDateFormat:@"zzzz"];

NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
for (NSString *name1 in timeZoneNames)
{
    NSTimeZone *tz = [NSTimeZone timeZoneWithName:name1];
    [f setTimeZone:tz];

    DLog(@"%@ = \"%@\" = %@", [tz abbreviation], name1, [f stringFromDate:myDate]);
}

我可以获得所有可用的时区缩写和全名,例如:

EET = "Europe/Kiev" = Eastern European Standard Time
WET = "Europe/Lisbon" = Western European Standard Time
CET = "Europe/Ljubljana" = Central European Standard Time
GMT = "Europe/London" = Greenwich Mean Time

但是时区名称依赖于语言环境(我当前的[[NSLocale currentLocale] localeIdentifier]是en_GB),所以我得到了一些结果:

GMT-4 = "America/New_York" = Eastern Daylight Time
GMT-4 = "America/Nipigon" = Eastern Daylight Time
GMT-8 = "America/Nome" = Alaska Daylight Time
GMT-2 = "America/Noronha" = Fernando de Noronha Standard Time

有没有办法可以获得所有现有的时区缩写而不是GMT-X?

非常感谢任何帮助:)

谢谢!

2 个答案:

答案 0 :(得分:1)

使用名称获取所有区域并获取secondsFromGMT:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {

        for(NSString *name in [NSTimeZone knownTimeZoneNames]) {
            NSTimeZone *z = [NSTimeZone timeZoneWithName:name];
            NSLog(@"%@", z.name); //name

            NSLog(@"GMT-%d", (z.secondsFromGMT/60)/60); //GMT-%D%

            NSLog(@"%@", z.abbreviation); //abbreviation
        }
    }
}

答案 1 :(得分:1)

你没有得到这些缩写,因为它们不是标准的。与大多数计算机制造商一样,Apple使用半官方IANA time zone database。 IANA不认为“AEST”是标准缩写(实际上他们的australasia文件包含对此确切问题的重要讨论),因此它不包含在iOS的时区数据中。