Forum: Ruby on Rails How to present Rails form datetime select in different time zone?

92ee0fabd6fadd94879a27c56fc12f64?d=identicon&s=25 Andrew Havens (Guest)
on 2013-08-22 10:58
(Received via mailing list)
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-22 11:31
(Received via mailing list)
On 22 August 2013 00:59, Andrew Havens <andrewh@copiousinc.com> wrote:
> 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
92ee0fabd6fadd94879a27c56fc12f64?d=identicon&s=25 Andrew Havens (Guest)
on 2013-08-22 21:24
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.