Bogdan Laukhin Bogdan Laukhin - 6 months ago 36
Objective-C Question

How to get GMT value for each timezone from knownTimeZoneNames

I know how to get the list of known timezones:

[NSTimeZone knownTimeZoneNames]
.

But this method returns only time zone names (like "Africa/Abidjan").

How can I get GMT value (like
GMT+3
or
GMT-7
)for each of known time zone from that array?

Answer

In general, create an NSTimeZone instance for each time zone name and then get the secondsFromGMT property from each instance:

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

The only outstanding issue would be converting from seconds to hours or hours/minutes, which shouldn't be too difficult. Perhaps the default [NSTimeZone description] would return the correct information?

Comments