Подтвердить что ты не робот

Проверить время между 9.30-4 рубином

У меня есть код, который ошибочно производит его, и я думаю, что должен быть лучший способ проверить время > 9.30 утра и время < 16 вечера любые идеи очень полезны.

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
4b9b3361

Ответ 1

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

Ответ 2

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

Ответ 3

Просто:

def checkTime
  return ((Time.now.hour * 60) + Time.now.min) >= 570 && ((Time.now.hour * 60) + Time.now.min) < 960
end

Ответ 4

Работа в среде смешанной версии и нуждающийся в этом точном методе здесь, что я ввел в свой код:

if RUBY_VERSION == "1.9.3"
  def check_time(starthour,endhour,t=Time.now)
    early = Time.new(t.year, t.month, t.day, starthour, 0, 0)
    late  = Time.new(t.year, t.month, t.day, endhour, 0, 0)
    t.between?(early, late)
  end
elsif RUBY_VERSION == "1.8.7"
  def check_time(starthour,endhour,t=Time.now)
    early = Time.local(t.year, t.month, t.day, starthour, 0, 0)
    late  = Time.local(t.year, t.month, t.day, endhour, 0, 0)
    t.between?(early, late)
  end
end