views:

62

answers:

1

I set the local timezone in Rails with this javascript function in my layout:

<script type="text/javascript" charset="utf-8">
        <% unless session[:timezone_offset] %>
            $.ajax({
           url: '/main/timezone',
           type: 'GET',
           data: { offset: (new Date()).getTimezoneOffset() }
            });
        <% end %>
    </script>

where this is the receiving function:

# 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 => true
  end

And then I have the offset in my session, so I do something like this to show a time:

<%= (@product.created_at + session[:timezone_offset]).strftime("%m/%d/%Y %I:%M%p") + " #{ActiveSupport::TimeZone[session[:timezone_offset]]}" %>

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...

+1  A: 

You can set current time zone and it will be remembered for all operations. It can be done in a before_filter of some very high controller, like AppController. For example

class ApplicationController < ActionController::Base
  before_filter :set_zone_from_session

  private

  def set_zone_from_session
    # set TZ only if stored in session. If not set then the default from config is to be used
    # (it should be set to UTC)
    Time.zone = ActiveSupport::TimeZone[session[:timezone_offset]] if session[:timezone_offset]
  end

end

Probably it doesn't look better on the first sight - but it will influence all views so no need for any conversions there.

pawien
Only issue with this is then it saves in that Time Zone so now my database will have time saved in different time zones. Maybe that's not bad if it keeps track of the time zone... What do you think? Is this how it's normally done? Thanks!
Tony
It should not save with timezones - under normal circumstances. For example migrations for Postgres create time columns without zone info. Same for Sqlite. So there should be no info about time zone stored. I've got some wild idea to make a list of simple examples and working with tzones would be one of them. But not now as I have to make some work for living :-)
pawien
When I read it again I have a feeling that I wasn't clear enough - times are stored always in UTC regardless of your Time.zone settings.
pawien
Hmmm ok I'll try it again. I remembered it storing in the Time.zone when I changed it. Also if you ever have the urge to write a tutorial but no means to do it, you are welcome to guest post on my blog - http://tonyamoyal.com ...I get pretty solid traffic
Tony