可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
adding following to application.rb
works
config.time_zone = \'Eastern Time (US & Canada)\'
config.active_record.default_timezone = :local # Or :utc
回答2:
I have decided to compile this answer because all others seem to be incomplete.
config.active_record.default_timezone determines 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
If you want to change Rails timezone, but continue to have Active Record save in the database in UTC, use
# application.rb
config.time_zone = \'Eastern Time (US & Canada)\'
If you want to change Rails timezone AND have Active Record store times in this timezone, use
# 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.
Note
Do not forget to restart your Rails server after modifying application.rb
.
Remember that config.active_record.default_timezone
can take only two values
- :local (converts to the timezone defined in
config.time_zone
)
- :utc (converts to UTC)
Here\'s how you can find all available timezones
rake time:zones:all
回答3:
I came to the same conclusion as Dean Perry after much anguish. config.time_zone = \'Adelaide\'
and config.active_record.default_timezone = :local
was the winning combination. Here\'s what I found during the process.
回答4:
In my case (Rails 5), I ended up adding these 2 lines in my 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
and Melbourne was listing in the timezone I\'m in!
回答5:
If you want to set the timezone to UTC globally, you can do the following in 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.
回答6:
On rails 4.2.2, go to application.rb
and use config.time_zone=\'city\'
(e.g.:\'London\' or \'Bucharest\' or \'Amsterdam\' and so on).
It should work just fine. It worked for me.
回答7:
I had to add this block to my environment.rb
file and all was well :)
Rails.application.configure do
config.time_zone = \"Pacific Time (US & Canada)\"
config.active_record.default_timezone = :local
end
- I added it before the line
Rails.application.initialize!
回答8:
for Chinese user, just add two lines below to you config/application.rb
:
config.active_record.default_timezone = :local
config.time_zone = \'Beijing\'
回答9:
If you want local time to set, add the following text in application.rb
config.time_zone = \'Chennai\'
# WARNING: This changes the way times are stored in the database (not recommended)
config.active_record.default_timezone = \'Chennai\'
Then restart your server