문제

I'm trying to get ALL existing timezone's abbreviations. [NSTimeZone abbreviationDictionary] only returns some of them, but some like AEST (from Australia/Brisbane) do no appear.

I found out that with this code

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]);
}

I can get all available timezone abbreviated and full names such as:

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

But the timezone names are locale-dependent (my current [[NSLocale currentLocale]localeIdentifier] is en_GB), so I get some results like:

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

Is there a way I can get all the existing timezone abbreviations instead of GMT-X?

Any help is much appreciated :)

Thank you!

도움이 되었습니까?

해결책

You don't get those abbreviations, because they're not standard. Apple, like most computer makers, uses the semi-official IANA time zone database. IANA does not consider "AEST" to be a standard abbreviation (in fact their australasia file includes significant discussion on this exact question), so it's not included in iOS's time zone data.

다른 팁

use the names to get all zones and to get the 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
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top