time_select 带有 12 小时时间和 Ruby on Rails 中的时区 [英] time_select with 12 hour time and Time Zone in Ruby on Rails
问题描述
我需要从 Rails 2.3.8 应用程序的用户那里捕获时间和时区,但一直无法想出一个干净的解决方案来创建和解析选择.
理想情况下,我会有以下下拉菜单:
- 小时 (1-12)
- 分钟 (0-59)
- 上午/下午
- 时区
是否有一个 gem/插件可以完成我正在寻找的东西?我需要在数据库中存储时间和时区吗?最佳存储策略是什么?
我最终需要以 UTC 格式输出这些值,但用户应该能够返回并查看正确时区的时间.
您可以使用适当的方法获取时区选择:
同样,还有用于日期的 date_select.>
存储:
如果时区特定于用户并且不会更改,则将他们的时区存储在他们的用户记录中并在加载 current_user 时设置它.Rails 会将时间与 UTC 相互转换,并始终将 UTC 存储在数据库中,并为您自动转换为该默认时区(包括夏令时!).最简单的方法.
use_zone(zone) 可让您覆盖默认区域用于块,因此您可以接受表单值并使用该函数设置它并在该块中设置您的值.
更新:我写了一些 Rails 时区示例 作为博客条目.
I have the need to capture a time and time zone from users of a rails 2.3.8 app, but have been unable to think of a clean solution to create and parse the selections.
Ideally I would have a drop-down menus for the following:
- hour (1-12)
- minute (0-59)
- AM/PM
- Time Zone
Is there a gem/plugin that accomplishes what I am looking for? Will I need to store both the time and time zone in the database? What is the best strategy for storage?
I'll eventually need to spit these values out in UTC, but a user should be able to go back and review the time in the correct time zone.
You can get time zone selects with the appropriate methods:
Similarly, there's date_select for dates.
Storage:
If the timezone is specific to the user and doesn't change, then store their time zone in their user record and set it when you load the current_user. Rails will convert times to/from UTC and always store UTC in the database and do the automatic convert to that default timezone for you (including daylight savings!). Easiest way to do it.
use_zone(zone) lets you override the default zone for a block, so you can accept a form value and set it with that function and set your value in that block.
UPDATE: I wrote up some Rails timezone examples as a blog entry.
这篇关于time_select 带有 12 小时时间和 Ruby on Rails 中的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!