Reputation: 1465
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
Upvotes: 10
Views: 3816
Reputation: 846
Working in a mixed version environment and needing this exact method here's what I put in my code:
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
Upvotes: 4
Reputation: 80065
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
Upvotes: 5
Reputation: 18463
Just:
def checkTime
return ((Time.now.hour * 60) + Time.now.min) >= 570 && ((Time.now.hour * 60) + Time.now.min) < 960
end
Upvotes: 4
Reputation: 35308
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
Upvotes: 7