Forum: Ruby Time Compare

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7694e527192e45b73b95f2ca41034463?d=identicon&s=25 Cyrus Dev (cyrusdev)
on 2009-01-17 14:21
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
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-01-17 14:34
(Received via mailing list)
Cyrus Dev 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
990bf71a4e84e1145a3131f35656dc18?d=identicon&s=25 List Rb (listx300108u79872)
on 2009-01-17 15:17
(Received via mailing list)
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
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2009-01-17 15:55
badboy wrote:
> Cyrus Dev 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
Ff97ca87af59ee68ceff5877a8365788?d=identicon&s=25 Jarmo Pertman (juuser)
on 2009-01-23 21:50
Since Time uses Comparable, then why not use .between? method
(http://www.ruby-doc.org/core/classes/Comparable.ht...

t = Time.now
t_before = t - 10
t_after = t + 10

puts t.between?(t_before, t_after)
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-01-25 02:04
Jarmo Pertman wrote:
> Since Time uses Comparable, then why not use .between? method
> (http://www.ruby-doc.org/core/classes/Comparable.ht...

(Note that #between includes the boundaries. You have more control when
you use explicit > and < )
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-27 09:23
(Received via mailing list)
2009/1/17 list. rb <list.rb@gmail.com>:
> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-27 12:00
Robert Klemme 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.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-01-27 12:14
(Received via mailing list)
Hi,

Am Dienstag, 27. Jan 2009, 17:20:13 +0900 schrieb Robert Klemme:
> 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
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-01-27 12:45
(Received via mailing list)
Brian Candler <b.candler@pobox.com> 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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-27 13:08
(Received via mailing list)
2009/1/27 Brian Candler <b.candler@pobox.com>:
> '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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-27 13:51
>> 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");
This topic is locked and can not be replied to.