Rails更改默认时区 [英] Rails change default timezone
问题描述
我正在尝试将Rails 3.2.8应用程序中的默认时区更改为GMT + 04:00,但我不知道该怎么做。以下解决方案不适用于我:
I'm trying to change default timezone in Rails 3.2.8 app to GMT+04:00, but I don't know how to do it. The following solutions do not work for me:
config.time_zone = 'Moscow'
config.time_zone = "(GMT+04:00) Moscow"
config.active_record.default_timezone = 'Moscow'
config.active_record.default_timezone = :local
我也在Rails控制台中尝试了以下操作:
ActiveSupport :: TimeZone.all.map(&:name)
返回了列表值,包括莫斯科
。
Time.zone
返回了(GMT + 00:00)UTC
不正确,应该是(GMT + 04:00)UTC
。然后,我更改了 Time.zone =莫斯科
和 Time.now
返回了正确的值(... +0400 )。
Also I've tried in rails console the following:
ActiveSupport::TimeZone.all.map(&:name)
wich returned a list of values, including "Moscow"
.
Time.zone
returned (GMT+00:00) UTC
wich is not correct, it should be (GMT+04:00) UTC
. Then I changed Time.zone = "Moscow"
and Time.now
returned the correct value (... +0400).
因此,要解决此问题,我只使用了 Time.now + 4.hour
,但我还需要datetime_select显示我的当地时间。 Time.now + 4.hour
不是正确的解决方案。
So to fix it I simply used Time.now + 4.hour
, but I also need datetime_select to display my local time. Time.now + 4.hour
is not a correct solution.
请分享您如何设置的任何想法默认时区为其本地值。
预先谢谢您。
Please, share any ideas how one can set default time zone to his local value. Thank you in advance.
推荐答案
突然之间,我已经定位了问题所在。
我运行Win7,我的WEBrick服务器显示正确的时间,但是项目本身显示了错误的时间。同时,我的生产服务器正常运行,因此,显然问题出在操作系统上。
UPD :此问题已解决。我应该在更改application.rb文件后重新启动WEBrick服务器。
All of a sudden I've localized the problem. I run Win7 and my WEBrick server is showing the correct time, but the project itself shows a wrong one. In the same time, my production server is working correctly, so, obviously, the problem is with the OS. UPD: The problem is fixed. I should have restarted the WEBrick server after changing the application.rb file.
这篇关于Rails更改默认时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!