Ruby,从 Date.day_fraction_to_time 获取小时、秒和时间

发布于 2024-09-20 00:28:21 字数 1686 浏览 7 评论 0原文

我在此处找到了此方法。

  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

但是如果我用 ruby​​1 运行它,我没有问题。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╄→承喏 2024-09-27 00:28:21

我不知道为什么它被设为私有,但您仍然可以访问它:

hours,minutes,seconds,frac = Date.send(:day_fraction_to_time, stop-start)

这样您就可以覆盖 OOP 封装机制...这不是一件很好的事情,但它确实有效。

I don't know why it was made private, but you can still access it:

hours,minutes,seconds,frac = Date.send(:day_fraction_to_time, stop-start)

This way you override the OOP encapsulation mechanizm... This is not a very nice thing to do, but it works.

温暖的光 2024-09-27 00:28:21

我找到了另一种对我来说更优雅的方法:

start = DateTime.now
sleep 3
stop = DateTime.now

puts "Date.day_fraction_to_time using wrapper"
class Date
    class << self
      def wrap_day_fraction_to_time( day_frac )
        day_fraction_to_time( day_frac )
      end
   end
end
hours, minutes, seconds, frac =
    Date.wrap_day_fraction_to_time( stop - start )
p hours, minutes, seconds, frac

感谢 ruby- 的 Colin Bartlett forum.com

要了解它是如何工作的,我建议阅读 David Seiler 的 答案

I've found another method which seems more elegant to me:

start = DateTime.now
sleep 3
stop = DateTime.now

puts "Date.day_fraction_to_time using wrapper"
class Date
    class << self
      def wrap_day_fraction_to_time( day_frac )
        day_fraction_to_time( day_frac )
      end
   end
end
hours, minutes, seconds, frac =
    Date.wrap_day_fraction_to_time( stop - start )
p hours, minutes, seconds, frac

Thanks to Colin Bartlett from ruby-forum.com

To understand how it works I suggest to read David Seiler's answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文