简体   繁体   English

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

[英]Specify UTC Offset instead of timezone to ActiveSupport::TimeWithZone

With ActiveSupport::TimeWithZone I'm able to do the following and get back a TimeWithZone object in the correct timezone. 使用ActiveSupport :: TimeWithZone我可以执行以下操作,并在正确的时区中获取TimeWithZone对象。

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

Is there an equivalent method to in_time_zone where I can pass it the UTC Offset in seconds and get back a TimeWithZone object with the specified offset? 是否有与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

Thanks in advance! 提前致谢!

Using Rails 5.1.2 & Ruby 2.4.1 使用Rails 5.1.2和Ruby 2.4.1

Yes it is in vanilla Ruby: #getlocal(sec) . 是的,它在香草Ruby中: #getlocal(sec) It is not going to give you an ActiveSupport::TimeWithZone but it will give you the Time which you can format or do whatever you want with it, including using ActiveSupport extensions. 它不会为您提供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 is 7 hours in seconds :) -8:00 would be 28800 PS:-25200是7小时(以秒为单位):-8:00将是28800

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM