[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Time Compare

Cyrus Dev

1/17/2009 1:20:00 PM

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
--
Posted via http://www.ruby-....

11 Answers

Jan-Erik R.

1/17/2009 1:33:00 PM

0

Cyrus Dev schrieb:
> 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
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



list. rb

1/17/2009 2:15:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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

On Sat, Jan 17, 2009 at 8:32 AM, badboy <badboy@heartofgold.co.cc> wrote:

> Cyrus Dev schrieb:
>
>> 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
>>
> 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
>
>
>
>

Siep Korteling

1/17/2009 2:54:00 PM

0

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
--
Posted via http://www.ruby-....

Jarmo Pertman

1/23/2009 8:49:00 PM

0

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)
--
Posted via http://www.ruby-....

Albert Schlef

1/25/2009 1:03:00 AM

0

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 < )
--
Posted via http://www.ruby-....

Robert Klemme

1/27/2009 8:20:00 AM

0

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

--
remember.guy do |as, often| as.you_can - without end

Brian Candler

1/27/2009 10:59:00 AM

0

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.
--
Posted via http://www.ruby-....

Bertram Scharpf

1/27/2009 11:11:00 AM

0

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

pjb

1/27/2009 11:45:00 AM

0

Brian Candler <b.candler@pobox.com> writes:

> 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 :-)

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.
--
__Pascal Bourguignon__

Robert Klemme

1/27/2009 12:05:00 PM

0

2009/1/27 Brian Candler <b.candler@pobox.com>:
> 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 :-)

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

--
remember.guy do |as, often| as.you_can - without end