Rails 还是 Rails 时间配置的问题

chucai · May 29, 2012 · Last by hick replied at August 16, 2013 · 3654 hits

运行环境 Rails 2.3.8 Ruby 1.8.7 Linux

>> a = Activity.last
=> #<Activity id: 247, name: "而让人", stime: "2012-05-29 15:20", circle_id: 1, created_at: "2012-05-29 15:24", updated_at: "2012-05-29 15:24", user_id: 5, comments_count: 0, status: 0, stop_time: nil, user_count: 0>
>> a.stime
=> Tue May 29 15:20:00 UTC 2012
>> i = a.stime.to_i
=> 1338304800
>> Time.at(i)
=> Tue May 29 23:20:00 +0800 2012

如上,是我在 console 中运行的代码 同一个时间,相互转换后的时间怎么会不一样呢? 如下是我的配置文件

config.time_zone = 'Beijing'
config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false # this is important!

两个时间一样的啊,只是时区不同,a.stime 是 UTC 的

#1 楼 @ShiningRay 恩,如何将本地时间保存如数据库呢? 我的配置有问题么?

config.active_record.default_timezone = :local

#2 楼 @chucai Rails 3 和 Rails 2 在这里的设置是有区别的,问题因该是出在这里,这个写法在 Rails 3 里奏效,Rails 2 里我记得不是这么写的。

哎,好乱啊... rails4 我最终还是 created_at.localtime.to_s(:db) 这样的搞定了!不要修改 config/application.rb。

You need to Sign in before reply, if you don't have an account, please Sign up first.