2017-10-19 52 views
0

我可以執行以下操作並獲取正確時區中的TimeWithZone對象。使用ActiveSupport :: TimeWithZone指定UTC偏移量而不是時區ActiveSupport :: TimeWithZone

Time.current.in_time_zone('Alaska') 
:> Thu, 19 Oct 2017 08:45:08 AKDT -08:00 

是否有in_time_zone等效的方法,我可以通過它在幾秒鐘的偏移UTC並取回與指定偏移TimeWithZone對象?

offset = -25200 # -25200 seconds == -08:00 
Time.current.in_utc_offset(offset) 
:> Thu, 19 Oct 2017 08:45:08 -08:00 

在此先感謝!

使用Rails 5.1.2 &紅寶石2.4.1

回答

1

是的,它是在香草紅寶石:#getlocal(sec)。它不會給你一個ActiveSupport::TimeWithZone,但它會給你Time,你可以格式化或做任何你想做的事情,包括使用ActiveSupport擴展。

2.4.1 :016 > Time.now.getlocal(3600) 
=> 2017-10-19 22:56:45 +0100 
2.4.1 :017 > Time.now.getlocal(-3600) 
=> 2017-10-19 20:56:48 -0100 

PS:-25200以秒爲單位:) -87小時:00將28800

+0

正是我一直在尋找,謝謝。我很難在文檔中找到它,但卻發現了這樣的情況。超級有用。 – theartofbeing

相關問題