How to get Rails to interpret a time as being in a specific time zone?

前端 未结 2 526
死守一世寂寞
死守一世寂寞 2021-01-17 00:58

In Ruby 1.8.7, how to set the time zone of a time?

In the following examples, my system time zone is PST (-8:00 hours from UTC)

Given a time (21 Feb 2011

2条回答
  •  死守一世寂寞
    2021-01-17 02:04

    In ruby 1.8.7 it doesn't appear to be very easy to do what are asking for according to the documentation:

    http://www.ruby-doc.org/core-1.8.7/classes/Time.html

    However in 1.9 it looks a lot easier by passing the timezone offset to the localtime() method on a Time object:

    http://www.ruby-doc.org/core/classes/Time.html#M000346

    UPDATE

    The offset for Time.zone is easy since its an object on its own: (This is in a Rails console)

    ruby-1.8.7-p248 :001 > Time.zone
     => #, @utc_offset=nil> 
    ruby-1.8.7-p248 :002 > Time.zone.utc_offset
     => -21600 
    ruby-1.8.7-p248 :003 > Time.zone.formatted_offset
     => "-06:00" 
    

提交回复
热议问题