Timezone support in Rails 2.1+

3 Mar 2009, by Michael Cindrić

Setting up Timezone support in the past was a pain, but now with rails 2.1 and above it's easy and done in minutes. Here is what you need to do

# config/environment.rb
config.time_zone = "Pacific Time (US & Canada)"

# controllers/application.rb
before_filter :set_user_time_zone

private
def set_user_time_zone
  Time.zone = current_user.time_zone if logged_in?
end

# Add to field to user model
add_column :users, :time_zone, :string

Once you have this in place then to user the users timezone in your app its just something like this

# To use users timezone rather then server time
  current_user.time_zone.today


Cookies help us deliver our services. By using our services, you agree to our use of cookies.