将时间与轨道中的时区进行比较

时间:2015-01-07 00:28:57

标签: ruby-on-rails ruby datetime timezone bootstrap-datetimepicker

我正在使用Ruby 2.1.2和Rails 4.1.4

tl; dr; 我正在约会&进入控制器的用户从表格(通过参数)进入控制器的时间如下:

pickuptime = params[:appointment][:pickuptime]
(byebug) pickuptime
"01/06/2015 7:26 PM"

我需要确保它在将来,以便客户过去不能创建约会,并且我将客户的time_zone存储在数据库中,因为客户可能位于不同的地方。

详情:

我通过表格在控制器中抽出时间:

pickuptime = params[:appointment][:pickuptime]
(byebug) pickuptime
"01/06/2015 7:26 PM"

然后我将其转换为DateTime,以便我可以建立约会记录:

pickuptime = DateTime.strptime(pickuptime, "%m/%d/%Y %l:%M %p")
@appointment = @car.appointments.build(garage_id: garage_id, pickuptime: pickuptime)

我的Appointment模型有一个验证,确保您不能在过去创建约会:

def pickuptime_is_in_future
  if pickuptime < Time.current
    errors.add(:pickuptime, "Appointment must be in the future")
  end
end

我想在构建约会时考虑客户的time_zone(@car.garage.time_zone),该时间存储在数据库中,例如

pry(main)> Garage.first.time_zone
Garage Load (0.6ms)  SELECT  "garages".* FROM "garages"   ORDER BY "garages"."id" ASC LIMIT 1
=> "Eastern Time (US & Canada)"

现在这不起作用。当我将来创建约会1分钟时,我会得到以下结果:

(byebug) pickuptime
Tue, 06 Jan 2015 19:22:00 UTC +00:00
(byebug) Time.current
Wed, 07 Jan 2015 00:21:53 UTC +00:00

导致验证失败,尽管约会是在未来创建的,因此应该成功。

非常感谢任何关于如何正确实现这一点的指导!