views:

35

answers:

1

Hello, I currently have a model called Job which has an attribute called CPU. This corresponds to the CPU time that a job was running. I would like to add all the time attributes of all the jobs for a specific date. This column is in the time format 00:00:00. Therefore I thought this would work:

def self.cpu_time
  sum(:cpu)
end

Which returns the following "Sat Jan 01 00:00:00 UTC 2000".

For my test data, I used the following cpu times: 00:00:46 00:26:46

Any help would be appreciated

This solved my problem, although it doesnt seem to be the rails way:

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
A: 

You don't state what data type you're using for the cpu column. Personally I would store it in seconds and then convert to hours, minutes and seconds after summing it.

John Topley
The CPU column is stored in the Time format 00:00:00
jalagrange
How would you propose I store the time in seconds? I am currently parsing an XML file in which the CPU time node is also in time format 00:00:00. How do I change from that format, to seconds?
jalagrange
A Time object represents a specific time, not a length of time. If you want to store a length of time, store the number of seconds. You can convert the "00:00:00" string to seconds with `h,m,s = "01:12:34".split(':'); h.to_i * 3600 + m.to_i * 60 + s`
mckeed