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
On Sat, Jun 27, 2009 at 5:30 PM, Jason
Burgett[email protected] wrote:
between two times. For example, is “t” between 5:00 PM and 9:00 PM.
Posted via http://www.ruby-forum.com/.
–
Robby R.
Chief Evangelist, Partner
PLANET ARGON, LLC
design // development // hosting w/Ruby on Rails
http://robbyonrails.com/
http://twitter.com/planetargon
aim: planetargon
+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]
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 R. wrote:
current_time = Time.now
(current_time.hour >= 17) and (current_time.hour <= 21)
=> true
On Sat, Jun 27, 2009 at 5:30 PM, Jason
Burgett[email protected] wrote:between two times. For example, is “t” between 5:00 PM and 9:00 PM.
Posted via http://www.ruby-forum.com/.
–
Robby R.
Chief Evangelist, PartnerPLANET ARGON, LLC
design // development // hosting w/Ruby on Railshttp://planetargon.com/
http://robbyonrails.com/
http://twitter.com/planetargon
aim: planetargon+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]
You can also create a named_scope in your Model and call it from a
method:
named_scope :compiled_this_week, lambda { { :conditions => ['compiled_on
? and compiled_on < ?’, Time.now.beginning_of_week,
Time.now.end_of_week] } }
compiled_on is just a field for when the date was compiled_on - you can
use created_at, etc. for substitution.
2009/6/29 Rob B. [email protected]:
 (x <= z && z < y)
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
On Jun 27, 2009, at 9:08 PM, Jason B. wrote:
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
On Tue, Jun 30, 2009 at 3:57 AM, Colin L.[email protected]
wrote:
Note that (x…y).include?(z) is equivalent to:
(x <= z && z < y)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.
No, that’s the difference between a two dot and a three dot range
(a…b) includes b, (a…b) does not.
–
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
2009/6/30 Rick DeNatale [email protected]:
  current_time.minute == 0 &&
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs