08 Jul 2008
(Updated 09 Jul 2008)
Calculate the duration of a distributed job or individual task
function sec = duration(obj)
%DURATION Calculate the number of seconds from creation to finished.
% SEC = DURATION(OBJ) calculate total time (in seconds) to run a
% distributed job or task.
% If no return argument is assigned, the duration is only displayed. If
% the job or task has not finished, use the current time to show duration
% to this point.
% DURATION only calculates time for tasks or jobs completed within 30
% time = duration(job);
% 515 seconds
% Copyright 2007-2008 The MathWorks, Inc.
% Raymond S. Norris (firstname.lastname@example.org)
% Get the create and finished times. (Not sure if this is when create
% started or create finished).
ct = get(obj,'CreateTime');
ft = get(obj,'FinishTime');
typ = 'Task';
typ = 'Job';
disp([typ ' is still running. Using current time as finished time.'])
% datestr() doesn't support a time zone, so just put in EST. It
% doesn't matter for our calculations.
ft = [datestr(now,'ddd mmm dd HH:MM:SS') ' EST ' datestr(now,'yyyy')];
if isempty(ct) || isempty(ft)
sec = NaN;
% In case there's an error somewhere, try/catch it and return NaN with no
% Remove the standard time marking (e.g. EST) and the day (e.g. Thu).
sidx = strfind(ct,' ');
ct([1:4 sidx(4):sidx(5)-1]) = ;
ft([1:4 sidx(4):sidx(5)-1]) = ;
% Convert to a date number
fmt = 'mmm dd HH:MM:SS yyyy';
ct = datenum(ct,fmt);
ft = datenum(ft,fmt);
% Get time difference. Let's assume that it doesn't go past 30 days.
[y,mo,d,h,mi,s] = datevec(ft-ct);
s = s + mi*60 + h*60*60 + d*24*60*60;
s = NaN;
if isnan(s), s=0; end
disp([char(9) num2str(s) ' seconds'])
sec = s;