2010-10-23 40 views
3
> e = Event.first 
> e.registration_start_utc #registration_start_utc is a datetime column 
=> Sat, 23 Oct 2010 06:38:00 UTC +00:00 
> e.registration_start_utc.utc? 
=> true 
> ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(e.registration_start_utc) 
=> Sat, 23 Oct 2010 02:38:00 UTC +00:00 

2問題有關此:導軌utc_to_local和夏令時間

1)這是爲什麼表示 「UTC」 最後輸出 - 小時得到轉換(6 => 2),但它仍表示UTC 。爲什麼不是EST/EDT?

2)夏令時切換後紐約的偏移從-4變爲-5會發生什麼?數據庫中的值不會改變,所以我唯一的結論是,我的應用程序將開始在任何地方顯示「1:38」而不是正確的2:38?

我最關心的是#2。 #1更多的是好奇心。

謝謝!

+0

參見[這裏](http://rubyforge.org/pipermail/tzinfo - 用戶/ 2007年9月/ 000015.html)由菲爾推理 – Zabba 2011-03-14 15:41:54

回答

1

2)utc_to_local使用日期來確定哪個偏移量是正確的,因此對於給定的日期,輸出總是相同的。

可以測試,像這樣:

t = Time.utc(2011,3, 14, 12) 
# => 2011-03-14 12:00:00 UTC 
t2 = Time.utc(2011,3, 11, 12) 
# => 2011-03-11 12:00:00 UTC 
ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t) 
# => 2011-03-14 08:00:00 UTC 
ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t2) 
# => 2011-03-14 07:00:00 UTC 

1)它似乎沒有權利我滿意。我的猜測是,他們只對小時,分鐘等的實際值感興趣,而忽視時區。

在任何情況下,你可能會更好用:

e.registration_start_utc.in_time_zone("Eastern Time (US & Canada)") 

又見this question我剛纔問...