Ruby,从 Date.day_fraction_to_time 获取小时、秒和时间
我在此处找到了此方法。
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
所以我去看了 documentation(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>'
I've found this method here.
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)
I have the following error:
`<main>': private method `day_fraction_to_time' called for Date:Class (NoMethodError)
I've checked /usr/lib/ruby/1.9.1/date.rb and I've found it:
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
But I have no problem if I run it with ruby1.8. /usr/lib/ruby/1.8/date.rb gives me:
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
So i went to see the documentation(1.9) and there's no trace of this method. I know it's a dumb question, but why did they remove it? There is even this example on how to use the method in /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
but I'm still getting the error:
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>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么它被设为私有,但您仍然可以访问它:
这样您就可以覆盖 OOP 封装机制...这不是一件很好的事情,但它确实有效。
I don't know why it was made private, but you can still access it:
This way you override the OOP encapsulation mechanizm... This is not a very nice thing to do, but it works.
我找到了另一种对我来说更优雅的方法:
感谢 ruby- 的 Colin Bartlett forum.com
要了解它是如何工作的,我建议阅读 David Seiler 的 答案
I've found another method which seems more elegant to me:
Thanks to Colin Bartlett from ruby-forum.com
To understand how it works I suggest to read David Seiler's answer