我正在升级一个大的、旧的、笨重的 Rails 应用程序的 Ruby(主要是这样我就不必在重新格式化的笔记本电脑上重新安装 REE),而且我被时区问题严重困扰。基本上,从数据库中提取日期时间并不能正确地将它们转换为本地时间:
新系统 - Ruby 2.1.2、Ubuntu 14.04
>> Time.zone.name
=> "Central Time (US & Canada)"
>> ActiveRecord::Base.time_zone_aware_attributes
=> true
>> ActiveRecord::Base.default_timezone
=> :utc
>> Transaction.last.created_at
=> 2014-07-15 02:09:02 UTC
>> Transaction.last.created_at_before_type_cast
=> 2014-07-15 02:09:02 UTC
>> Transaction.last.created_at.localtime
=> 2014-07-14 21:09:02 -0500
>> exit
$ date
Mon Jul 14 22:27:50 CDT 2014
旧系统 - REE,Ubuntu 12.04
>> Transaction.last.created_at
=> Mon, 14 Jul 2014 22:03:11 CDT -05:00
>> Transaction.last.created_at_before_type_cast
=> Tue Jul 15 03:03:11 UTC 2014
>> Transaction.last.created_at.localtime
=> Mon Jul 14 22:03:11 -0500 2014
如您所见,我确保time_zone_aware_attributes
已设置,已设置区域(我在 environment.rb 中设置),并且 ActiveRecord以UTC 格式存储时间(如预期的那样)。我对此感到很困惑。有人有想法么?
更新
before :all do
@current_tz = Time.zone
Time.zone = 'Pacific Time (US & Canada)'
end
after :all do
Time.zone = @current_tz
end
it 'should report time as Pacific time' do
shift = FactoryGirl.create(:shift)
new_obj = Transaction.create(shift: shift, time: DateTime.new(2013, 3, 31, 0, 0, 0, 0), amount: 0)
new_obj.reload
I18n.localize(new_obj.time, format: :timeonly).should_not == '12:00 am' #We created it with a UTC date, but it should get I18n'd to Pacific time
end
#en.yml
time:
formats:
timeonly: "%l:%M %p"
上面的测试也失败了。I18n 的东西似乎完全坏了。
更新 2
我似乎已将问题隔离到 1.9.3 -> 2.1。我想使用 1.9.3 很好,我想我会在新服务器上运行它,直到升级 Rails。伤心。我仍然很想听听有关修复的任何建议。