繁体   English   中英

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

[英]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

暂无
暂无

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

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