How do I change the zone offset for a time in Ruby on Rails?

前端 未结 13 2348
感情败类
感情败类 2020-12-30 22:03

I have a variable foo that contains a time, lets say 4pm today, but the zone offset is wrong, i.e. it is in the wrong time zone. How do I change the time zone?

When

13条回答
  •  迷失自我
    2020-12-30 22:30

    This is what I did, as I am not using Rails and don't want to use any non-core gems.

    t = Time.now # my local time - which is GMT
    zone_offset = 3600 # offset for CET - which is my target time zone
    zone_offset += 3600 if t.dst? # an extra hour offset in summer
    time_cet = Time.mktime(t.sec, t.min, t.hour, t.mday, t.mon, t.year, nil, nil, t.dst?, zone_offset)
    

提交回复
热议问题