问题描述
我的网站部署在 heroku 上.Time.now 将在今天返回,但记录的 created_at 字段(现在创建)将显示明天.我认为这与服务器时间有关?
My site is deployed on heroku. Time.now will return today, but the created_at field of a record (created right now) will say its tomorrow. I assume this has to do with server time?
有没有办法确保它们相同?最好的,艾略特
Is there a way to make sure they're the same?Best,Elliot
更新所以我做了这个heroku rake time:zones:us"
Update so I did this "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' 时,应用程序无法启动.有什么想法吗?
however, when I set config.time_zone = 'UTC -05:00' in my environment, the app fails to start. any ideas?
推荐答案
Rails 总是在数据库中存储 UTC 时间;created_at 字段本身应该正好被您的时区相对于 UTC 的变化所抵消.
Rails always stores UTC time on the database; the created_at field by itself should be offset by exactly your timezone's variation relative to UTC.
每当您在应用程序中加载记录时,字段都会转换为 environment.rb 中指定的时区.它可能是这样的:
Whenever you load a record in your application, the fields get converted to the timezone specified in environment.rb. It might have something like this:
config.time_zone = 'UTC'
为了将时间正确转换为您的时区,您可以将此配置设置更改为与您的实际时区相匹配的设置.例如:
For the time to be converted properly to your timezone, you might change this configuration setting to one matching your actual time zone. For instance:
config.time_zone = 'Central Time (US & Canada)'
要查看可用区域,请在您的 rails 目录中发出rake -D time".这将为您提供有关如何获取用于配置的时区名称的说明.
To see available zones, issue "rake -D time" on your rails directory. This will give you instructions on how to get time zone names for use in configuration.
这篇关于时间.现在&created_at 有什么不同?Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!