How to use NSTimeZone -timeZoneWithName: with a city name from Rails ActiveSupport?

Hlung picture Hlung · Apr 30, 2012 · Viewed 26.5k times · Source

If I only have the city name like Bangkok or Tokyo, how can I supply a timezone parameter in [NSTimeZone timeZoneWithName:@"Asia/Tokyo"] where it also has continent and slash in front of city?

I already tried [NSTimeZone timeZoneWithName:@"Tokyo"], it doesn't work.

Answer

Hlung picture Hlung · May 2, 2012

Thanks for the answers guys, but it looks like those city names are just a format that Rails "ActiveSupport::TimeZone" uses. So I just have to map it back. The mapping is here -> http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

It maps the timezone from Rails "ActiveSupport::TimeZone" format to another format that iOS uses (e.g. "International Date Line West" => "Pacific/Midway", "Midway Island" => "Pacific/Midway", "Samoa" => "Pacific/Pago_Pago", ... ).

I have created a plist file containing a NSDictionary property that can easily be used for mapping.

Edit: an updated version (with usage example) for Rails 3.2 (thanks RJ Regenold!)