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
Cannot Install Ruby Gems - Zlib Error
How to Extend a Class from an Initializer and Have It Reload in Development Environment
Implicit Argument Passing of Super from Method Defined by Define_Method() Is Not Supported
How to Use Ruby to Write Individual Records to a Redshift Database
How to Pass Named Arguments to a Rake Task
How to Scroll a Web Page Using Watir
Argument Out of Range Rails 4 and Bootstrap3-Datetimepicker-Rails
What's the Best Way to Start a Background Process, That Can Get Accessed Later On
How to Refresh a Page with Turbolinks
Unwanted Form Parameters Being Appended to Pagination Links
Working with a Large Data Object Between Ruby Processes
Rails Elasticsearch Aggregation
How to Create Pretty JSON in Chef (Ruby)