如何将 TZInfo 标识符转换为 Rails 时区名称/键 [英] How to Convert TZInfo identifier to Rails TimeZone name/key

查看:63
本文介绍了如何将 TZInfo 标识符转换为 Rails 时区名称/键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将作为 TZInfo 标识符接收的 js 值转换为 Rails TimeZone 名称/键?

FROM: "America/New_York" 从 JavaScript TZinfo 检测返回
TO:东部时间(美国和加拿大)" Rails TimeZone 中使用的约定


或者另一个例子:
"Pacific/Honolulu" => 转换为 => "Hawaii"

两者都可以在 ActiveSupport::TimeZone <Object 映射但 rails 使用键 [i.g.东部时间(美国和加拿大)"] 在下拉菜单中,验证和存储到 Time.use_zone().


根据我对 ActiveSupport::TimeZone.us_zones 的理解,这似乎很重要,尤其是在 DayLights 节省时间(这轨听起来处理得很好)和仅匹配偏移量无法实现的情况下.如果它没有使用 rails TimeZone 名称存储到数据库,则验证将失败,并且在用户的配置文件设置页面中与 ActiveSupport::TimeZone.zones_map

的下拉列表不正确匹配

这样做的目的是用户无需在注册时选择时区,也无需在注册后在设置中更改时区.浏览器检测到它并在注册时将其传递给 hidden_​​field.在极少数情况下,他们会在与家庭/工作地点不同的地方注册.他们可以稍后在他们的帐户设置中手动覆盖.

在尝试摄取 js 时区检测时似乎是一个常见的差距.这甚至可能成为一个次要问题,即如何将返回的信息从 js 传递到 rails 进行转换,然后返回到 js 以存储回表单的 hidden_​​field 中?希望我正确地提出了问题&诚然,轨道有点绿色,所以可能有一个简单的解决方案......

非常感谢大家的帮助!
-E

<小时>

ActiveSupport Time.zone 文档
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-i-parse

MAPPING = {东部时间(美国和加拿大)" =>美国/纽约"


使用 js 打包的 gem 'temporal-rails' 检测用户时区:
https://github.com/jejacks0n/temporal

User Time_Zone 实现如下:
http://railscasts.com/episodes/106-time-zones-revised>

*使用设计 &设计-不可避免

<小时>

注册查看脚本

 <脚本>$(函数(){varDetected_zone = Temporal.detect();控制台日志(检测区域);//返回对象检测区域 = 检测区域.timezone.name;控制台日志(检测区域);//返回 "America/New_York"$('#user_time_zone').val(detected_zone);//!需要将其转换为 rails TimeZone 名称!});

用户模型

 validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name)

用户帐户设置表单

 <%= f.label :time_zone, label: "Time Zone" %><br/><%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>

解决方案

Temporal 包含所需的逻辑,但要回答您的问题:

Time.zone = ActiveSupport::TimeZone.new("America/New_York")

编辑,我想我的回答不完整.您想将它从美国/纽约"获取到东部时间(美国和加拿大)",对吗?如果是这样的话,这是我拥有的最佳解决方案——尽管有人可能会提供更好的解决方案.

ActiveSupport::TimeZone::MAPPING.select {|k, v|v == "America/New_York" }.keys.first

How do you convert js values received as TZInfo identifiers to Rails TimeZone name/key?

FROM: "America/New_York" returned from JavaScript TZinfo detection
TO: "Eastern Time (US & Canada)" convention used in Rails TimeZone


or another example:
"Pacific/Honolulu" => converted to => "Hawaii"

Both are available in ActiveSupport::TimeZone < Object mapping but rails uses the key [i.g. "Eastern Time (US & Canada)"] in drop-downs, validation & storing to Time.use_zone().


Based on what I understand of ActiveSupport::TimeZone.us_zones this seems to be important especially incases of DayLights savings time (which rails sounds to handle well) and matching just the offset would not accomplish. If it is not stored to DB with the rails TimeZone name then validation fails and does not match up properly in the user's profile settings page with the dropdown list of ActiveSupport::TimeZone.zones_map

Goal of this is that the user does not have to select their timezone on signup or are required to change it in their settings after signup. The browser detects it and passes it to hidden_field on signup. In the rare occasion they sign up in a place different than their home/work. they can manually override in their account settings later.

Seems to be a common gap when trying to ingest js timezone detection. This might even become a secondary question of how to pass the returned info from js to rails for conversion and then back to js to store back in the hidden_field of the form? Hopefully I framed the question properly & admittedly a bit green with rails so there may be a simple solution to this...

Thanks so much for all the help!
-E


ActiveSupport Time.zone Documentation
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-i-parse

MAPPING = {"Eastern Time (US & Canada)" => "America/New_York"


Using js packaged gem 'temporal-rails' to detect users timezone:
https://github.com/jejacks0n/temporal

User Time_Zone implement as seen:
http://railscasts.com/episodes/106-time-zones-revised

*Using Devise & Devise-Inevitable


Sign-Up View Script

    <script>
    $(function() {
        var detected_zone = Temporal.detect();
        console.log(detected_zone);  // returns object
        detected_zone = detected_zone.timezone.name;
        console.log(detected_zone);  // returns "America/New_York"
        $('#user_time_zone').val(detected_zone);  // ! need to convert this to rails TimeZone name !
    });
    </script>

User Model

    validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name)

User Account Settings Form

    <%= f.label :time_zone, label: "Time Zone" %><br />
    <%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>

解决方案

Temporal includes the needed logic, but to answer your question:

Time.zone = ActiveSupport::TimeZone.new("America/New_York")

Edit, I guess my answer is incomplete. You want to get it from "America/New_York" to "Eastern Time (US & Canada)", correct? If that's the case this is the best solution I have -- though someone may be able to provide a better one.

ActiveSupport::TimeZone::MAPPING.select {|k, v| v == "America/New_York" }.keys.first

这篇关于如何将 TZInfo 标识符转换为 Rails 时区名称/键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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