iPhone NSTimeZone: localTimeZone confusion

情到浓时终转凉″ 提交于 2019-12-06 09:33:47

问题


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?)


回答1:


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.




回答2:


Are you getting that from within the simulator? If so what does this command returns for if you run it from a terminal?
$ systemsetup -gettimezone

The timezone setting GUI in Mac OS 10.5 would set timezones to US/*. If you manually set it from the console you can set it to one of the expected timezones.

Run this to get a list of the valid, and notice that US/Central isn't among them.
$ systemsetup -listtimezones

Run this to set it to America/Chicago
$ systemsetup -settimezone America/Chicago



来源:https://stackoverflow.com/questions/1736699/iphone-nstimezone-localtimezone-confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!