# Time Compare

**URL:** <https://www.ruby-forum.com/t/time-compare/157994>\
**Category:** Ruby\
**Created:** [January 17, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/time-compare/157994 "2009-01-17T14:21:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 17, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/time-compare/157994/1 "2009-01-17T14:21:08Z")

</div>

Hello all

is there any direct comparison function in ruby or in rails for time

I need a function which compare current time is between 2 time objects  
or not

…

Please help me

thanks in advance

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 17, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/time-compare/157994/2 "2009-01-17T14:34:32Z")

</div>

Cyrus D. schrieb:

> thanks in advance  
> my first solution would be to put the three time objects in an Array:  
> now = Time.now  
> timebefore = now - 30_60_60  
> timeafter = now + 30_60_60  
> timearray = [now, timebefore, timeafter]  
> and after that using  
> timearray.sort  
> to sort them. If now is between the two dates if should be on position 1

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 17, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/time-compare/157994/3 "2009-01-17T15:17:19Z")

</div>

now = Time.now  
yesterday=now-1.day  
tomorrow=now+1.day

is\_between = true if now \> yesterday and now \< tomorrow

So… you want to know if

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 17, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/time-compare/157994/4 "2009-01-17T15:55:07Z")

</div>

badboy wrote:

> Cyrus D. schrieb:
> 
> > thanks in advance  
> > my first solution would be to put the three time objects in an Array:  
> > now = Time.now  
> > timebefore = now - 30_60_60  
> > timeafter = now + 30_60_60  
> > timearray = [now, timebefore, timeafter]  
> > and after that using  
> > timearray.sort  
> > to sort them. If now is between the two dates if should be on position 1

Or put the time objects in a range.

timeslot = (Time.now+1..Time.now+2)

# use like this:

4.times do  
puts timeslot.include?(Time.now)  
sleep 1  
end

hth,

Siep

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 23, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/time-compare/157994/5 "2009-01-23T21:50:55Z")

</div>

Since Time uses Comparable, then why not use .between? method  
([module Comparable - RDoc Documentation](http://www.ruby-doc.org/core/classes/Comparable.html#M007548)):

t = Time.now  
t\_before = t - 10  
t\_after = t + 10

puts t.between?(t\_before, t\_after)

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 25, 2009, 2:04am UTC](https://www.ruby-forum.com/t/time-compare/157994/6 "2009-01-25T02:04:46Z")

</div>

Jarmo P. wrote:

> Since Time uses Comparable, then why not use .between? method  
> ([http://www.ruby-doc.org/core/classes/Comparable.html#M007548](http://www.ruby-doc.org/core/classes/Comparable.html#M007548)):

(Note that #between includes the boundaries. You have more control when  
you use explicit \> and \< )

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 27, 2009, 12:00pm UTC](https://www.ruby-forum.com/t/time-compare/157994/7 "2009-01-27T12:00:56Z")

</div>

Robert K. wrote:

> is\_between = now \> yesterday and now \< tomorrow

Danger danger!!!

irb(main):012:0\> foo = 3 \> 1 and 3 \< 2  
=\> false  
irb(main):013:0\> foo  
=\> true

‘and’ and ‘or’ have lower precedence even than ‘=’, so it’s parsed as

(foo = 3 \> 1) and (3 \< 2)

The moral is: don’t use these very-low precedence operators unless you  
really know what you’re doing. And even then, use parentheses to be  
sure. Learned the hard way 🙂

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 27, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/time-compare/157994/8 "2009-01-27T12:14:10Z")

</div>

Hi,

Am Dienstag, 27. Jan 2009, 17:20:13 +0900 schrieb Robert K.:

> is\_between = now \> yesterday && now \< tomorrow  
> is\_between = now \> yesterday and now \< tomorrow

Sorry, Robert, but in the second case the binding is

(is\_between = now \> yesterday) and now \< tomorrow

Or

irb(main):001:0\> a,b,c=1,4,2  
=\> [1, 4, 2]  
irb(main):002:0\> i = b\>a and b\<c  
=\> false  
irb(main):003:0\> i  
=\> true

A common Ruby pitfall…

Bertram

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 27, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/time-compare/157994/9 "2009-01-27T12:45:54Z")

</div>

Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ‘and’ and ‘or’ have lower precedence even than ‘=’, so it’s parsed as
> 
> (foo = 3 \> 1) and (3 \< 2)
> 
> The moral is: don’t use these very-low precedence operators unless you  
> really know what you’re doing. And even then, use parentheses to be  
> sure. Learned the hard way 🙂

That’s the second time today I notice that you are required to use  
parentheses in Ruby (cf. m(arg){block}). Would that be a Lots of  
Insipid and Stupid Parentheses language?

In anycase, I’ve got too small a brain to remember such rules, I put  
parentheses everywhere.

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 27, 2009, 9:23am UTC](https://www.ruby-forum.com/t/time-compare/157994/10 "2009-01-27T09:23:17Z")

</div>

2009/1/17 list. rb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> now = Time.now  
> yesterday=now-1.day  
> tomorrow=now+1.day
> 
> is\_between = true if now \> yesterday and now \< tomorrow

Sorry for interrupting you, but this is not a safe idiom. Why?  
Because of this: assuming someone had used “is\_between” before then  
chances are that the value is logical true (there are far more true  
values than false values). If the date is not in between you still  
retain the old value of x. Instead, rather do the assignment  
unconditionally:

is\_between = now \> yesterday && now \< tomorrow  
is\_between = now \> yesterday and now \< tomorrow

You can as well do

is\_between = (yesterday..tomorrow).include? now

but this has a slightly different semantics because it will also be  
true if your test time matches one of the boundaries.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 27, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/time-compare/157994/11 "2009-01-27T13:51:27Z")

</div>

> > The moral is: don’t use these very-low precedence operators unless you  
> > really know what you’re doing. And even then, use parentheses to be  
> > sure. Learned the hard way 🙂
> 
> That’s the second time today I notice that you are required to use  
> parentheses in Ruby (cf. m(arg){block}). Would that be a Lots of  
> Insipid and Stupid Parentheses language?

Only if you had to use them all the time, and put them in the wrong  
place 🙂

> In anycase, I’ve got too small a brain to remember such rules, I put  
> parentheses everywhere.

That’s always the safe bet.

You could argue that Ruby would have been a better language if ‘and’,  
‘or’ and ‘not’ were removed completely. I wouldn’t disagree, but it’s  
easy to avoid them when you know.

BTW, Perl has exactly the same problem too. Some of the following work,  
and some don’t. But because Perl doesn’t make such wide use of  
exceptions, this is a common idiom:

open FILE, “/etc/mot” or die “No such file”;  
open FILE, “/etc/mot” or die(“No such file”);  
open FILE, “/etc/mot” || die “No such file”;  
open FILE, “/etc/mot” || die(“No such file”);  
open(FILE, “/etc/mot”) or die “No such file”;  
open(FILE, “/etc/mot”) or die(“No such file”);  
open(FILE, “/etc/mot”) || die “No such file”;  
open(FILE, “/etc/mot”) || die(“No such file”);

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [January 27, 2009, 1:08pm UTC](https://www.ruby-forum.com/t/time-compare/157994/12 "2009-01-27T13:08:12Z")

</div>

2009/1/27 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ‘and’ and ‘or’ have lower precedence even than ‘=’, so it’s parsed as
> 
> (foo = 3 \> 1) and (3 \< 2)
> 
> The moral is: don’t use these very-low precedence operators unless you  
> really know what you’re doing. And even then, use parentheses to be  
> sure. Learned the hard way 🙂

Thanks for the heads up! Originally I did not have the “and” version  
in because I usually use the “&&” just because of the precedence issue  
you mention. Somehow reading the other “and” version tricked my into  
believing it would be safe here.

Mantra of the day: Always test! Always test! Always test! 🙂

Cheers

robert
