iPhone NSTimeZone:localTimeZone混乱 [英] iPhone NSTimeZone: localTimeZone confusion

查看:137
本文介绍了iPhone NSTimeZone:localTimeZone混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,致电

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

为您提供设备的本地时区。它给我的是US / Central,我在 [NSTimeZone abbreviationDictionary] 的缩写列表或时区列表中的任何位置都找不到 [NSTimeZone knownTimeZoneNames] 中的名称。这个来自哪里?我需要将当前设备时区传递给Rails应用程序,它理解澳大利亚/悉尼America / Chicago等内容,但不是 美/中环

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".

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

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

推荐答案

你可能正在使用 ActiveSupport :: TimeWithZone ,无需 tzinfo gem:

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

如果您不需要 tzinfo ,你只会获得一些相当无用的时区子集。

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

编辑 :为了避免混淆,我使用私有时间#get_zone API的原因是因为当你打电话时,幕后使用的是什么时间#区= 。如果您不需要 tzinfo ,则调用 Time.send(:get_zone,US / Central)返回 nil

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.

这篇关于iPhone NSTimeZone:localTimeZone混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆