1
我希望時間和to_time具有反思性。Ruby String to_time壞了?
UPDATE
這就是答案,如果添加的參數:本地,它不會忽略偏移。我覺得很奇怪,它在默認情況下會忽略數據(偏移量),但...
Time.zone.now.to_s.to_time(:local) returns 2012-03-20 14:39:03 +0100
/UPDATE
> Time.zone.now.to_s
=> "2012-03-20 12:50:05 +0100"
> Time.zone.now.to_s.to_time.to_s
=> "2012-03-20 12:50:05 UTC"
# PROBLEM:
# UTC is +0000, meaning that there is 1 hour diff in the above
> Time.zone.now
=> Tue, 20 Mar 2012 12:51:32 CET +01:00
> Time.zone.now.to_time
=> Tue, 20 Mar 2012 12:51:32 CET +01:00
> Time.zone.now.to_json
=> "\"2012-03-20T12:50:36+01:00\""
> Time.zone.now.to_json.to_time
=> 2012-03-20 12:50:36 UTC
我有這個問題,JSON消息。無論如何,我不希望to_time實際改變時間。可以轉換爲UTC,但它應該相應地調整時間。有人知道這是一個已知問題還是「按設計」?
出色答卷! 'Time.zone.now.to_s.to_time(:local)'返回 '2012-03-20 14:39:03 + 0100'請添加到您的答案。不過,我發現默認行爲是錯誤的。使用默認值:utc它解析錯誤,它'添加'整個偏移量。 – oma 2012-03-20 13:40:09