【问题标题】:Calculate total hours in Rails计算 Rails 的总小时数
【发布时间】:2016-11-09 06:30:49
【问题描述】:
Foo.first = {id: 1, start:2000-01-01 07:00:00 UTC, end: 2000-01-01 12:00:00 UTC, total_hours_worked: nil}

这里以end为例。

我已经为startend 创建了Foot.time,因为我只需要几小时/分钟。我以前从未使用过 Time 对象,所以请耐心等待。

我需要用startend 获得总小时数:

start = Foo.find(1).start
end = Foo.find(1).end
start + end #=> errors
start.to_i + end.to_i = 1893438000 #=> What's that?
distance_of_time_in_words(start.to_i + end.to_i) #=> "about 60 years" What?

我期待5:00:00。我知道5:00:00 确实意味着早上 5 点,但上下文会有所不同。

很少搜索我见过this。我真的很努力不在 Rails/Ruby 中为这种“应该采用的方法”使用更多的宝石。

我的问题将找到我真正想要实现的目标的答案(获得一周的总工作时间):

Foo.pluck(:total_hours_worked).sum(&:to_d) #=> best way?

:total_hours_worked 是一个字符串,只是为了让事情变得不那么复杂。

【问题讨论】:

    标签: ruby-on-rails ruby postgresql ruby-on-rails-5


    【解决方案1】:

    以下是获得所需格式的方法 ("5:00:00"):

    # Monkeypatch Integer class to add a new method
    class Integer
      def pretty_duration
        seconds = self % 60
        minutes = (self / 60) % 60
        hours   = self / (60 * 60)
    
        format('%02d:%02d:%02d', hours, minutes, seconds)
      end
    end
    
    # Get start and end time converted into integers
    timestart = (Time.now - 5.hours).to_i
    timeend   = Time.now.to_i
    
    # Use pretty_duration method.
    # P.S. Difference between time objects already gives you a number, but we need Integer
    (timeend - timestart).pretty_duration
    #=> "05:00:00"
    

    此设置允许您不依赖 Rails 视图助手 (distance_of_time_in_words),因为您可能需要在视图之外的某个地方对时间进行格式化。

    我的问题将找到我真正想要实现的目标的答案 (获取一周的总工作时间):

    Foo.pluck(:total_hours_worked).sum(&:to_d) #=> 最好的方法?在哪里 :total_hours_worked 将是一个字符串,只是为了减少事情 复杂。

    最有效的方法(如果您将total_hours_worked 设置为正确格式的字符串)将在数据库级别上将其汇总为十进制:

    Foo.sum('total_hours_worked::decimal')
    

    【讨论】:

    • 你喜欢猴子补丁,嗯? :)
    • @SergioTulentsev 没那么多,只是我碰巧在我当前的应用程序中使用了这个 :)
    • 这一切很有趣,我在尝试这个时正在吃一些猴子坚果。哈哈。我会确认的。
    【解决方案2】:

    end 是一个关键字。你不能给它赋值。

    现在,获取时差:

    start = "2000-01-01 07:00:00 UTC".to_time
    endtime = "2000-01-01 12:00:00 UTC".to_time
    difference = ((endtime - start) / 1.hour)
    # => 5.0
    

    或使用助手

    distance_of_time_in_words(endtime, start)
    # => "about 5 hours"
    

    【讨论】:

      【解决方案3】:

      由于开始和结束之间需要时间,所以您编写的代码应该是:

      distance_of_time_in_words(end.to_i - start.to_i)
      

      您应该能够使用here 描述的方法格式化输出。

      【讨论】:

      • 准确! about 5 hours。但是如何获得5:00:00
      • @Sylar 尝试 distance_of_time_in_words((end.to_i - start.to_i) / 1.hour)
      • 没有。我得到了less than a minute
      • @Sylar (end.to_i - start.to_i) / 1.hour 应该给你 5.0,你可以使用我在问题中更新的链接进行格式化。
      【解决方案4】:

      使用下面的代码获取差异并获取格式为“5:00:00”的小时

      distance_of_time_in_words(end.to_i - start.to_i).strftime("%H%M%S")
      
      t.strftime("%H:%M:%S") > "22:49:27"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-26
        • 1970-01-01
        • 2017-01-08
        • 2023-01-20
        • 2019-05-05
        相关资源
        最近更新 更多