Search code examples
iphoneruby-on-railsobjective-cnstimezone

iPhone NSTimeZone: localTimeZone confusion


From what I understand, calling

NSLog(@"Local Time Zone %@",[[NSTimeZone localTimeZone] name]);

gives you the local time zone of the device. What it's giving me is "US/Central", and I can't find that anywhere in the list of abbreviations in [NSTimeZone abbreviationDictionary], or the list of time zone names in [NSTimeZone knownTimeZoneNames]. Where is this one coming from? I need to pass the current device time zone to a Rails app, and it understands things like "Australia/Sydney" or "America/Chicago", but not "US/Central".

How do I take what localTimeZone is giving me and convert it to a string that Rails can understand (i.e. any time zone in knownTimeZoneNames, which is supposed to be all time zone names that the system knows about?)


Solution

  • You're likely using ActiveSupport::TimeWithZone without having required the tzinfo gem:

    $ irb -rrubygems -ractivesupport -rtzinfo
    >> Time.send(:get_zone, "US/Central").now
    => Tue, 17 Nov 2009 04:27:23 CST -06:00
    

    If you don't require tzinfo, you'll only get a subset of timezones which is fairly useless.

    EDIT: Just to avoid confusion, the reason I used the private Time#get_zone API is because that's what's being used behind the scenes when you call Time#zone=. If you don't require tzinfo, calling Time.send(:get_zone, "US/Central") returns nil.