如何让 Rails 3 将数据库中的时间解释为太平洋格式?
这让我很困惑。我的 MySQL 数据库服务器将时间存储在“SYSTEM”中,即 PDT,我的应用程序服务器也是如此。但我不知道如何让 ActiveRecord 将来自我的数据库的日期时间解释为已经采用 PDT 格式。但如果我设置:
config.time_zone = "Pacific Time (US & Canada)"
那么数据库时间值为:
2011-06-09 03:19:57
然后在 Rails 控制台中我最终得到:
Wed, 08 Jun 2011 20:19:57 PDT -07:00
它减去了 7 小时!这告诉我它将传入时间解释为 UTC 而不是太平洋时间。
那么如何让 Rails 在正确的时区解释数据库日期呢?
This is pretty confusing for me. My MySQL database server stores times in "SYSTEM", which is PDT, as does my application server. But I can't figure out how to make ActiveRecord interpret datetimes coming from my database as already being in PDT format. But if I set:
config.time_zone = "Pacific Time (US & Canada)"
then a database time value of:
2011-06-09 03:19:57
then in the Rails console I end up with:
Wed, 08 Jun 2011 20:19:57 PDT -07:00
It subtracted 7 hours! That tells me it is interpreting the incoming time as UTC instead of Pacific.
So how do I get Rails to interpret database dates in the right time zone?
我找到了答案。在environment.rb中,我添加:
由于某种原因,它默认为:utc。文档说它默认为 :utc,但我的应用程序不是。 Rails 3.0.8 仅供参考。
I found the answer. In environment.rb, I added:
For some reason, it was defaulting to :utc. The docs say it defaults to :utc, but my application wasn't. Rails 3.0.8 FYI.