2

据我了解,打电话

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

为您提供设备的本地时区。它给我的是"US/Central" ,我在[NSTimeZone abbreviationDictionary]的缩写列表或[NSTimeZone knownTimeZoneNames]的时区名称列表中找不到任何地方。这是哪里来的?我需要将当前设备时区传递给 Rails 应用程序,它可以理解"Australia/Sydney""America/Chicago"等内容,但不能理解"US/Central"

如何获取 localTimeZone 给我的信息并将其转换为 Rails 可以理解的字符串(即 knownTimeZoneNames 中的任何时区,它应该是系统知道的所有时区名称?)

4

2 回答 2

5

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.

于 2009-11-17T10:30:00.957 回答
0

你是从模拟器中得到的吗?如果是这样,如果您从终端运行此命令,它会返回什么?
$系统设置-gettimezone

Mac OS 10.5 中的时区设置 GUI 会将时区设置为 US/*。如果您从控制台手动设置它,您可以将其设置为预期的时区之一。

运行此程序以获取有效列表,并注意 US/Central 不在其中。
$ systemsetup -listtimezones

运行此命令将其设置为 America/Chicago
$ systemsetup -settimezone America/Chicago

于 2009-11-15T15:38:22.627 回答