# Comparing time values

**URL:** <https://www.ruby-forum.com/t/comparing-time-values/133337>\
**Category:** Rails\
**Created:** [March 30, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337 "2008-03-30T12:16:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/1 "2008-03-30T12:16:16Z")

</div>

In my controller, I would like to validate wether the time is before or  
after 10:30 a given day. The only way I’ve found so far is convert back  
from time to string to integer and so forth using the strftime-method,  
which seems a bit overkill.

Somewhere along the lines of Time.now =\> 10:30, although I can’t seem to  
find the proper methods to accomplish this.

Is there such a simple way to do this seemlingly mundane task?

Best regards,  
Gustav

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/2 "2008-03-30T12:26:31Z")

</div>

One way that seems to work ok is using seconds:

```
@time = Time.new
if (@time.hour * 60 + @time.min) * 60 > 37800 # 10:30 in seconds 

```

from 00:00  
@time += (60 \* 60 \* 24) # add another weekday  
@weekday = @time.wday  
else  
@weekday = @time.wday  
end

But still… seems a bit clumsy 😉

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/3 "2008-03-30T13:26:55Z")

</div>

Another issue I’m struggling with regarding Time is that I can’t seem to  
find a way to search in my database based on time.

I have an arrival time:

@arrival\_time = Time.parse(“11:30”)

and a flights-table with an arrival\_time column, created with :time in  
migrations. I can’t seem to find a way to search for “arrival\_time  
earlier than @arrival\_time”. Obviously my second-workaround won’t cut it  
here.

Anyone had the same problem and/or have a possible a solution?

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/4 "2008-03-30T14:04:25Z")

</div>

For your original problem, @time \> @time.change(:hour =\> 10, :min =\>  
30) might be a bit more readable that what you’ve got. Also look into  
using method like advance (or in rails 2.0 just time = time + 1.day)  
as those are clever enough to get things right when days aren’t 24  
hours long ( when DST kicks in and so on)

On Mar 30, 12:26 pm, Gu stav [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> here.

Does everything just work as expected here ?  
Flights.find :all, :condition =\> [‘arrival\_time \< ?’,@arrival\_time]  
seems to work for me.

Fred

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/5 "2008-03-30T15:54:24Z")

</div>

On 30 Mar 2008, at 14:42, Rob B. wrote:

> 1.day.since(@time) would give you a 23, 24, or 25 hour change  
> depending on whether you cross a DST boundary in either direction.

Actually in 2.0, 1.day is clever and those do work (happily today is  
the day where DST kicked in here so it’s easy to test!).  
1.day (and 1.month etc…) used to just return some fixed number of  
seconds, but they are now instances of ActiveSupport::Duration, which  
know how to handle all the various cases.

Fred

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/6 "2008-03-30T15:43:08Z")

</div>

On Mar 30, 2008, at 8:03 AM, Frederick C. wrote:

> For your original problem, @time \> @time.change(:hour =\> 10, :min =\>  
> 30) might be a bit more readable that what you’ve got. Also look into  
> using method like advance (or in rails 2.0 just time = time + 1.day)  
> as those are clever enough to get things right when days aren’t 24  
> hours long ( when DST kicks in and so on)

The cleverness is not in 1.day, but in the #ago and #since methods.  
1.day.since(@time) would give you a 23, 24, or 25 hour change  
depending on whether you cross a DST boundary in either direction.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/7 "2008-03-30T20:41:33Z")

</div>

I’m actually having the same problem right now, since ruby doesn’t  
have a class that deals with time, without a date attached to it.  
I’ve been playing around with rolling my own sudo Time class that just  
handles hours, minutes, and seconds. Along those lines, I’m trying it  
with storing the data as an integer in the database as seconds in the  
day. So, for example, 1AM would be 3600.

Then I will use composed\_of to instantiate that integer into my sudo  
Time object. Playing around, it seems to work. I can define a to\_s  
method on the sudo Time class and it will print it out in time, then I  
don’t have to have all those strftime method calls in my views. By  
having a to\_i method, ar seems to convert that into an integer  
correctly when saving.

The other alternative is to convert that Time object into something  
that the database understands for searches and then again in the  
views. But, it sounds like you were already doing that.

Maybe a monkeypatch on the Time class would help so you don’t have to  
remember the strftime all the time.

class Time  
def to\_time  
self.strftime("%R")  
end  
end

time = Time.parse(“10:30”)  
conditions = [“arrival\_time \< ‘#{time.to\_time}’”] if time

Although, perhaps it would be better to just create a new class that  
simply stores the Time object in it and then calls the strftime on  
to\_s, inspect, etc… Then use composed\_of in your model.

Hope that helps a little bit, I would be interested to hear what you  
decide to do. If I come up with anything I think is good, I’d be  
happy to share it.

---

<div class="post-metadata">

**Author:** ![gustav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/919ad9/32.png) [@gustav](https://www.ruby-forum.com/u/gustav)\
**Post date:** [March 30, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/comparing-time-values/133337/8 "2008-03-30T20:43:59Z")

</div>

> By  
> having a to\_i method, ar seems to convert that into an integer  
> correctly when saving.

Actually, I don’t think it is calling to\_i, its calling what I map  
that to in my composed\_of block. First, time I’ve used composed\_of,  
its pretty neat.
