I have a Time object

t = Time.now

t

Sat Jun 27 19:17:59 -0500 2009

Now I'm stuck trying to figure out an easy way to determine if "t" is

between two times. For example, is "t" between 5:00 PM and 9:00 PM.

I have a Time object

t = Time.now

t

Sat Jun 27 19:17:59 -0500 2009

Now I'm stuck trying to figure out an easy way to determine if "t" is

between two times. For example, is "t" between 5:00 PM and 9:00 PM.

current_time = Time.now

(current_time.hour >= 17) and (current_time.hour <= 21)

=> true

Ok, the real calculation I'm trying to do is between 12:00am and 4:00am

cuurent_time = Time.now

(current_time.hour > 0) and (current_time.hour <= 4)

If the current time is 00:17:24...

Duh, just figure out my error. I was doing that but didn't have

current_time >= 0, I had current_time > 0.

Thanks for the help.

Robby Russell wrote:

Ok, the real calculation I'm trying to do is between 12:00am and 4:00am

cuurent_time = Time.now

(current_time.hour > 0) and (current_time.hour <= 4)

If the current time is 00:17:24...

Duh, just figure out my error. I was doing that but didn't have

current_time >= 0, I had current_time > 0.Thanks for the help.

This will get everything up to 4:59. If you want 12:00am (00:00) to 4:00am (04:00) inclusive, you want something like

(0...4).include?(current_time.hour) ||

(current_time.hour == 4 &&

current_time.minute == 0 &&

current_time.second == 0)

Note that (x...y).include?(z) is equivalent to:

(x <= z && z < y)

-Rob

Rob Biedenharn http://agileconsultingllc.com

Rob@AgileConsultingLLC.com

That is not correct is it? I thought the default on a Range is to

include the limits so it would be

x<=z && z<=y

If I am right then you would need (0..3).include? in the above code.

Also in principle you may need to check that microseconds are zero,

though the design of the rest of the app may make this not necessary.

current_time.usec == 0

Colin

No, that's the difference between a two dot and a three dot range

(a..b) includes b, (a...b) does not.

Of course, I think I need to increase my font size, or possibly get

new spectacles.

Colin