When Rails saves a time column, it converts my timezone to UTC. When I want to get the record out of the database, it always comes back with 2000-01-01
attached so if the timezone conversion rolls it to the next day it causes problems.
Example (Rails version 3.2.8):
Here's a simplified version of my ReservableTime
model.
> ReservableTime
=> ReservableTime(id: integer, start_at: time, end_at: time)
So, I make one that starts at 3:00pm and ends at 4:30pm:
> ReservableTime.create({start_at: Time.parse("3:00pm"), end_at: Time.parse("4:00pm")})
=> #<ReservableTime id: 1446, start_at: "2017-02-05 23:00:00", end_at: "2017-02-06 00:30:00">
Rails converted the times to UTC from my time zone (GMT-08:00). As you can see, the end_at
column rolled over to the next day. Would be fine if this were a date column but when I go to retrieve the record...
> reservable_time = ReservableTime.find 1446
=> #<ReservableTime id: 1446, start_at: "2000-01-01 23:00:00", end_at: "2000-01-01 00:30:00">
The times come back with the same default date (2000-01-01) so my end_at
wound up being before my start_at
!
So, if I want to know if something falls between my two times I will find that nothing will...
> Time.parse("4:00pm").between?(reservable_time.start_at, reservable_time.end_at)
=> false
Grrr... And just to make sure I'm not crazy:
> Time.parse("4:00pm").between?(Time.parse("3:00pm"), Time.parse("4:30pm"))
=> true
What can be done?