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

时间:2022-10-28 20:59:25

I am using Ruby 2.1.2 and Rails 4.1.4

我使用的是Ruby 2.1.2和Rails 4.1.4

tl;dr; I am getting a date & time from the user that comes into the controller from a form (via params) like this:

TL;博士;我从一个表格(通过params)进入控制器的用户得到一个日期和时间,如下所示:

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

and I need to make sure it is in the future so that customers can't create appointments in the past, and I have the customer's time_zone stored in the database because customers may be in different places.

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

Detail:

I am getting a time in my controller via a form:

我通过表单在控制器中得到一些时间:

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

I then convert it to a DateTime so that I can build an appointment record:

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

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

My Appointment model has a validation that ensures that you cannot create an appointment in the past:

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

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

I would like to take into account the customer's time_zone (@car.garage.time_zone), which is stored in the database, when building the appointment, e.g.

我想在构建约会时考虑客户的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)"

Right now this is not working. When I create an appointment for 1 minute in the future, I get the following:

现在这不起作用。当我在将来创建约会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

which causes the validation to fail, despite the fact that the appointment was created in the future and should therefore succeed.

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

Any guidance on how to implement this correctly would be much appreciated!

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

1 个解决方案

#1


0  

Use in_time_zone.

See https://www.reinteractive.net/posts/168-dealing-with-timezones-effectively-in-rails & http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails for good advice on working with timezones in rails.

请参阅https://www.reinteractive.net/posts/168-dealing-with-timezones-effectively-in-rails&http://www.elabs.se/blog/36-working-with-time-zones-in -ruby-on-rail,提供有关在轨道中使用时区的良好建议。

#1


0  

Use in_time_zone.

See https://www.reinteractive.net/posts/168-dealing-with-timezones-effectively-in-rails & http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails for good advice on working with timezones in rails.

请参阅https://www.reinteractive.net/posts/168-dealing-with-timezones-effectively-in-rails&http://www.elabs.se/blog/36-working-with-time-zones-in -ruby-on-rail,提供有关在轨道中使用时区的良好建议。