Adding times in ruby on rails
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 he开发者_运维问答lp 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
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.
精彩评论