我有一个错误地生成它的代码,我认为必须有一个更好的方法来检查时间>上午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)
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)
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)
| 归档时间: |
|
| 查看次数: |
3439 次 |
| 最近记录: |