Ruby-on-rails 带导轨的时区 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4362663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:49:07  来源:igfitidea点击:

Timezone with rails 3

ruby-on-railsruby-on-rails-3timezone

提问by Addy

I have pretty common issue but for some reason i have tried all the suggestions on the web and none seem to work.

我有很常见的问题,但出于某种原因,我已经尝试了网络上的所有建议,但似乎都没有奏效。

I have set the Timezone in config to 'EST'

我已将配置中的时区设置为“EST”

config.time_zone = 'Eastern Time (US & Canada)'

But when the time is shown on the the screen, it continues to show the UTC time that is stored in the DB. I tried the debugger and here is the output

但是当时间显示在屏幕上时,它会继续显示存储在 DB 中的 UTC 时间。我试过调试器,这是输出

(rdb:1) Time.zone 
#<ActiveSupport::TimeZone:0x1061f4760 @utc_offset=nil, @current_period=nil, @name="Eastern Time (US & Canada)", @tzinfo=#<TZInfo::TimezoneProxy: America/New_York>>
(rdb:1) Order.first.placed_at
Fri Jan 01 15:00:00 UTC 2010

Update: Here is another user who has the same question Rails timezone is wrong when shown

更新:这是另一个有相同问题的用户, 显示时 Rails timezone is wrong

回答by Paul Schreiber

Try in_time_zone. For example

试试in_time_zone。例如

>> Time.now
=> Sun Dec 05 21:34:45 -0500 2010
>> Time.zone
=> #<ActiveSupport::TimeZone:0x1033d97b8 @name="Pacific Time (US & Canada)", @tzinfo=#<TZInfo::DataTimezone: America/Los_Angeles>, @utc_offset=-28800, @current_period=nil>
>> Time.now.in_time_zone
=> Sun, 05 Dec 2010 18:34:54 PST -08:00

In your case, you want Order.first.placed_at.in_time_zone.

在你的情况下,你想要Order.first.placed_at.in_time_zone.

回答by Danny

If you want to set a different time zone for different users of your app, make sure you use an around_filter as opposed to a before filter to change Time.zone. Something to do with how Time.zone leaks over to other threads and thus other users may inheret a time zone that they shouldn't. From this blog post: http://ilikestuffblog.com/2011/02/03/how-to-set-a-time-zone-for-each-request-in-rails/

如果您想为应用程序的不同用户设置不同的时区,请确保使用 around_filter 而不是 before 过滤器来更改 Time.zone。与 Time.zone 如何泄漏到其他线程有关,因此其他用户可能会继承他们不应该使用的时区。来自这篇博文:http: //ilikestuffblog.com/2011/02/03/how-to-set-a-time-zone-for-each-request-in-rails/

in application_controller.rb:

在 application_controller.rb 中:

around_filter :set_time_zone

private

def set_time_zone
  old_time_zone = Time.zone
  Time.zone = current_user.time_zone if logged_in?
  yield
ensure
  Time.zone = old_time_zone
end

I also found it helpful to use the time_zone_selectform helper method when allowing users to change their time zone. If you called your field :time_zone, you would use it like:

我还发现在允许用户更改时区时使用time_zone_select表单助手方法很有帮助。如果你调用你的字段:time_zone,你会像这样使用它:

f.time_zone_select(:time_zone)

And lastly, this looks pretty awesome. Auto detect and set time zone via javascript. Here's the rails gem to add to the asset pipeline: https://github.com/scottwater/detect_timezone_railsand accompanying blog post: http://www.scottw.com/automated-timezone-detection

最后,这看起来非常棒。 通过 javascript 自动检测和设置时区。这是要添加到资产管道的 rails gem:https: //github.com/scottwater/detect_timezone_rails和随附的博客文章:http: //www.scottw.com/automated-timezone-detection

回答by Duke

Check that your ActiveRecord::Base.time_zone_aware_attributesis true.

检查你的ActiveRecord::Base.time_zone_aware_attributes是否属实。

回答by joshnabbott

I think what you're describing is this bug - https://github.com/rails/rails/issues/6816

我认为你所描述的是这个错误 - https://github.com/rails/rails/issues/6816