Jason Burgett
2009-Jun-28 00:30 UTC
Determine if a time is within certain hours of the day
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. -- Posted via http://www.ruby-forum.com/.
Robby Russell
2009-Jun-28 00:44 UTC
Re: Determine if a time is within certain hours of the day
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<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Robby Russell Chief Evangelist, Partner PLANET ARGON, LLC design // development // hosting w/Ruby on Rails http://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]
Jason Burgett
2009-Jun-28 01:08 UTC
Re: Determine if a time is within certain hours of the day
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:> 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<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> between two times. For example, is "t" between 5:00 PM and 9:00 PM. >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > > > -- > Robby Russell > Chief Evangelist, Partner > > PLANET ARGON, LLC > design // development // hosting w/Ruby on Rails > > http://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]-- Posted via http://www.ruby-forum.com/.
Älphä Blüë
2009-Jun-28 03:04 UTC
Re: Determine if a time is within certain hours of the day
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. -- Posted via http://www.ruby-forum.com/.
Rob Biedenharn
2009-Jun-29 15:47 UTC
Re: Determine if a time is within certain hours of the day
On Jun 27, 2009, at 9:08 PM, Jason Burgett 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> Robby Russell 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<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> between two times. For example, is "t" between 5:00 PM and 9:00 PM. >>> -- >>> Posted via http://www.ruby-forum.com/. >> >> -- >> Robby Russell >> Chief Evangelist, Partner >> >> PLANET ARGON, LLC >> design // development // hosting w/Ruby on Rails >> >> http://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]
Colin Law
2009-Jun-30 07:57 UTC
Re: Determine if a time is within certain hours of the day
2009/6/29 Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org>:> ... > 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)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
Rick DeNatale
2009-Jun-30 11:08 UTC
Re: Determine if a time is within certain hours of the day
On Tue, Jun 30, 2009 at 3:57 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/6/29 Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org>: >> ... >> 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) > > 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