# Time problems

**URL:** <https://www.ruby-forum.com/t/time-problems/188716>\
**Category:** Rails\
**Created:** [May 17, 2010, 7:31am UTC](https://www.ruby-forum.com/t/time-problems/188716 "2010-05-17T07:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 17, 2010, 7:31am UTC](https://www.ruby-forum.com/t/time-problems/188716/1 "2010-05-17T07:31:09Z")

</div>

I am storying a column called start time and endtime, both having  
mysql type “time”.

I am using formtastic to create the forms like

\<%= f.input :time\_start, :label =\> “Between”, :as  
=\> :time, :ignore\_date =\> true %\>

The time gets stored just fine (though in 24 hour format, which is  
another headache), but when I extract this time to compare it to  
Time.now, to figure out, if “now” is between end and start time, the  
time returned is something like Sat Jan 01 15:00:00 UTC 2000, which is  
nowhere in 2010.

Why is this happening? Whats the best way to store start and end time  
to compare later on with “now”.

Thanks

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 17, 2010, 9:18am UTC](https://www.ruby-forum.com/t/time-problems/188716/2 "2010-05-17T09:18:49Z")

</div>

On May 17, 6:29 am, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am storying a column called start time and endtime, both having  
> mysql type “time”.

[snip]

> The time gets stored just fine (though in 24 hour format, which is  
> another headache), but when I extract this time to compare it to  
> Time.now, to figure out, if “now” is between end and start time, the  
> time returned is something like Sat Jan 01 15:00:00 UTC 2000, which is  
> nowhere in 2010.
> 
> Why is this happening? Whats the best way to store start and end time  
> to compare later on with “now”.

The mysql time type is a pure time of day type (ie no date component).  
You want a datetime column.

Fred

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 17, 2010, 6:57pm UTC](https://www.ruby-forum.com/t/time-problems/188716/3 "2010-05-17T18:57:47Z")

</div>

Thanks Frederick.

If I do that, it stores the date of the day the record was updated or  
created, I would still need to extract just the time portion and  
compare it. Is there a way to do that?

My use case is that on May 1st, customer makes reservation beween  
7-9pm for May 5th. The record is stored with timestamps of 5/1/2010:  
7-9 pm.

When he shows up on May5th, I need to validate Time.now… whether it’s  
between 7 - 9.  
On May 17, 12:17 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 17, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/time-problems/188716/4 "2010-05-17T19:40:44Z")

</div>

badnaam wrote:

> The time gets stored just fine (though in 24 hour format, which is  
> another headache)

Time is effectively meaningless without the date part. You say you want  
to validate times like 7 pm to 9 pm. Does that mean 7-9 pm standard time  
or daylight time? Without the date you can’t know. The time can be  
stored in UTC, but in order to display the time in a format your users  
will understand (local time) you again must know the date.

> My use case is that on May 1st, customer makes reservation beween  
> 7-9pm for May 5th. The record is stored with timestamps of 5/1/2010:  
> 7-9 pm.
> 
> When he shows up on May5th, I need to validate Time.now… whether it’s  
> between 7 - 9.  
> On May 17, 12:17ï¿½am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Even in your example you are specifying the date with the time. So if  
your customer makes a reservation between 7-9pm on May 5th, store a  
datetime range where start\_time = May 5, 2010 7:00 PM EDT and end\_time =  
May 5, 2010 9:00 PM EDT.
