Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check time between 9.30 to 4 ruby

Tags:

time

ruby

i have a code that erroneously produces it and i am thinking that there must be a better way to check for time >9.30 am and time <4 pm any ideas is highly appreicated.

def checkTime

   goodtime=false
   if(Time.now.hour>9 and Time.now.min>30) then

     if(Time.now.hour<16) then
       goodtime=true

     else
       # do nothing
     end
   elsif Time.now.hour>9 and Time.now.hour<16 then
     goodtime=true

   else
      # do nothing
   end
   return goodtime

 end
like image 347
junkone Avatar asked Apr 10 '12 14:04

junkone


2 Answers

t = Time.now

Range.new(
  Time.local(t.year, t.month, t.day, 9),
  Time.local(t.year, t.month, t.day, 16, 30)
) === t
like image 169
d11wtq Avatar answered Oct 27 '22 06:10

d11wtq


def check_time(t=Time.now)
  early = Time.new(t.year, t.month, t.day, 9, 30, 0, t.utc_offset)
  late  = Time.new(t.year, t.month, t.day, 16, 0, 0, t.utc_offset)
  t.between?(early, late)
end
like image 5
steenslag Avatar answered Oct 27 '22 07:10

steenslag