top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to present Rails form datetime select in different time zone?

+1 vote
1,071 views

I have a Rails application with a default time zone of UTC. This works fine for almost everything. Now I have a situation where I would like to present the values in a datetime_select offset by a specific time zone. As far as I know, this helper does not have an option to specify the time zone that is used when persisting this data. When the form is submitted, it only sends the year, month, day, hour, and minute fields. ActiveRecord is responsible for converting these values into the time zone configured for the application. In this case, I want to change the time zone for only these fields so that it is localized to the user (but still store the correct UTC time). How can I make this change?

posted Aug 22, 2013 by Sonu Jindal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Here's how you can allow the user to set a date using a specific time zone:

To convert the multi-parameter attributes that are submitted in the form to a specific time zone, add a method in your controller to manually convert the params into a datetime object. I chose to add this to the controller because I did not want to affect the model behavior. You should still be able to set a date on the model and assume your date was set correctly.

def create
  convert_datetimes_to_pdt("start_date")
  convert_datetimes_to_pdt("end_date")
  @model = MyModel.new(params[:my_model])
  # ...
end

def update
  convert_datetimes_to_pdt("start_date")
  convert_datetimes_to_pdt("end_date")
  # ...
end

def convert_datetimes_to_pdt(field)
  datetime = (1..5).collect {|num| params['my_model'].delete "#{field}(#{num}i)" }
  if datetime[0] and datetime[1] and datetime[2] # only if a date has been set
    params['my_model'][field] = Time.find_zone!("Pacific Time (US & Canada)").local(*datetime.map(&:to_i))
  end
end

Now the datetime will be adjusted to the correct time zone. However, when the user goes to edit the time, the form fields will still display the time in UTC. To fix this, we can wrap the fields in a call to Time.use_zone:

Time.use_zone("Pacific Time (US & Canada)") do
  f.datetime_select :start_date
end
answer Aug 22, 2013 by Garima Jain
+1 vote

You could adjust the values in the controller prior to saving the data.

answer Aug 22, 2013 by Sheetal Chauhan
Similar Questions
+2 votes

How to convert utc time to a time in a particular time zone. As I need to display local time of Timezone(-05:00).

+1 vote

I would like to convert time zone abbreviation to full time zone in Python.

I'm currently writing dict with this information : http://www.timeanddate.com/library/abbreviations/timezones/

Questions :
* are there the same list somewhere (I didn't found in pytz) ?
* is it possible to append this list in pytz or in standard python date module?

+1 vote

During a LTE attach, MME may send the UE a EMM Information message with Universal time and local time zone IE. This IE contains Year, month, day, hour, min, sec.
My problem is that I can not find any reference yet about what should be the value of the year. Is it calculated from 1900 or 2000 or as it is ? or there is some other reference year ?
Any reference spec with this particular information will be highly helpful.

+2 votes

I have a table with several thousand records in it. They take a long time to load and are essentially useless presented altogether.. I'd like present the user a search form to select a limited subset of records for display in the index function. What options are available to solve this problem and where might I look for examples or demos?

+1 vote

I have just started with rails. I am making a attendance web app. I want to insert student attendance into attendances table and unable to do it as the form just inserts only last entry of the form. Student table and Attendances table have associations (has_many,belongs_to).

Please let me know how the form should be and controller API should look like.

...