Created
February 3, 2011 16:59
-
-
Save BMorearty/809775 to your computer and use it in GitHub Desktop.
How to reset the timezone after handling a request.
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
around_filter :set_time_zone | |
def set_time_zone | |
old_time_zone = Time.zone | |
Time.zone = current_user.time_zone if logged_in? | |
yield | |
ensure | |
Time.zone = old_time_zone | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use around_filter instead of before_filter to set the time zone in a Rails action. That way the thread-local time zone will be reset on the way out of handling this request.
See my comment in http://railscasts.com/episodes/106-time-zones-in-rails-2-1
You might be tempted to combine the first two lines of set_time_zone into one line with the comma assignment syntax and add "if logged_in?" to the one line in the ensure clause. Don't do it. If you did that, the user's time zone would leak out to the next request when the user logs out.