【问题标题】:How to change default timezone for Active Record in Rails?如何更改 Rails 中 Active Record 的默认时区?
【发布时间】:2011-09-01 09:43:01
【问题描述】:

在我的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 设为美国东部标准时间。然而,当在数据库中创建记录时,看起来datetime 是以 UTC 格式存储的。

在上面的评论中,他们说

...并使 Active Record 自动转换到该区域...

我该怎么做,在哪里?

另外,我也将在 heroku 上部署它,我希望继续使用该设置

【问题讨论】:

  • 在 MySQL 中 datetime 是一种无时区类型。 IE。它可以在您想要的任何时区。如果您将其视为UTC,那很好。但请注意,如果有人直接查看您的数据库并对其进行不同的解释。

标签: ruby-on-rails ruby timezone rails-activerecord


【解决方案1】:

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

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


如果您想更改 Rails 时区,但要继续以 UTCActive Record 保存在数据库中,请使用

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

如果您想更改 Rails 时区 并且 在该时区有 Active 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

【讨论】:

  • 这似乎是最完整/准确的答案——@Omnipresent
  • 这应该是公认的答案。它更完整。
  • @Mihai-AndreiDinculescu 你认为将datetime 值在UTC 中存储在数据库中然后在前端 中更改它们是否更好b> 喜欢moment.js 或者您有什么建议可以在任何用户的正确时区显示这些数据?
  • @AlexVentura 是的,比我聪明的人很久以前就同意以 UTC 存储日期是最好的方法。
  • 是的,我明白了。但是您建议在 rails 视图中实现什么来为不同时区的任何用户显示正确的值?
【解决方案2】:

将关注添加到application.rb 作品

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

【讨论】:

  • 这对我有用:config.time_zone = 'London'config.active_record.default_timezone = :local
  • 您可以通过在rails c - ActiveSupport::TimeZone.all.map(&:name) 中运行它来查看所有时区
  • 非常感谢!我也一直在为此苦苦挣扎。我有一个继承的遗留 PHP 应用程序,它与同一个 mysql 数据库交互并始终存储为本地;更新它以使用 UTC 不是一种选择。我以前什么都没做:config.time_zone = 'Central Time (US & Canada)'config.active_record.default_timezone = 'Central Time (US & Canada)',但将其设置为 :local 符号就可以了。
  • rails 指南说config.active_record.default_timezone 应该只设置为:local:utc。我认为其他任何事情都会产生意想不到的结果。
  • 您可以通过在rake time:zones:all中运行来查看所有时区
【解决方案3】:

在痛苦万分之后,我得出了与 Dean Perry 相同的结论。 config.time_zone = 'Adelaide'config.active_record.default_timezone = :local 是获胜组合。在此过程中,这里是what I found

【讨论】:

  • 这是正确答案,default_timezone 只接受 :local 或 :utc
【解决方案4】:

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

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

就是这样!为了确保正确读取墨尔本,我在终端中运行了以下命令:

bundle exec rake time:zones:all

墨尔本在我所在的时区列出!

【讨论】:

  • 如果我希望每个用户都能看到他们自己时区的日期时间值怎么办?
  • @AlexVentura 当您向用户显示日期时间值时,将其转换为他们的时区。使数据库记录持久化是一种更好的做法
【解决方案5】:

如果你想全局设置时区为 UTC,你可以在 Rails 4 中进行如下操作:

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

请务必重新启动您的应用程序,否则您将看不到更改。

【讨论】:

    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      在 Ruby on Rails 6.0.1 中,转到 config > locales > 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 的人有所帮助

      【讨论】:

        【解决方案8】:

        在 rails 4.2.2 上,转到 application.rb 并使用 config.time_zone='city'(例如:'London' 或 'Bucharest' 或 'Amsterdam' 等)。

        它应该可以正常工作。它对我有用。

        【讨论】:

          【解决方案9】:

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

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

          【讨论】:

            【解决方案10】:

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

            然后重启你的服务器

            【讨论】:

            • default_timezone 接受两个选项之一.. :local 或 :utc.. 它不接受特定时区,所以这不起作用.. 你需要将 default_timezone 设置为 :local跨度>
            猜你喜欢
            • 1970-01-01
            • 2012-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-13
            • 1970-01-01
            • 2011-09-16
            相关资源
            最近更新 更多