Rails 2.3.4 中基于行的时区

发布于 2024-08-06 21:13:23 字数 447 浏览 0 评论 0原文

处理以下情况的最佳方法是什么:

我们有创建“事件”的“管理员”。这些事件可以是全国性的,即:不同的时区。截至目前,有一个选择菜单,其中包含当前的美国时区列表。数据库表中有一个与所选时区相对应的列。这样管理员就可以选择下午 5 点东部时间、下午 5 点太平洋时间等。而不是试图找出 GMT 等效时间。

应用程序根据environment.rb 文件使用UTC 作为默认时区。

如果管理员选择“东部时间(美国和加拿大)”,所选日期戳仍会保存为 UTC(或应用程序默认值)。我需要执行查询,其中事件在上面选择的时间(包括时区)之前不会显示。

既然事件需要自己的时区,那么数据应该如何保存呢? 我最初认为我需要在 Rails 保存值之前欺骗它,因此它保存了时区的 UTC 偏移量。

这将在 before_save 和 after_find 模型方法中处理。但它似乎不起作用,或者更确切地说我错过了一些东西..

What is the best way to handle the following situation:

We have "admins" who create "events". These events can be nationwide, ie: different time zones. As of now there is a select menu with the current list of US time zones. There is a corresponding column in the database table, for the time zone selected. This is so the admin can select 5PM Eastern, 5PM Pacific, etc. Instead of trying to figure out the GMT equivalent.

The application is using UTC as per the environment.rb file, as the default time-zone.

If an admin selects "Eastern Time (US & Canada)", the date stamp selected is still saved as UTC (or the app default). I need to perform queries where events don't show up before the time selected above including the time zone.

Since the events need their own time zone, how should the data be saved?
I was originally thinking I need to trick Rails before it saves the value, so it saves UTC offset for the time zone.

This would be handled in before_save and after_find model methods. But it doesn't seem to work, or rather I am missing something..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-08-13 21:13:23

你错过了一个简单的事实。时间是普遍的。世界标准时间 (UTC) 下午 1:00 是任何地方世界标准时间 (UTC) 下午 1:00。您甚至不需要为每个事件存储 time_zone 。您只需让管理员选择它即可更轻松地输入时间。就后端而言,它始终采用 UTC。你当然可以输入不同时区的时间,然后输出到不同的时区,但无论你如何扭转它仍然是相同的“伦敦下午 1:00”。因此,当用户查看事件时,您可以简单地执行 event_date < Time.now.utc(所有过去的事件)。

但是,如果观察用户实际上与事件位于不同的时区,并且您希望以用户的家乡时区显示事件日期,那么您可以让用户选择他们的家乡时区,并将其存储在用户表中。然后,在 ApplicationController 上的 before_filter 中,您可以执行以下操作:

class ApplicationController < ActionController::Base
  before_filter :set_time_zone

  private
  def set_time_zone
    Time.zone = current_user.time_zone if current_user
  end
end

然后,来自数据库的每个时间值(例如 event.date)将自动转换为用户所在时区。

或者也许我误解了这个问题。 :) 让我知道。

You're missing a simple fact. Time is universal. 1:00pm in UTC is 1:00pm in UTC anywhere. You don't even need to store a time_zone with every event. You just need to let admins select it for easier time input. As far as backend, it's always in UTC. You can certainly input time from different timezone, and output into different timezone, but it's still the same "1:00pm in London" no matter how you twist it. So when a user is looking at events, you can simply do event_date < Time.now.utc (all past events).

However, if the observing user is actually in a different timezone from the events, and you want to display event dates in the user's home timezone, then you can let user select their home timezone, and store it in users table. Then in before_filter on your ApplicationController you can just do:

class ApplicationController < ActionController::Base
  before_filter :set_time_zone

  private
  def set_time_zone
    Time.zone = current_user.time_zone if current_user
  end
end

Then every Time value that comes from database (such as event.date) will be automatically converted in the user's home time zone.

Or maybe I misunderstood the question. : ) Let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文