我有一个UTC的DateTime,我想添加一个以秒为单位的偏移量
#json parsed - utc offset in seconds - turned into integer
@utc_offset = result["UTCOffsetMillis"].to_i
#json parsed - utc date
start_date_string = result["startDate"].split("-")
start_date = DateTime.new(start_date_string[0].to_i, start_date_string[1].to_i,start_date_string[2].to_i)
如何将这个以秒为单位的偏移量添加到start_date??
轨道3.2.3Ruby 1.9.2p320
要在DateTime
中添加以秒为单位的偏移量,可以使用:
DateTime.new(2012, 10, 31) + 5.seconds
#=> Wed, 31 Oct 2012 00:00:05 +0000
要更改时区,我会使用Time
而不是DateTime
:
Time.new(2012, 10, 31, 0, 0, 0, 3600).localtime
#=> 2014-10-31 00:00:00 +0100
3600
是以秒为单位的UTC偏移量。