Rails 3 时区错误 [英] Rails 3 Timezone error
问题描述
我正在为 Rails 3 beta 中的时区支持而苦苦挣扎,我想知道这是一个错误还是我做错了什么.他是问题所在:
<前>>Time.zone = 'Madrid' # 现在是 GMT+2=>马德里">c = Comment.new=> #<评论id:nil,标题:",pub_at:nil>>c.pub_at = Time.zone.parse('10:00:00')=> 2010 年 5 月 31 日星期一 10:00:00 CEST +02:00>c.保存>C=> #<评论 id: 3, title: "", pub_at: "2010-05-31 08:00:00">>c.重新加载=> #<评论 id: 3, title: "", pub_at: "2010-05-31 08:00:00">ruby-1.8.7-p249 > c.pub_at=> 2010 年 5 月 31 日星期一 13:00:00 CEST +02:00如您所见,pub_at 属性已正确存储在数据库中,但在检索它时会增加 3 小时,我怀疑这是因为它使用的是 GMT-3 中的本地机器时区.
rails 2.3.5 中相同的命令序列可以完美运行.
有什么技巧吗?我应该举报一张罚单吗?
如果我没记错的话,所有日期都以 UTC 格式存储到数据库中.
rails 和 console 处理日期的方式不同!
因此您应该尝试通过将时区设置到您的 application.rb 中来在您的应用程序中测试它
config.local_zone = '马德里'
然后打印值!你应该会看到正确的时间.
希望对您有帮助
I am struggling with time zone support in Rails 3 beta and I would like to know if it is a bug or if I am doing something wrong. He is the problem:
> Time.zone = 'Madrid' # it is GMT+2 => "Madrid" > c = Comment.new => #<Comment id: nil, title: "", pub_at: nil> > c.pub_at = Time.zone.parse('10:00:00') => Mon, 31 May 2010 10:00:00 CEST +02:00 > c.save > c => #<Comment id: 3, title: "", pub_at: "2010-05-31 08:00:00"> > c.reload => #<Comment id: 3, title: "", pub_at: "2010-05-31 08:00:00"> ruby-1.8.7-p249 > c.pub_at => Mon, 31 May 2010 13:00:00 CEST +02:00
As you can see, the pub_at attribute is stored correctly in the database but when it is retrieved it adds 3 hours and I suspect that it is because it is using my local machine timezone that is in GMT-3.
The same sequence of commands in rails 2.3.5 works perfectly.
Any toughts? Should I report a ticket?
If i remember well, all the date are store into the database as UTC.
And rails and console doesn't handle the dates as the same way !
So you should try to test it within your application by setting the timezone into your application.rb
config.local_zone = 'Madrid'
And then print the value ! You sould see the correct time.
Hope this help
这篇关于Rails 3 时区错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!