How do I set default timezone for Rails and ActiveRecord? I have a lot of places where time is diplayed(for example creation of some instance in the database, etc.). The time is displayed in my current timezone. Should i somehow configure Rails/ActiveRecord or *nix distributive that i am using on hosting platform?
如何为Rails和ActiveRecord设置默认时区?我有很多地方可以显示时间(例如在数据库中创建一些实例等)。时间显示在我当前的时区中。我应该以某种方式配置我在托管平台上使用的Rails / ActiveRecord或* nix分配?
I've tryied to add that to application.rb
file without any result:
我试图将它添加到application.rb文件,没有任何结果:
config.time_zone = 'Moscow'
config.active_record.default_timezone = :local
I've also tried to use
我也试过用
config.time_zone = 'Moscow'
config.active_record.default_timezone = "Moscow"
But in that case i get a warning
但在那种情况下我会收到警告
warning: :database_timezone option must be :utc or :local - defaulting to :local
Is there a solution that will prevent me from changing all those places where time is rendered?
有没有一种解决方案可以阻止我改变那些时间呈现的地方?
2 个解决方案
#1
1
Just try using the below given one only in application.rb,
只需在application.rb中尝试使用下面给出的一个,
config.active_record.default_timezone = :local
discard,
config.time_zone
May that help you.
可以帮助你。
#2
0
First off: I'm always for not messing with the way AR stores time stamps, because Rails already does all the magic about time conversion for you.
首先:我总是没有弄乱AR存储时间戳的方式,因为Rails已经为你做了所有关于时间转换的魔术。
I assume you want to display the times in your app in the time zone the current user has selected (or that you have determined per-user in some other way). In that case what you configured in config.time_zone
is of no consequence, because you obviously want to overwrite it. Rails has a neat mechanism for this called Time.use_zone
.
我假设您要在当前用户选择的时区中显示应用中的时间(或者您已经以其他方式确定了每个用户)。在这种情况下,您在config.time_zone中配置的内容无关紧要,因为您显然想要覆盖它。 Rails有一个简洁的机制,叫做Time.use_zone。
I used that in an around_filter
before, like this:
我之前在around_filter中使用过,像这样:
around_filter :set_time_zone
def set_time_zone
Time.use_zone(current_user.try(:time_zone) || Time.zone) do
yield
end
end
This sets the time zone for the current request to the current_user's time_zone if that user is present. Falls back to the time zone configured in application.rb otherwise.
如果该用户存在,这会将当前请求的时区设置为current_user的time_zone。否则回落到application.rb中配置的时区。
Inside the Time.use_zone
block, all times are displayed and saved as in that zone.
在Time.use_zone块内,所有时间都显示并保存在该区域中。
What's very handy about this approach is that after each request the time zone is reset to what is configured. If you'd just change Time.zone and wouldn't bother resetting it, it can potentially leak to other users.
这种方法非常方便的是,在每次请求之后,时区将重置为配置的内容。如果您只是更改Time.zone并且不打算重置它,它可能会泄漏给其他用户。
I suggest you try this and see if it does what you want before trying to wrap your head around what all this means, because that can make your head spin. I'm happy to explain more about the how and why, if you're interested, though.
我建议你尝试一下,看看它是否能达到你想要的效果,然后再尝试将所有这一切都包裹起来,因为这样可以让你头晕目眩。如果您有兴趣,我很乐意更多地解释如何以及为什么。
#1
1
Just try using the below given one only in application.rb,
只需在application.rb中尝试使用下面给出的一个,
config.active_record.default_timezone = :local
discard,
config.time_zone
May that help you.
可以帮助你。
#2
0
First off: I'm always for not messing with the way AR stores time stamps, because Rails already does all the magic about time conversion for you.
首先:我总是没有弄乱AR存储时间戳的方式,因为Rails已经为你做了所有关于时间转换的魔术。
I assume you want to display the times in your app in the time zone the current user has selected (or that you have determined per-user in some other way). In that case what you configured in config.time_zone
is of no consequence, because you obviously want to overwrite it. Rails has a neat mechanism for this called Time.use_zone
.
我假设您要在当前用户选择的时区中显示应用中的时间(或者您已经以其他方式确定了每个用户)。在这种情况下,您在config.time_zone中配置的内容无关紧要,因为您显然想要覆盖它。 Rails有一个简洁的机制,叫做Time.use_zone。
I used that in an around_filter
before, like this:
我之前在around_filter中使用过,像这样:
around_filter :set_time_zone
def set_time_zone
Time.use_zone(current_user.try(:time_zone) || Time.zone) do
yield
end
end
This sets the time zone for the current request to the current_user's time_zone if that user is present. Falls back to the time zone configured in application.rb otherwise.
如果该用户存在,这会将当前请求的时区设置为current_user的time_zone。否则回落到application.rb中配置的时区。
Inside the Time.use_zone
block, all times are displayed and saved as in that zone.
在Time.use_zone块内,所有时间都显示并保存在该区域中。
What's very handy about this approach is that after each request the time zone is reset to what is configured. If you'd just change Time.zone and wouldn't bother resetting it, it can potentially leak to other users.
这种方法非常方便的是,在每次请求之后,时区将重置为配置的内容。如果您只是更改Time.zone并且不打算重置它,它可能会泄漏给其他用户。
I suggest you try this and see if it does what you want before trying to wrap your head around what all this means, because that can make your head spin. I'm happy to explain more about the how and why, if you're interested, though.
我建议你尝试一下,看看它是否能达到你想要的效果,然后再尝试将所有这一切都包裹起来,因为这样可以让你头晕目眩。如果您有兴趣,我很乐意更多地解释如何以及为什么。