How can I get ALL locale independent NSTimeZone abbreviations?
我正在尝试获取所有现有时区的缩写。
我发现这段代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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]); } |
我可以缩写所有可用的时区并使用全名,例如:
1 2 3 4 | 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),因此我得到一些结果,例如:
1 2 3 4 | 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?
任何帮助深表感谢 :)
谢谢!
您不会得到这些缩写,因为它们不是标准的。 与大多数计算机制造商一样,Apple使用半官方的IANA时区数据库。 IANA并不将" AEST"视为标准缩写(实际上,他们的
使用名称获取所有区域并获取secondsFromGMT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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 } } } |