如何更改 Rails 中 Active Record 的默认时区?
在我的 application.rb
中,我遇到了以下评论
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Eastern Time (US & Canada)'
正如您从上面看到的,我已将 config.time_zone
设置为 EST 时间。但是,当在数据库中创建记录时,日期时间看起来仍以 UTC 格式存储。
在上面的评论中,他们说
...并使 Active Record 自动转换到此区域...
我怎样才能做到这一点,在哪里?
另外,我也会将其部署在 Heroku 上,并且我希望保留该设置
In my application.rb
I came across the following comment
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Eastern Time (US & Canada)'
As you see from above, I've made config.time_zone
to EST time. However, still when records are created in the DB, it looks like datetime
is being stored in UTC format.
In the above comment, they say
...and make Active Record auto-convert to this zone...
How can I do that, and where?
Also, I'll be deploying this on heroku as well and i'd like the setting to carry over
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我决定编译这个答案,因为所有其他答案似乎都不完整。
如果您想更改Rails时区,但继续将活动记录以UTC保存在数据库中,请使用
如果您想更改Rails时区ANDActive Record 在此时区存储时间,请使用
警告:在以非 UTC 格式将时间保存到数据库之前,您确实应该三思而后行,甚至三思而行。
请记住,
config.active_record.default_timezone
只能采用两个值config.time_zone
中定义的时区)以下是查找所有可用时区的方法
I have decided to compile this answer because all others seem to be incomplete.
If you want to change Rails timezone, but continue to have Active Record save in the database in UTC, use
If you want to change Rails timezone AND have Active Record store times in this timezone, use
Warning: you really should think twice, even thrice, before saving times in the database in a non-UTC format.
Remember that
config.active_record.default_timezone
can take only two valuesconfig.time_zone
)Here's how you can find all available timezones
将以下内容添加到
application.rb
有效adding following to
application.rb
works经过一番痛苦之后,我得出了与迪安·佩里相同的结论。
config.time_zone = 'Adelaide'
和config.active_record.default_timezone = :local
是获胜组合。这是我在此过程中发现的内容。I came to the same conclusion as Dean Perry after much anguish.
config.time_zone = 'Adelaide'
andconfig.active_record.default_timezone = :local
was the winning combination. Here's what I found during the process.就我而言(Rails 5),我最终在我的
app/config/environments/development.rb
中添加了这两行,就是这样!为了确保正确读取 Melbourne,我在终端中运行了命令:
bundle exec rake time:zones:all
并且 Melbourne 列在我所在的时区中!
In my case (Rails 5), I ended up adding these 2 lines in my
app/config/environments/development.rb
That's it! And to make sure that Melbourne was read correctly, I ran the command in my terminal:
bundle exec rake time:zones:all
and Melbourne was listing in the timezone I'm in!
如果要将全局时区设置为 UTC,可以在 Rails 4 中执行以下操作:
确保重新启动应用程序,否则您将看不到更改。
If you want to set the timezone to UTC globally, you can do the following in Rails 4:
Be sure to restart your application or you won't see the changes.
在 Ruby on Rails 6.0.1 中,转到 config > 区域设置 > application.rb 并添加以下内容:
您可以看到我用 2 行配置时区:
我希望它可以帮助那些使用 Ruby on Rails 6.0.1 的人
In Ruby on Rails 6.0.1 go to config > locales > application.rb and add the following:
You can see that I am configuring the time zone with 2 lines:
I hope it helps those who are working with Ruby on Rails 6.0.1
我必须将此块添加到我的
environment.rb
文件中,一切都很好:)Rails.application.initialize!
行之前 >I had to add this block to my
environment.rb
file and all was well :)Rails.application.initialize!
在 Rails 4.2.2 上,转到
application.rb
并使用config.time_zone='city'
(例如:'伦敦' 或 '布加勒斯特' 或 '阿姆斯特丹' 等在)。它应该工作得很好。这对我有用。
On rails 4.2.2, go to
application.rb
and useconfig.time_zone='city'
(e.g.:'London' or 'Bucharest' or 'Amsterdam' and so on).It should work just fine. It worked for me.
对于中国用户,只需在
config/application.rb
中添加以下两行:for Chinese user, just add two lines below to you
config/application.rb
:具体将其添加到您的环境文件中,例如:
Production.rb
:时区设置
Specifically adding it into your environment files such as:
Production.rb
:TimeZone Setting
如果您想设置当地时间,请在
application.rb
中添加以下文本,然后重新启动服务器
If you want local time to set, add the following text in
application.rb
Then restart your server