将 UTC 偏移量而不是时区指定为 ActiveSupport::TimeWithZone

Specify UTC Offset instead of timezone to ActiveSupport::TimeWithZone

使用 ActiveSupport::TimeWithZone 我可以执行以下操作并在正确的时区取回 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 & Ruby 2.4.1

是的,它是原版 Ruby:#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 是 7 小时秒:) -8:00 是 28800