如何在带有事件的 Rails 应用程序中处理时区 [英] How to deal with time zones in a Rails app with events

查看:33
本文介绍了如何在带有事件的 Rails 应用程序中处理时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于乐队的 Rails 应用程序.乐队可以导入他们在不同时区发生的表演.将这些事件存储在 UTC 中似乎需要大量的工作.我必须弄清楚创建的任何节目的时区,然后在向用户显示时转换回节目的本地时区.是否有一个简单的插件可以根据地理位置获取 UTC 偏移量?这可能会有所帮助,但有没有人看到我应该在这里存储 UTC 的任何主要原因?我知道在 UTC 中存储时间戳可能是个好主意……但是乐队事件时间?

I have a Rails app for bands. Bands can import their shows which all occur in different time zones. It seems like a ton of work to store these events in UTC. I would have to figure out the time zone for any show created and then convert back to the show's local time zone when displaying to the user. Is there a simple plugin to get a UTC offset based on geolocation? That would probably help, but does anyone see any major reasons why I should store in UTC here? I understand storing timestamps in UTC is probably a good idea...but band event times?

推荐答案

我正在做类似的事情 - 一个包含事件列表的站点.在我的情况下,将时间标准化(以 UTC 为单位)很重要,因为我们需要担心发布时间和促销时间(即,网站上的活动何时出现以及促销链接何时出现),而不仅仅是显示事件开始时间(它本身并不关心它在哪个时区).

I'm working on something similar - a site with a list of events. In my situation, it's important to have the times standardized (in UTC) because we have announce and on-sale times to worry about (i.e., when the events appear on the site and when the on-sale links show up), not just displaying the event start time (which itself doesn't care what time zone it's in).

从数据库中的 UTC 时间到给定地点的本地时间(即,在事件列表中显示本地时间)非常简单,使用类似 e.start.in_time_zone(#{e.venue.time_zone}").我无法弄清楚的是在数据输入点获取本地时间,认为本地时间需要转换为 UTC,而不必处理更改 Time.zone.

Going from a UTC time in the database to the local time for the given venue (i.e., to display the local time in the event listing) is pretty simple using something along the lines of e.start.in_time_zone("#{e.venue.time_zone}"). What I couldn't figure out was getting the local time at the point of data entry recognized as a local time needed to be converted to UTC, without having to deal with changing Time.zone.

我发现了一些有用的东西.看看这篇文章:http://steveluscher.com/archives/changed-a-times-zone-in-rails-keeping-the-same-local-representation.我在我的 config/initializers 目录中添加了一个新文件 (time_zone_support.rb).以下是内容:

I found something that works. Check out this post: http://steveluscher.com/archives/changing-a-times-zone-in-rails-keeping-the-same-local-representation. I added a new file (time_zone_support.rb) to my config/initializers directory. Here are the contents:

module ActiveSupport
  class TimeWithZone
    def zone=(new_zone = ::Time.zone)
      # Reinitialize with the new zone and the local time
      initialize(nil, ::Time.__send__(:get_zone, new_zone), time)
    end
  end
end

这允许在控制台中执行以下操作:

This allows the following in the console:

>> e.starts = Time.zone.parse("2010-09-12 10:00 am")
=> Sun, 12 Sep 2010 10:00:00 UTC +00:00
>> e.starts.zone = e.time_zone
=> "Pacific Time (US & Canada)"
>> e.starts
=> Sun, 12 Sep 2010 10:00:00 PDT -07:00
>> e.save
=> true

希望对你也有帮助!

这篇关于如何在带有事件的 Rails 应用程序中处理时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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