如何从过时的 TZInfo 标识符中获取 Rails TimeZone 名称? [英] How do I get Rails TimeZone names from obsolete TZInfo identifiers?

查看:33
本文介绍了如何从过时的 TZInfo 标识符中获取 Rails TimeZone 名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有一个问题可以回答如何将America/Los_Angeles"转换为Pacific Time (US & Canada)".但是我想将美国/太平洋"和其他过时的时区转换为 Rails 时区.我无法在图书馆中找到任何可以帮助我完成此任务的内容.

There is already a question which answers how to convert "America/Los_Angeles" to "Pacific Time (US & Canada)". However I want to convert "US/Pacific" and other obsolete time zones to Rails TimeZone. I am unable to find anything in the library which helps me accomplish this.

推荐答案

来自 Rails ActiveSupport::时区文档:

与 Active Support 捆绑的 TZInfo 版本仅包含支持 TimeZone 类定义的区域所需的定义.如果您需要使用 TimeZone 未定义的区域,则需要安装 TZInfo gem(如果本地安装了最新版本的 gem,则将使用它而不是捆绑版本.)

The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones defined by the TimeZone class. If you need to use zones that aren’t defined by TimeZone, you’ll need to install the TZInfo gem (if a recent version of the gem is installed locally, this will be used instead of the bundled version.)

就我个人而言,我认为 Rails 时区很愚蠢.它们被任意限制为 Rails 所描述的146 个区域的有意义的子集".但他们没有解释他们如何确定哪些区域有意义",哪些区域被丢弃.

Personally, I think Rails time zones are silly. They are arbitrarily limited to what Rails describes as "a meaningful subset of 146 zones". But they do nothing to explain how they determine which zones are "meaningful" and which are discarded.

诸如US/Pacific"之类的标识符在 TZDB 数据中称为链接"或别名".我可以看到,也许它们可能会从 Rails 中省略,但还有许多其他现实世界的时区完全缺失.

Identifiers such as "US/Pacific" are known as "links" or "aliases" in the TZDB data. I could see that perhaps they might be omitted from Rails, but there are many other real-world time zones that are missing entirely.

我在现实世界中唯一见过 Rails 时区标识符的地方是 Twitter API.另一方面,IANA/TZDB 标识符无处不在.

The only place I've every seen Rails time zones identifiers in the real world is in the Twitter API. On the other hand, IANA/TZDB identifiers are ubiquitous.

我能提供的最好建议是忘记 Rails 时区,只使用 Ruby TZInfo Gem直接地.它具有包含所有区域的完整 TZDB 实现,包括别名.

The best advice I can offer is to forget about Rails time zones, and just use the Ruby TZInfo Gem directly. It has the full TZDB implementation with all zones, including aliases.

如果您确实必须继续使用 Rails 区域,您可以首先使用 TZInfo 将别名解析为实际区域,然后查看该区域是否在 Rails MAPPING 字典中.例如:

If you really must continue to use Rails zones, you could first use TZInfo to resolve the alias to the actual zone, and then see if that that zone is in the Rails MAPPING dictionary. For example:

"US/Pacific" => "America/Los_Angeles"                    (via TZInfo)

"America/Los_Angeles" => "Pacific Time (US & Canada)"    (via Rails MAPPING)

我相信当您通过 TZInfo 加载US/Pacific"时,它将被子类化为 LinkedTimezoneInfo 对象,因此您可以从那里使用 link_to_identifier 属性.

I believe when you load "US/Pacific" via TZInfo, it will be subclassed as a LinkedTimezoneInfo object, so you could use the link_to_identifier attribute from there.

这篇关于如何从过时的 TZInfo 标识符中获取 Rails TimeZone 名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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