如何更改 Rails 中 Active Record 的默认时区?

发布于 2024-11-09 06:01:22 字数 561 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

彩虹直至黑白 2024-11-16 06:01:22

我决定编译这个答案,因为所有其他答案似乎都不完整。

config.active_record.default_timezone 确定从数据库提取日期和时间时是使用 Time.local(如果设置为 :local)还是 Time.utc(如果设置为 :utc)。默认值为:utc。
http://guides.rubyonrails.org/configuring.html


如果您想更改Rails时区,但继续将活动记录UTC保存在数据库中,请使用

# application.rb
config.time_zone = 'Eastern Time (US & Canada)'

如果您想更改Rails时区ANDActive Record 在此时区存储时间,请使用

# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local

警告:在以非 UTC 格式将时间保存到数据库之前,您确实应该三思而后行,甚至三思而行。

注意
修改 application.rb 后,不要忘记重新启动 Rails 服务器。


请记住,config.active_record.default_timezone 只能采用两个值

  • :local(转换为 config.time_zone 中定义的时区)
  • : utc(转换为 UTC)

以下是查找所有可用时区的方法

rake time:zones:all

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
请别遗忘我 2024-11-16 06:01:22

将以下内容添加到 application.rb 有效

 config.time_zone = 'Eastern Time (US & Canada)'
 config.active_record.default_timezone = :local # Or :utc

adding following to application.rb works

 config.time_zone = 'Eastern Time (US & Canada)'
 config.active_record.default_timezone = :local # Or :utc
浅忆 2024-11-16 06:01:22

经过一番痛苦之后,我得出了与迪安·佩里相同的结论。 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' and config.active_record.default_timezone = :local was the winning combination. Here's what I found during the process.

画中仙 2024-11-16 06:01:22

就我而言(Rails 5),我最终在我的 app/config/environments/development.rb 中添加了这两行,

config.time_zone = "Melbourne"
config.active_record.default_timezone = :local

就是这样!为了确保正确读取 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

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!

稀香 2024-11-16 06:01:22

如果要将全局时区设置为 UTC,可以在 Rails 4 中执行以下操作:

# Inside config/application.rb
config.time_zone = "UTC"
config.active_record.default_timezone = :utc

确保重新启动应用程序,否则您将看不到更改。

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.

梦中楼上月下 2024-11-16 06:01:22

在 Ruby on Rails 6.0.1 中,转到 config > 区域设置 > application.rb 并添加以下内容:

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

您可以看到我用 2 行配置时区:

config.active_record.default_timezone =: local
config.time_zone = 'Lima'

我希望它可以帮助那些使用 Ruby on Rails 6.0.1 的人

In Ruby on Rails 6.0.1 go to config > locales > application.rb and add the following:

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:

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

再可℃爱ぅ一点好了 2024-11-16 06:01:22

我必须将此块添加到我的 environment.rb 文件中,一切都很好:)

Rails.application.configure do
    config.time_zone = "Pacific Time (US & Canada)"
    config.active_record.default_timezone = :local
end
  • 我将其添加到 Rails.application.initialize!之前 >

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!
稳稳的幸福 2024-11-16 06:01:22

在 Rails 4.2.2 上,转到 application.rb 并使用 config.time_zone='city' (例如:'伦敦' 或 '布加勒斯特' 或 '阿姆斯特丹' 等在)。

它应该工作得很好。这对我有用。

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.

著墨染雨君画夕 2024-11-16 06:01:22

对于中国用户,只需在 config/application.rb 中添加以下两行:

config.active_record.default_timezone = :local
config.time_zone = 'Beijing'

for Chinese user, just add two lines below to you config/application.rb :

config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
差↓一点笑了 2024-11-16 06:01:22

具体将其添加到您的环境文件中,例如:
Production.rb

时区设置
  config.time_zone = 'Asia/Dubai'

Specifically adding it into your environment files such as:
Production.rb:

TimeZone Setting
  config.time_zone = 'Asia/Dubai'
凌乱心跳 2024-11-16 06:01:22

如果您想设置当地时间,请在 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

然后重新启动服务器

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 = :local

Then restart your server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文