0

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?

Jason Galuten
  • 1,042
  • 2
  • 11
  • 20
  • 1
    How would you represent a time that starts at 11pm and ends at 2am the next day with your current design? I think you need to store the start and end as DateTime – usha Feb 05 '17 at 23:44
  • @Vimsha I see what you mean. I will never need times to cross from one day to the other so I hadn't thought of that. If I store the times as DateTime then it adds a layer everywhere of getting rid of the date... – Jason Galuten Feb 05 '17 at 23:57
  • http://stackoverflow.com/questions/12832857/date-reverts-to-2000-01-01-from-activerecord-to-mysql and http://stackoverflow.com/questions/1966627/why-does-rails-always-display-a-datetime-as-1-1-2000-with-the-correct-time – Raj Feb 06 '17 at 00:30
  • If your time rolls over to another date then you have to store it as DateTime there is no other solution to it..unless of course you end up saving `time` column in your local timezone which is not a good practice if you are planing to release it globally. – Abhinay Feb 06 '17 at 02:38

1 Answers1

0

Thanks for the comments, everyone. Super helpful. I decided to go with DateTime.

What I learned: If there is a need to compare the difference between two times, or determine whether something falls between two times, Time will not work, and DateTime should be used.

It feels like a waste if you don't need the date part, but the truth is that the date part is needed in order to keep the time zone conversion from destroying the relationship between two times.

Jason Galuten
  • 1,042
  • 2
  • 11
  • 20