【问题标题】:Time.now & Created_at are different? Ruby on RailsTime.now 和 Created_at 不同吗? Ruby on Rails
【发布时间】:2010-03-25 00:47:42
【问题描述】:

我的网站部署在 heroku 上。 Time.now 今天将返回,但记录的 created_at 字段(现在创建)将在明天返回。我认为这与服务器时间有关?

有没有办法确保它们相同? 最好的, 艾略特


更新所以我做了这个“heroku rake time:zones:us”

它给了我:

    * UTC -10:00 *
Hawaii

* UTC -09:00 *
Alaska

* UTC -08:00 *
Pacific Time (US & Canada)

* UTC -07:00 *
Arizona
Mountain Time (US & Canada)

* UTC -06:00 *
Central Time (US & Canada)

* UTC -05:00 *
Eastern Time (US & Canada)
Indiana (East)

但是,当我在我的环境中设置 config.time_zone = 'UTC -05:00' 时,应用程序无法启动。有什么想法吗?

【问题讨论】:

  • 您需要使用“东部时间(美国和加拿大)”或“印第安纳州(东部)”来设置时区,因为它们是 UTC -05:00 的名称。此外,最好在设置 config.time_zone 时调用 Time.current 而不是 Time.now。
  • 嗨:时区的名称出现在 UTC 规范之下。例如,使用: config.time_zone = 'Indiana (East)' 正如 Corey 所说,使用 Time.current(或 Time.zone.now)将产生根据您配置的时区调整的时间。一般来说,您可以使用您习惯的任何 Time 方法,但您应该在 Time.zone 上调用它们,这会考虑您的时区。这当然是与 Rails 相关的;如果没有 ActiveSupport,它将无法在普通 Ruby 上运行。

标签: ruby-on-rails


【解决方案1】:

Rails 始终将 UTC 时间存储在数据库中; created_at 字段本身应该被您的时区相对于 UTC 的变化所抵消。

每当您在应用程序中加载记录时,字段都会转换为 environment.rb 中指定的时区。它可能有这样的东西:

config.time_zone = 'UTC'

为了将时间正确转换为您的时区,您可以将此配置设置更改为与您的实际时区匹配的设置。例如:

config.time_zone = 'Central Time (US & Canada)'

要查看可用区域,请在您的 rails 目录上发出“rake -D time”。这将为您提供有关如何获取时区名称以用于配置的说明。

【讨论】:

  • 您好 Roadmaster,感谢您的回复,我试了一下并收到了更新的错误
  • 我相信您需要在 application.rb 而不是 environment.rb 中执行此操作
【解决方案2】:

为了补充 Roadmaster 的答案,我遇到了类似的挑战:正常的 Rails 时间戳是根据 UTC 存储在数据库中的,但我需要根据本地查询以查找 今天创建的所有记录时区。

查询如下所示:

completions.where("created_at BETWEEN ? AND ?", 
  Date.today, Date.today + 1.day).count >= 1

我通过在日期调用#to_time 解决了这个问题,如下所示。这会将它们转换为具有正确时区的时间戳,并在数据库中获取正确的记录,从而有效地使查询时区感知。

completions.where("created_at BETWEEN ? AND ?", 
  Date.today.to_time, Date.today.to_time + 1.day).count >= 1

【讨论】:

    【解决方案3】:

    只需要取消注释并更改为您想要的时区。

    如果你想查看所有的时区,运行rake time:zones:all会输出一个列表。

    config/Application.rb
    
    module Clerk
      class Application < Rails::Application
        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.
    
        # 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 = 'La Paz'
    
        # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
        # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
        # config.i18n.default_locale = :de
      end
    end
    

    【讨论】:

      【解决方案4】:

      我在代码行中添加了以下内容。我希望 created_atActiveRecord 数据创建中。

      config/Application.rb
      
      module RailsApp
        class Application < Rails::Application
      
          config.time_zone = 'Pacific Time (US & Canada)'
          config.active_record.default_timezone = 'Pacific Time (US & Canada)'
      
      
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        • 1970-01-01
        • 2017-12-08
        • 1970-01-01
        • 2020-02-01
        • 2012-01-09
        相关资源
        最近更新 更多