Rails - 在指定时区选择日期时间

发布于 2024-11-13 05:46:59 字数 755 浏览 12 评论 0原文

我正在使用一个音乐会巡演网站的应用程序,其中所有时间(公告时间、促销开始时间和活动开始时间)都是每个特定场地所在时区的本地时间。我将用户输入的日期/时间(如果适用)运行 before_filter 以设置适当的时区,以便所有内容都以 UTC 格式存储在数据库中。对于“新”表单以及在索引和显示操作中显示时间,完全没有问题。当数据从数据库中带回视图中时,我使用 in_time_zone 根据特定地点进行调整。

唯一的问题是编辑表单。日期/时间选择显示 UTC 格式的数据。当我在网站上工作时,我会在心理上进行调整,但对其他人来说却很混乱。我想做一些类似的事情:

<%= f.datetime_select :start_datetime.in_time_zone(@event.time_zone) %>

或者,在控制器中:

def edit
  @performance = Performance.find(params[:id])
  @event = @performance.event
  @performance.start_datetime = @performance.start_datetime.in_time_zone(@event.time_zone)
end

然后简单地, <%= f.datetime_select :start_datetime %>

不幸的是,我还没有找到正确的方法来做到这一点。您有什么值得尝试的想法吗?

非常感谢。

I'm working with an app for a concert tour website, where all times (announcement times, on-sale start times, and event start times) are local to each particular venue's time zone. I take the user entered date/time where applicable and run a before_filter to set the appropriate time zone so that everything is stored in the database in UTC. For the 'new' form and for displaying the times in index and show actions, no problem at all. When the data is brought back out of the database and into a view, I use in_time_zone to adjust per the particular venue.

The only issue is on the edit form. The date/time select is showing the data in UTC. When I'm working on the site, I mentally adjust, but for others it's confusing. I'd like to do something along the lines of:

<%= f.datetime_select :start_datetime.in_time_zone(@event.time_zone) %>

Or, in the controller:

def edit
  @performance = Performance.find(params[:id])
  @event = @performance.event
  @performance.start_datetime = @performance.start_datetime.in_time_zone(@event.time_zone)
end

Then simply, <%= f.datetime_select :start_datetime %>.

Unfortunately, I haven't found the right way to do this. Do you have any ideas worth giving a shot?

Thank you very much.

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

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

发布评论

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

评论(4

ぃ双果 2024-11-20 05:46:59

您可以使用 datetime_select 的默认方法,如下所示:

%br
= f.label :req_sess_start, "Session starts at"
= f.datetime_select(:req_sess_start, :start_year => 2010, :ampm => true, :default => 0.days.from_now.in_time_zone(@timezone))

由于显示的默认值,客户端将假定必须在他/她的本地时区中输入时间,但是...
该值实际上将位于应用程序默认的时区中(如 application.rb 中提供的,默认值是 UTC)。
因此,您需要一些服务器端编码才能将其转换为正确的值。

You may use default method of datetime_select, as following:

%br
= f.label :req_sess_start, "Session starts at"
= f.datetime_select(:req_sess_start, :start_year => 2010, :ampm => true, :default => 0.days.from_now.in_time_zone(@timezone))

Because of the default value shown, client will assume that times has to be entered in his/her local timezone, but...
this value will be actually in the timezone default to your application (as provided in application.rb, and the default is UTC).
So, you would require some server side coding to convert it to correct value.

栖迟 2024-11-20 05:46:59

我不确定我是否理解您想要做什么,但由于您正在存储 @event.time_zone,因此您可以添加

:default => start_time.in_time_zone(@event.time_zone)

到 datetime_select 表单字段。

I'm not sure if I understand what you want to do, but since you're storing the @event.time_zone, you could add

:default => start_time.in_time_zone(@event.time_zone)

to your datetime_select form field.

_蜘蛛 2024-11-20 05:46:59

像这样的事情怎么样:

# change PST to correct zone abbrev.
new_zone = ActiveSupport::TimeZone.new("PST")
@performance.start_datetime = @performance.start_datetime.in_time_zone(new_zone)

How about something like this:

# change PST to correct zone abbrev.
new_zone = ActiveSupport::TimeZone.new("PST")
@performance.start_datetime = @performance.start_datetime.in_time_zone(new_zone)
洋洋洒洒 2024-11-20 05:46:59

我刚刚注意到这是一篇旧帖子,但是:
如果我是你,我会使用虚拟属性来表示场地的日期时间。例如,您可以向性能添加一个名为 adjustment_start_time 的属性。

I just noticed this is an old post but:
If I were you, I would use a virtual attribute to represent the date time of the venue. For instance, you could add an attribute to performance called adjusted_start_time.

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