I set the local time zone in Rails using 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 is the receiving function:
def timezone
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 an offset in my session, so I'm doing something like this to show the time:
<%= (@product.created_at + session[:timezone_offset]).strftime("%m/%d/%Y %I:%M%p") + " #{ActiveSupport::TimeZone[session[:timezone_offset]]}" %>
Is all this really necessary in Rails 3? I think the first two blocks of code may be, but the third seems a bit excessive ...
source
share