tandusrl / acts_as_bookable

A reservation plugin for Rails applications that allows resources to be bookable.
MIT License
140 stars 103 forks source link

TimeZone problem with check_availability #23

Closed chrisedington closed 7 years ago

chrisedington commented 7 years ago

Hi,

Having an interesting problem which I cannot solve, would appreciate any feedback:

[1] pry(#<#<Class:0x007fd082dcde80>>)> slots = date.schedule.occurrences_between(date.schedule.start_time,date.schedule.end_time + 23.hour)
=> [2017-03-01 09:00:00 +0200, 2017-03-01 10:00:00 +0200, 2017-03-01 11:00:00 +0200, 2017-03-01 12:00:00 +0200, 2017-03-01 15:00:00 +0200, 2017-03-01 16:00:00 +0200, 2017-03-01 17:00:00 +0200]

Above, I get the available slots for a day, and they are returned in the correct timezone and all looks good. Further info on these "slots":

[3] pry(#<#<Class:0x007fd082dcde80>>)> slots.last
=> 2017-03-01 17:00:00 +0200
[4] pry(#<#<Class:0x007fd082dcde80>>)> slots.last.zone
=> "SAST"
[5] pry(#<#<Class:0x007fd082dcde80>>)> 

I then do an availability check on one of the slots, and I see the below. Note the line Datetime('2017-03-01T15:00:00Z')) - this is 2 hours BEHIND the slot.last timezone it showed above.

[2] pry(#<#<Class:0x007fd082dcde80>>)> date.check_availability(time: slots.last, amount: 1)
   (0.1ms)  SELECT COUNT(*) FROM "acts_as_bookable_bookings" WHERE "acts_as_bookable_bookings"."bookable_id" = ? AND (Datetime(time) = Datetime('2017-03-01T15:00:00Z'))  [["bookable_id", 17]]
=> true
[3] pry(#<#<Class:0x007fd082dcde80>>)> 

Some further examination of timezones in use are:

[5] pry(#<#<Class:0x007fd082dcde80>>)> Time.now
=> 2017-03-01 11:22:44 +0200
[6] pry(#<#<Class:0x007fd082dcde80>>)> Time.now.zone
=> "SAST"
[7] pry(#<#<Class:0x007fd082dcde80>>)> 

Any ideas on how I can get these timezones to align?

chrisedington commented 7 years ago

Semi obvious now that I see it but I was creating my bookables with +0200 timezone, and I should have done it in UTC.

I converted my bookable creation to UTC:

IceCube::Schedule.new(date.utc, duration: 45.minutes)