奇怪的created_at行为

时间:2009-06-22 00:07:23

标签: ruby-on-rails datetime time utc

我在config.time_zone = 'UTC'中设置了environment.rb,但我仍然使用Rails的内置日期时间字段获得了一些奇怪的行为:

>> Time.now
=> Sun Jun 21 17:05:59 -0700 2009
>> Feedback.create(:body => "testing")
=> #<Feedback id: 23, body: "testing", email_address: nil, name: nil, created_at: "2009-06-22 00:06:09", updated_at: "2009-06-22 00:06:09">
>> Time.parse(Feedback.last.created_at.to_s)
=> Mon Jun 22 00:06:09 UTC 2009

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

看起来它正确地在ActiveRecord对象中设置时区,所以我认为你不必太担心。如果您想强制Rails的时间戳使用UTC,您可以使用Time.utc

Time.now.utc
=> Mon Jun 22 00:54:21 UTC 2009