Skip to content

Instantly share code, notes, and snippets.

@BMorearty
Created February 3, 2011 16:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BMorearty/809775 to your computer and use it in GitHub Desktop.
Save BMorearty/809775 to your computer and use it in GitHub Desktop.
How to reset the timezone after handling a request.
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
@BMorearty
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment