[英]Ruby upgrade (from 1.8.7) on Rails 3.0.20 application causing time zone weirdness
我正在升級一個大的,舊的,笨重的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。 傷心。 我仍然希望聽到有關修復的任何建議。
嘗試使用I18n助手來格式化/抵消您的時間輸出 - 並強制時區(如果需要)
puts I18n.localize(Transaction.last.created_at, format: :long)
要么
Time.use_zone("Central Time (US & Canada)") do
puts I18n.localize(Transaction.last.created_at, format: :long)
end
我建議你將默認的Time.zone
設置為UTC,並根據需要明確地為每個用戶更新它 - 這是一篇可能有幫助的博客文章: http : //jessehouse.com/blog/2013/11/15/working -with-時區和-的Ruby-on-導軌/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.