我在这里找到了这种方法。
start = DateTime.now
sleep 15
stop = DateTime.now
#minutes
puts ((stop-start) * 24 * 60).to_i
hours,minutes,seconds,frac = Date.day_fraction_to_time(stop-start)
我有以下错误:
`<main>': private method `day_fraction_to_time' called for Date:Class (NoMethodError)
我检查了/usr/lib/ruby/1.9.1/date.rb并找到了它:
def day_fraction_to_time(fr) # :nodoc:
ss, fr = fr.divmod(SECONDS_IN_DAY) # 4p
h, ss = ss.divmod(3600)
min, s = ss.divmod(60)
return h, min, s, fr * 86400
end
但是如果我用ruby1.8运行它就没有问题。/usr/lib/ruby/1.8/date.rb给了我:
def self.day_fraction_to_time(fr)
ss, fr = fr.divmod(SECONDS_IN_DAY) # 4p
h, ss = ss.divmod(3600)
min, s = ss.divmod(60)
return h, min, s, fr
end
所以我去看了文档(1.9)并且没有这种方法的痕迹。我知道这是一个愚蠢的问题,但他们为什么要删除它?甚至还有关于如何使用/usr/lib/ruby/1.9.1/date.rb中的方法的示例:
def secs_to_new_year(now = DateTime::now())
new_year = DateTime.new(now.year + 1, 1, 1)
dif = new_year - now
hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(dif)
return hours * 60 * 60 + mins * 60 + secs
end
但我仍然收到错误:
test.rb:24:in `secs_to_new_year': private method `day_fraction_to_time' called for Date:Class (NoMethodError)
from test.rb:28:in `<main>'