Ruby 检查9.30到4点之间的时间
我有一个错误产生它的代码,我想一定有更好的方法来检查时间>上午9点30分和时间9点和时间。现在.min>30) 如果(Time.now.hour9和Time.now.hourRuby 检查9.30到4点之间的时间,ruby,time,Ruby,Time,我有一个错误产生它的代码,我想一定有更好的方法来检查时间>上午9点30分和时间9点和时间。现在.min>30) 如果(Time.now.hour9和Time.now.hour def检查时间 返回((Time.now.hour*60)+Time.now.min)>=570&((Time.now.hour*60)+Time.now.min)
def检查时间
返回((Time.now.hour*60)+Time.now.min)>=570&((Time.now.hour*60)+Time.now.min)<960
结束
在混合版本环境中工作,需要这种精确的方法我在代码中添加了以下内容:
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
我需要分钟也大于30分钟,如果小时是我得到的
NoMethodError:undefined方法'minutes'为2012-04-10 18:19:18+0200:Time
那((Time.now.hour*60)+Time.now.min)呢?谢谢你的回答。我遇到了一些问题,比如ArgumentError:irb中的参数数量错误(7代表0):7:ininitialize'from(irb):7:in
new'from(irb):7我无法让这个在2.0.0中工作,因为它报告:TypeError:can't iterate from
。使用将范围端点转换为\u i
,然后与t进行比较。但是,到\u i
可以工作。
def checkTime
return ((Time.now.hour * 60) + Time.now.min) >= 570 && ((Time.now.hour * 60) + Time.now.min) < 960
end
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
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
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