# Date-agnostic times

**URL:** <https://www.ruby-forum.com/t/date-agnostic-times/139345>\
**Category:** Rails\
**Created:** [June 3, 2008, 4:28am UTC](https://www.ruby-forum.com/t/date-agnostic-times/139345 "2008-06-03T04:28:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 3, 2008, 4:28am UTC](https://www.ruby-forum.com/t/date-agnostic-times/139345/1 "2008-06-03T04:28:30Z")

</div>

Let’s say I have a Trains model with arrival\_time specified in the  
migration as a :time. How can I structure the model and the query so  
that I can find all trains with an arrival\_time between 7:30 and  
9:30am, no matter the day? In my migration I specified :time but of  
course the database always contains a date and using :conditions =\>  
[‘arrival\_time \> ?’, time1] uses the date.

What am I missing? There has to be an easy way to do this…

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 3, 2008, 3:09pm UTC](https://www.ruby-forum.com/t/date-agnostic-times/139345/2 "2008-06-03T15:09:18Z")

</div>

Mark T. wrote:

> Let’s say I have a Trains model with arrival\_time specified in the  
> migration as a :time. How can I structure the model and the query so  
> that I can find all trains with an arrival\_time between 7:30 and  
> 9:30am, no matter the day? In my migration I specified :time but of  
> course the database always contains a date and using :conditions =\>  
> [‘arrival\_time \> ?’, time1] uses the date.
> 
> What am I missing? There has to be an easy way to do this…

Could you just track the time as an int like military time? (0000 -  
2359) You’d have to validate on entry to ensure that the minutes portion  
of a ‘time’ value never exceeded 59, but it should be doable.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 3, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/date-agnostic-times/139345/3 "2008-06-03T15:42:48Z")

</div>

On Jun 3, 9:09 am, Ar Chron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could you just track the time as an int like military time? (0000 -  
> 2359) You’d have to validate on entry to ensure that the minutes portion  
> of a ‘time’ value never exceeded 59, but it should be doable.

Yeah, I guess I’ll have to write my own Time class, although I’ll  
probably call it Clock to avoid confusion. Not sure why ruby needs  
both DateTime and Time, when they both represent dates AND times.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 3, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/date-agnostic-times/139345/4 "2008-06-03T17:18:57Z")

</div>

On Jun 2, 10:28 pm, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Let’s say I have a Trains model with arrival\_time specified in the  
> migration as a :time. How can I structure the model and the query so  
> that I can find all trains with an arrival\_time between 7:30 and  
> 9:30am, no matter the day? In my migration I specified :time but of  
> course the database always contains a date and using :conditions =\>  
> [‘arrival\_time \> ?’, time1] uses the date.
> 
> What am I missing? There has to be an easy way to do this…

What database are you usingm and can you change it? MySQL 5, I know  
for sure, has it’s own Time type that does not include a date, and  
indeed AR’s :time type maps to this.

An alternative is simply to always store the same day in every record  
(or create another column and do the same). Then your query will work  
correctly.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [June 4, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/date-agnostic-times/139345/5 "2008-06-04T15:18:21Z")

</div>

> What database are you usingm and can you change it? MySQL 5, I know  
> for sure, has it’s own Time type that does not include a date, and  
> indeed AR’s :time type maps to this.

Thanks for the info. Does it ignore the date portion of a ruby Time  
object?

I was developing on SQLite but I can change it to MySQL. From an  
idealistic point of view, I hate database-dependent code, especially  
this, which would mysteriously break when switched to another DB. But  
in this particular situation I don’t see that being an issue.

> An alternative is simply to always store the same day in every record  
> (or create another column and do the same). Then your query will work  
> correctly.

Another good idea. Thanks.
