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

前端 未结 13 2402
感情败类
感情败类 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:14

    I'm using Rails 2.0 before they added the code that makes weppos solution work. Here's what I did

    # Silly hack, because sometimes the input_date is in the wrong timezone
    temp = input_date.to_time.to_a
    temp[8] = true
    temp[9] = "Eastern Daylight Time"
    input_date = Time.local(*temp)
    

    I break the time down into a 10 element array, change the timezone and then convert the array back into a time.

提交回复
热议问题