I live in timezone GMT+5.30. My rails application while saving the values for updated_at and created_at always saves the GMT time. How can i default my rails ap开发者_C百科plication to enter the time in GMT+5.30 time?
It took me a while to catch this one too ...
in your environement.rb file, there is a :
config.time_zone = "something"
change it to :
config.time_zone = "Eastern Time (US & Canada)"
for example, and the time displayed in your views will be automatically converted.
Rails just stores time like this by default. it is you locale that convert the time to the good timezone.
You should always store times in UTC in your database so that it is not geographically bound. You can then present the times in the user's local time, or a system default of whatever you want. to do that modify the following in your application_controller.rb For instance:
class ApplicationController < ActionController::Base
before_filter :set_user_time_zone
protected
def set_user_time_zone
Time.zone = 'New Delhi'
end
end
You can render all these times in local time:
Time.now.localtime.in_time_zone(Time.zone)
# => Thu, 25 Aug 2011 01:23:43 IST +05:30
You could probably clean that up with a helper method.
It looks like you will get your answer here providing you app is on Rails 3.
精彩评论