ruby date gem utc offset

时间:2015-01-11 07:44:00

标签: ruby date utc

我在GMT中有一个unix时间字符串"1420960690"。我可以使用ruby“date”gem将其转换为可读文本,如下所示:

require 'date'
DateTime.strptime("1420960690", '%s')
# => #<DateTime: 2015-01-11T07:18:10+00:00 ((2457034j,26290s,0n),+0s,2299161j)>

日期以GMT显示,我需要MST。我不明白文档中UTC的偏移量。 http://ruby-doc.org/core-2.2.0/Time.html示例或正确方向的链接将不胜感激。

我正在寻找的输出是2015-01-10T00:18:10 + 00:00我只是不确定如何在不使用其他宝石的情况下得到答案。

2 个答案:

答案 0 :(得分:2)

显然,UTC(通常是GMT)的偏移量为0.这就是UTC的用途。

答案 1 :(得分:1)

我最终使用了。

DateTime.strptime("1420960690", '%s').to_time.utc - 25200
=> 2015-01-11 00:18:10 UTC

I got the off set by looking up MST from GMT which was 7 hours, so (7 * 60min) = 420min * 60sec = 25200 total seconds for the offset.