I'm trying to figure out how to set the timezone on a per request basis in Sinatra for a multithreaded application.
Rails provides the :around_action
filter to handle this wherein the request is processed inside of a Time.use_zone
block.
around_action :set_time_zone, if: :current_user
def set_time_zone(&block)
Time.use_zone(current_user.time_zone, &block)
end
Sinatra, however, only provides before and after filters:
before do
Time.zone = current_user.time_zone
end
after do
Time.zone = default_time_zone
end
That approach, however, does not seem threadsafe. What is the right way to accomplish this in Sinatra?