Skip to content

Instantly share code, notes, and snippets.

@ianlevesque
Created December 20, 2009 01:38
Show Gist options
  • Save ianlevesque/260321 to your computer and use it in GitHub Desktop.
Save ianlevesque/260321 to your computer and use it in GitHub Desktop.
Automatically set the TimeZone in Rails >= 2.1
// detect time zone
Event.observe(window, 'load', function() {
var date = new Date();
date.setTime(date.getTime() + (1000*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
var offset = -(new Date().getTimezoneOffset() / 60);
document.cookie = "timezone=" + offset + expires + "; path=/";
});
class ApplicationController < ActionController::Base
before_filter :set_timezone
protected
def set_timezone
tz_off = cookies[:timezone]
Time.zone = ActiveSupport::TimeZone[tz_off.to_i] unless tz_off.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment