Timezone in Rails

I have an application in which I want to allow the user to set time zones. When the user creates a reminder record, the record will be stored in db in UTC. But when he opens the calendar entry in the application, he should see the entry in the time zone of your choice. I use Chronic, but this is immaterial. In mine ApplicationController.rb, I have the following line:

before_filter :set_user_time_zone
...
def set_user_time_zone
    Time.zone = current_user.timezone if user_signed_in?
end

After that, the entries are saved in UTC, but with a time difference. For example, I set the time zone to Delhi, which is +530 from UTC. When I want to save the calendar entry for "Jan, 16 - 12:15 AM" - the database is full "2011-01-15 18:45:19 UTC" - so I want to save it 5:30 hours before the actual time.
In the user interface, I also see this entry instead of the time displayed in the current time zone of the user.

I want to know how to properly handle such time zone functions in rails.

+5
source share
1 answer

UTC, , User.

rails:

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
+8

All Articles