Does Anyone Know How to Appropriately Deal with User Timezones in Rails 2.3

Timezones in Rails

In the database you need to store the time in UTC and than for UI you need to convert it in the User's timezone.

Example in rails console:

Time.zone = "New Delhi"         #=> "New Delhi"

Time.now # => 2011-01-15 16:45:18 -0600

Time.now.in_time_zone # => Sun, 16 Jan 2011 04:15:26 IST +05:30

Correctly storing dates with TimeZone in MySQL DB for a Rails application

This blog post talks about native Time zone handling in Rails 2.1+:

http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/

The gist is that Rails will store all records in the DB in UTC and then convert to the users timezone for display. Which makes sense: store all data in a neutral form, and then convert it to the desired form at the last minute.

Setting per request Time.zone based on a model instance and keeping DRY

I think your issue has to do with the ordering that the filters are firing, i.e. the around_filter is running before your filter that sets the @event variable

I personally would do this per controller as needed, your application will eventually have many controller/actions that have nothing to do with events.

  • Setup user based timezone setting/handling which is handled in the application controller
  • Set the timezone based on @event as needed to override user setting

You may be interested in this blog post I did on rails timezones (see also the example code on github) - http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/

Timezone conversion not happening when displaying information on form field

As per comments:

Timezone for a field has a great answer : Rails 'config.time_zone' doesn't apply to datetime data loaded into a text_field. Fix?


<%= f.text_field :doc_date, value: f.object.doc_date %>

Rails 2.3.5: Is alternative to not use Time.zone= to transform time to local exist?

You can change the zone of any time like this:

t = Time.now                # gets local time
t.in_time_zone("UTC") # converts to UTC
t.in_time_zone("Auckland") # converts to Auckland time


Related Topics



Leave a reply



Submit