Ruby-on-rails 如何在 Rails 中更改 Active Record 的默认时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6118779/
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
How to change default timezone for Active Record in Rails?
提问by Omnipresent
In my application.rbI came across the following comment
在我的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)'
As you see from above, I've made config.time_zoneto EST time. However, still when records are created in the DB, it looks like datetimeis being stored in UTC format.
正如你从上面看到的,我已经config.time_zone到了美国东部时间。但是,仍然在数据库中创建记录时,它看起来像是datetime以 UTC 格式存储。
In the above comment, they say
在上面的评论中,他们说
...and make Active Record auto-convert to this zone...
...并使 Active Record 自动转换到该区域...
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
另外,我也将在 heroku 上部署它,我希望该设置能够延续
回答by Mihai-Andrei Dinculescu
I have decided to compile this answer because all others seem to be incomplete.
我决定编译这个答案,因为所有其他答案似乎都不完整。
config.active_record.default_timezonedetermines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :utc. http://guides.rubyonrails.org/configuring.html
config.active_record.default_timezone决定在从数据库中提取日期和时间时是使用 Time.local(如果设置为 :local)还是 Time.utc(如果设置为 :utc)。默认为:utc。 http://guides.rubyonrails.org/configuring.html
If you want to change Railstimezone, but continue to have Active Recordsave in the database in UTC, use
如果您想更改Rails时区,但继续将Active Record以UTC格式保存在数据库中,请使用
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
If you want to change Railstimezone ANDhave Active Recordstore times in this timezone, use
如果你想改变Rails的时区和有活动记录在这个时区,使用储存时间
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
Warning: you really should think twice, even thrice, before saving times in the database in a non-UTC format.
警告:在以非 UTC 格式在数据库中保存时间之前,您确实应该三思而后行。
Note
Do not forget to restart your Rails server after modifyingapplication.rb.
注意
修改后不要忘记重新启动 Rails 服务器application.rb。
Remember that config.active_record.default_timezonecan take only two values
请记住,config.active_record.default_timezone只能取两个值
- :local(converts to the timezone defined in
config.time_zone) - :utc(converts to UTC)
- :local(转换为 中定义的时区
config.time_zone) - :utc(转换为UTC)
Here's how you can find all available timezones
以下是查找所有可用时区的方法
rake time:zones:all
回答by Omnipresent
adding following to application.rbworks
添加以下application.rb作品
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local # Or :utc
回答by James Barona
I came to the same conclusion as Dean Perry after much anguish. config.time_zone = 'Adelaide'and config.active_record.default_timezone = :localwas the winning combination. Here's what I foundduring the process.
在经历了很多痛苦之后,我得出了与 Dean Perry 相同的结论。 config.time_zone = 'Adelaide'并且config.active_record.default_timezone = :local是获胜组合。这是我在这个过程中发现的。
回答by Carole
In my case (Rails 5), I ended up adding these 2 lines in my app/config/environments/development.rb
在我的情况下(Rails 5),我最终在我的 app/config/environments/development.rb
config.time_zone = "Melbourne"
config.active_record.default_timezone = :local
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
bundle exec rake time:zones:all
and Melbourne was listing in the timezone I'm in!
墨尔本在我所在的时区上市!
回答by sergserg
If you want to set the timezone to UTC globally, you can do the following in Rails 4:
如果要将时区全局设置为 UTC,可以在 Rails 4 中执行以下操作:
# Inside config/application.rb
config.time_zone = "UTC"
config.active_record.default_timezone = :utc
Be sure to restart your application or you won't see the changes.
请务必重新启动您的应用程序,否则您将看不到更改。
回答by Andrei Laslo
On rails 4.2.2, go to application.rband use config.time_zone='city'(e.g.:'London' or 'Bucharest' or 'Amsterdam' and so on).
在 Rails 4.2.2 上,转到application.rb并使用config.time_zone='city'(例如:'London' 或 'Bucharest' 或 'Amsterdam' 等)。
It should work just fine. It worked for me.
它应该工作得很好。它对我有用。
回答by Taylor A. Leach
I had to add this block to my environment.rbfile and all was well :)
我不得不将此块添加到我的environment.rb文件中,一切都很好:)
Rails.application.configure do
config.time_zone = "Pacific Time (US & Canada)"
config.active_record.default_timezone = :local
end
- I added it beforethe line
Rails.application.initialize!
- 我在行之前添加了它
Rails.application.initialize!
回答by u445908
for Chinese user, just add two lines below to you config/application.rb:
对于 CN 用户,只需在下面添加两行config/application.rb:
config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
回答by Juan Castro Lurita
In Ruby on Rails 6.0.1 go to config> locales> application.rby agrega lo siguiente:
在 Ruby on Rails 6.0.1 中,转到config> locales> application.rby agrega lo siguiente:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CrudRubyOnRails6
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
config.active_record.default_timezone = :local
config.time_zone = 'Lima'
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
end
end
You can see that I am configuring the time zone with 2 lines:
您可以看到我正在用 2 行配置时区:
config.active_record.default_timezone =: local
config.time_zone = 'Lima'
I hope it helps those who are working with Ruby on Rails 6.0.1
我希望它可以帮助那些使用 Ruby on Rails 6.0.1 的人
回答by Anand
If you want local time to set, add the following text in application.rb
如果要设置本地时间,请在 application.rb
config.time_zone = 'Chennai'
# WARNING: This changes the way times are stored in the database (not recommended)
config.active_record.default_timezone = :local
Then restart your server
然后重启你的服务器

