代码之家  ›  专栏  ›  技术社区  ›  jalagrange

在Ruby on Rails中添加时间

  •  1
  • jalagrange  · 技术社区  · 16 年前

    我目前有一个名为job的模型,它有一个名为cpu的属性。这与作业运行的CPU时间相对应。我想为特定日期添加所有作业的所有时间属性。此列采用时间格式00:00:00。因此我认为这是可行的:

    def self.cpu_time
      sum(:cpu)
    end
    

    返回以下信息:“2000年1月1日星期六00:00:00 UTC”。

    对于我的测试数据,我使用了以下CPU时间: 00∶00∶46 00∶26:46

    任何帮助都将不胜感激

    这解决了我的问题,尽管它似乎不是轨道:

    def self.cput
        @times = find(:all,
                    :select => 'cput')
       @total_time =0
        for time in @times do  
          @total_time += time.cput.to_i - 946684800
          end
       @total_time  
    
      end
    
    1 回复  |  直到 15 年前
        1
  •  0
  •   John Topley    16 年前

    您没有说明要用于的数据类型 cpu 列。就我个人而言,我会将它存储在秒中,然后在求和后转换为小时、分钟和秒。