Andrew Havens
2013-Aug-21 23:59 UTC
How to present Rails form datetime select in different time zone?
Hello, 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? Thanks for your help! --Andrew -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6f9ce624-7b5a-4b96-b745-51e48a793d77%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Aug-22 09:29 UTC
Re: How to present Rails form datetime select in different time zone?
On 22 August 2013 00:59, Andrew Havens <andrewh-k20JcXJavzGBUy7/sJONFg@public.gmane.org> wrote:> Hello, > > 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? Thanks for your help!You could adjust the values in the controller prior to saving the data. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtxmvfgevyNsn0cS7%2B%2BTB%2BH8VdGfcynaZ2UJMcWpikgZA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Andrew Havens
2013-Aug-22 19:23 UTC
Re: How to present Rails form datetime select in different time zone?
I found a way to do it. 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)) endend 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 toTime.use_zone: Time.use_zone("Pacific Time (US & Canada)") do f.datetime_select :start_dateend -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ed3b8591-9e0a-4fa9-b761-e3d5619657a1%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.