检查时间在9.30到4之间

jun*_*one 9 ruby time

我有一个错误地生成它的代码,我认为必须有一个更好的方法来检查时间>上午9点30分和时间<4 pm,任何想法都是高度适用的.

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
Run Code Online (Sandbox Code Playgroud)

d11*_*wtq 6

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
Run Code Online (Sandbox Code Playgroud)


ste*_*lag 5

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
Run Code Online (Sandbox Code Playgroud)