4

I found out that iOS framework doesn’t understand proper time zone names, like "Eastern Standard Time”. If you type:

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

… you get:

EST: (null)

Instead, you have to use time ID:s (a narrower area), like "America/New_York" for example:

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

... then the console kindly responds:

EST: America/New_York (GMT-4) offset -14400 (Daylight)

Is there any way to translate time zone names to time ID:s? Because the web-API I’m using is giving me time zone names, not the time ID:s.

turingtested
  • 6,356
  • 7
  • 32
  • 47
  • Is there a full list of the names you need to map from? The `America/New_York` format is the only standard I'm familiar with. – Dan Loewenherz Aug 08 '14 at 22:23
  • I need to map every time zone name known to man. I'm making an app that handle positions and time over the whole earth. I think that Windows uses time zones names like "Eastern Standard Time", so I guess that the API is Window-based somehow. – turingtested Aug 08 '14 at 22:34

2 Answers2

9

This is how I did it. (The stupid solution, but it works.) I used the Windows/TZID timezone mapping at unicode.org and made a method around a big dictionary.

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

To use it, just type:

NSString *tzidTimezoneName = [self translateFromWindowsTimezone: someWindowsTimezoneName];

If you use this code, please make a comment about errors or missing timezones if you find any!

turingtested
  • 6,356
  • 7
  • 32
  • 47
0

No standard I know will use what you call "proper time zone names". Those are locale dependent, change between summer and winter etc. That being said this code will do what you describe:

- (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;
}
Gerd K
  • 2,933
  • 1
  • 20
  • 23
  • Not working! the response I get from NSLog(@"EST: %@", [self timeZoneForLongName:@"Eastern Standard Time"]); is EST: America/Atikokan, which is wrong. – turingtested Aug 09 '14 at 07:50
  • However, I found this site that maps Windows time zone names to TZID (which Xcode is using): http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html – turingtested Aug 09 '14 at 07:57
  • "EST: America/Atikokan" is NOT wrong, it is just one of MANY correct answers. Using "Eastern Standard Time" as input and having any expectation to the output being New York (instead of any of the many other cities being governed by EST) is just flawed. BTW Xcode is an IDE. – Gerd K Aug 09 '14 at 15:58
  • Yes, I'm aware of that. But if you write NSLog(@"Seconds from GMT: %i", [[NSTimeZone timeZoneWithName:@"America/New_York"]secondsFromGMT]); …you get: Seconds from GMT: -14400 … in the console And: NSLog(@"Seconds from GMT: %i", [[NSTimeZone timeZoneWithName:@"America/Atikokan"]secondsFromGMT]); …you get: Seconds from GMT: -18000 … not the same. – turingtested Aug 11 '14 at 08:12