Created
March 3, 2010 18:35
-
-
Save bkeepers/320844 to your computer and use it in GitHub Desktop.
Get timezone offset from the browser and use it for timezones in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function() { | |
$.cookie('tz', (new Date()).getTimezoneOffset()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
before_filter :set_timezone | |
# … | |
private | |
def set_timezone | |
if current_user && browser_timezone && browser_timezone.name != current_user.time_zone | |
current_user.update_attributes :time_zone => browser_timezone.name | |
end | |
Time.zone = current_user ? current_user.time_zone : browser_timezone | |
end | |
def browser_timezone | |
@browser_timezone ||= begin | |
ActiveSupport::TimeZone[-cookies[:tz].to_i.minutes] | |
end if cookies[:tz].present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment