While every sysadmin worth his or her salt runs their server in the UTC timezone, it is occasionally useful, even necessary, to know the timezone that the end user's browser is set to. Surprisingly, there isn't an easy way to do this - you'd think that the browser might supply that in a header, but no… getting it is an exercise in plumbing together raw pieces of the internet. This gem gives that to your average rails app with a minimum of fuss.
How to use it
Its a rails engine. You install it via a gem, then include various bits.
add this to your Gemfile with:
then wire it up in your rails app:
add this line to your config/routes.rb file:
mount TzMagic::Engine => "/tz_magic"
add this line to your helpers/application_helper.rb file:
add this line to the controller that you need to ensure the timezone exists for (application_controller.rb is an acceptable, if brute-force option):
you now have a helper you can refer to in your controllers or views like:
<%= time_zone.name %>
This is a full fleged TimeZone instance.
How it works
by including the TzMagic::BeforeFilter into your controller, you are adding a check that the user's session contains a :timezone_name element. If that element exists, all is fine. If it doesn't exist:
we redirect the user to our /timezone/new view
this hits our timezone#create method, which sets the cookie and returns the original url the user was going to via a json block.
The usual. Fork, modify, test, pullup request.