2017-10-19 53 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

相关问题