title: How do I convert a time to users time zone in Rails
body: <p>I set the local timezone in Rails with this JavaScript function in my layout:</p>
  
  <pre><code>&lt;script type="text/javascript" charset="utf-8"&gt;
      &lt;% unless session[:timezone_offset] %&gt;
          $.ajax({
                  url: '/main/timezone',
                  type: 'GET',
                  data: { offset: (new Date()).getTimezoneOffset() }
          });
      &lt;% end %&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>where this is the receiving function:</p>
  
  <pre><code># GET /main/timezone                                                     AJAX
    #----------------------------------------------------------------------------
    def timezone
      #
      # (new Date()).getTimezoneOffset() in JavaScript returns (UTC - localtime) in
      # minutes, while ActiveSupport::TimeZone expects (localtime - UTC) in seconds.
      #
      if params[:offset]
        session[:timezone_offset] = params[:offset].to_i * -60
        ActiveSupport::TimeZone[session[:timezone_offset]]
      end
      render :nothing =&gt; true
    end
  </code></pre>
  
  <p>And then I have the offset in my session, so I do something like this to show a time:</p>
  
  <pre><code>&lt;%= (@product.created_at + session[:timezone_offset]).strftime("%m/%d/%Y %I:%M%p") + " #{ActiveSupport::TimeZone[session[:timezone_offset]]}" %&gt;
  </code></pre>
  
  <p>Is all of this really necessary in Rails 3?  I think the first two code blocks may be, but the third seems a bit excessive...</p>