This is a static copy of a profile report

Home

timefun/private/formatdate (9 calls, 0.060 sec)
Generated 14-Nov-2016 07:47:22 using cpu time.
function in file /usr/local/MATLAB/MATLAB_Production_Server/R2015a/toolbox/matlab/timefun/private/formatdate.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
timefun/private/dateformverifyfunction9
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
54
dtstr = matlab.internal.dateti...
90.040 s66.7%
255
dtstrarray = sprintf(dtstr, dt...
90.010 s16.7%
248
[~, dtorder] = sort(dtorder);
90.010 s16.7%
253
if nrows == 1
90 s0%
252
nrows = size(dtvector,1);
90 s0%
All other lines  0.000 s0.0%
Totals  0.060 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
cnv2icudffunction90.020 s33.3%
Self time (built-ins, overhead, etc.)  0.040 s66.7%
Totals  0.060 s100% 
Code Analyzer results
No Code Analyzer messages.
Coverage results
Show coverage for parent directory
Total lines in function267
Non-code lines (comments, blank lines)91
Code lines (lines that can run)176
Code lines that did run84
Code lines that did not run92
Coverage (did run/can run)47.73 %
Function listing
time 
calls 
 line
   1 
function dtstrarray = formatdate(dtvector,formatstr,islocal)
   2 
%   FORMATDATE casts date vector into a specified date format
   3 
%   DATESTRING = FORMATDATE(DATEVECTOR, FORMATSTRING) turns the date
   4 
%   vector into a formatted date string, according to the user's date
   5 
%   format template.
   6 
%
   7 
%   INPUT PARAMETERS:
   8 
%   DATEVECTOR: 1 x m double array, containing standard MATLAB date vector.
   9 
%   FORMATSTRING: char string containing a user specified date format
  10 
%                 string. See NOTE 1.
  11 
%
  12 
%   RETURN PARAMETERS:
  13 
%   DATESTRING: char string, containing date and, optionally, time formated
  14 
%               as per user specified format string.
  15 
%
  16 
%   EXAMPLES:
  17 
%   The date vector [2002 10 01 16 8] reformed as a date and time string,
  18 
%   using a user format, 'dd-mm-yyyy HH:MM', will display as 
  19 
%   01-10-2002 16:08 .
  20 
%   
  21 
%   NOTE 1: The format specifier allows free-style date format, within the
  22 
%   following limits - 
  23 
%   ddd  => day is formatted as abbreviated name of weekday
  24 
%   dd   => day is formatted as two digit day of month
  25 
%   d    => day is formatted as first letter of name of weekday
  26 
%   mmm  => month is formatted as three letter abbreviation of name of month
  27 
%   mm   => month is formatted as two digit month of year
  28 
%   m    => month is formatted as one or two digit month of year
  29 
%   yyyy => year is formatted as four digit year
  30 
%   yy   => year is formatted as two digit year
  31 
%   HH   => hour is formatted as two digit hour of the day
  32 
%   MM   => minute is formatted as two digit minute of the hour
  33 
%   SS   => second is formatted as two digit second of the minute
  34 
%   The user may use any separator and other delimiters of his liking, but
  35 
%   must confine himself to the above format tokens regarding day, month,
  36 
%   year, hour, minute and second.
  37 
% 
  38 
%   
  39 
%------------------------------------------------------------------------------
  40 

  41 
% Copyright 2003-2012 The MathWorks, Inc.
  42 

      9 
  43 
if isempty(dtvector) || isempty(formatstr) 
  44 
    dtstrarray = '';
  45 
    return;
      9 
  46 
else 
      9 
  47 
    dtstr = formatstr; 
      9 
  48 
end 
  49 

      9 
  50 
showAmPm = [strfind(lower(dtstr), 'am'), strfind(lower(dtstr), 'pm')]; 
      9 
  51 
wrtAmPm = numel(showAmPm); 
  52 

  53 
% Canonicalize and error-check dtstr by converting it to ICU format.
  0.04 
      9 
  54 
dtstr = matlab.internal.datetime.cnv2icudf(dtstr); 
  55 

  56 
% To assist with formatting, use 'D' and 'S' for days and seconds,
  57 
% respectively, so as not to confuse the 'd' for day with the 'd' in '%d'
  58 
% when building the conversion string.  Note also that, since milliseconds
  59 
% are already defined as S, we will need to convert them to F first.
      9 
  60 
dtstr = strrep(dtstr, 'S', 'F'); 
      9 
  61 
dtstr = strrep(dtstr, 'd', 'D'); 
      9 
  62 
dtstr = strrep(dtstr, 's', 'S'); 
  63 

  64 
% Ensure that all hour formats are capitalized since AM/PM was handled.
      9 
  65 
dtstr = strrep(dtstr, 'h', 'H'); 
  66 

      9 
  67 
showYr   = strfind(dtstr,'y'); wrtYr   = numel(showYr); 
      9 
  68 
showMo   = strfind(dtstr,'M'); wrtMo   = numel(showMo); 
      9 
  69 
showNday = strfind(dtstr,'D'); wrtNDay = numel(showNday); 
      9 
  70 
showWday = strfind(dtstr,'E'); wrtWDay = numel(showWday); 
      9 
  71 
showHr   = strfind(dtstr,'H'); wrtHr   = numel(showHr); 
      9 
  72 
showMin  = strfind(dtstr,'m'); wrtMin  = numel(showMin); 
      9 
  73 
showSec  = strfind(dtstr,'S'); wrtSec  = numel(showSec); 
      9 
  74 
showMsec = strfind(dtstr,'F'); wrtMsec = numel(showMsec); 
      9 
  75 
showQrt  = strfind(dtstr,'Q'); wrtQrt  = numel(showQrt); 
      9 
  76 
showT    = strfind(dtstr,'T'); wrtT    = numel(showT); 
  77 

  78 
% Unescape 'T'.
      9 
  79 
if wrtT > 0 
  80 
    dtstr = strrep(dtstr, '''T''', 'T');
  81 
end
  82 

  83 
% Format day.
      9 
  84 
if wrtNDay > 0 
      9 
  85 
    dtstr = strrep(dtstr, 'DD', '%02d'); 
      9 
  86 
    day = abs(dtvector(:,3)); 
      9 
  87 
    showNday = showNday(1); 
  88 
else
  89 
    day = [];
  90 
end
  91 

  92 
% Format weekday.
      9 
  93 
if wrtWDay > 0 
  94 
    if islocal
  95 
        locale = 'local';
  96 
    else
  97 
        locale = 'en_us';
  98 
    end
  99 
    [~, dayOfWeek] = weekday(datenum(dtvector), 'long', locale);
 100 
    switch wrtWDay
 101 
        case 4  % long weeday names (e.g., 'Monday')
 102 
            dtstr = strrep(dtstr, 'EEEE', '%s');
 103 
        case 3  % short weekday names (e.g., 'Mon')
 104 
            dtstr = strrep(dtstr, 'EEE', '%s');
 105 
            dayOfWeek = dayOfWeek(:, 1:3);
 106 
        case 1  % 1-letter weekday names (e.g., 'M')
 107 
            dtstr = strrep(dtstr, 'E', '%s');
 108 
            dayOfWeek = dayOfWeek(:, 1);
 109 
    end
 110 
    showWday = showWday(1);
      9 
 111 
else 
      9 
 112 
    dayOfWeek = []; 
      9 
 113 
end 
 114 

 115 
% Format year.
 116 
% Calculating year may truncate the first element of the datevector to two
 117 
% digits, thus it must be done after any weekday calculations.
      9 
 118 
if wrtYr > 0 
      9 
 119 
    if wrtYr == 4 
      9 
 120 
            dtstr = strrep(dtstr,'yyyy','%.4d'); 
 121 
    else % wrtYr == 2
 122 
            dtstr = strrep(dtstr,'yy','%02d');
 123 
            dtvector(:,1) = mod(abs(dtvector(:,1)),100);
 124 
    end
      9 
 125 
	year = mod(dtvector(:,1),10000); 
      9 
 126 
    showYr = showYr(1); 
 127 
else
 128 
    year = [];
 129 
end
 130 

 131 
% Format quarter.
 132 
% This must happen after wrtNDay and wrtWDay are set.
      9 
 133 
if wrtQrt > 0 
 134 
    dtstr = strrep(dtstr,'QQQ', 'Q%1d');
 135 
	qrt = floor((dtvector(:,2)-1)/3)+1;
 136 
    showQrt = showQrt(1);
      9 
 137 
else 
      9 
 138 
    qrt = []; 
      9 
 139 
end 
 140 

 141 
% Format month.
      9 
 142 
if wrtMo > 0 
      9 
 143 
    switch wrtMo 
      9 
 144 
        case 4     %long month names 
 145 
            if islocal
 146 
                month = getmonthnamesmx('longloc');
 147 
            else
 148 
                month = {'January';'February';'March';'April';'May'; ...
 149 
                         'June';'July';'August';'September';'October'; ...
 150 
                         'November';'December'};
 151 
            end
 152 
            monthfmt = '%s';
 153 
            dtstr = strrep(dtstr,'MMMM',monthfmt);
 154 
            month = char(month(dtvector(:,2)));
      9 
 155 
        case 3     % short month names 
      9 
 156 
            if islocal 
 157 
                month = getmonthnamesmx('shortloc');
      9 
 158 
            else 
      9 
 159 
                month = {'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'}; 
      9 
 160 
            end 
      9 
 161 
            monthfmt = '%s'; 
      9 
 162 
            dtstr = strrep(dtstr,'MMM',monthfmt); 
      9 
 163 
            month = char(month(dtvector(:,2))); 
 164 
        case 2    % two-digit month number
 165 
            dtstr = strrep(dtstr,'MM','%02d');
 166 
            month = abs(dtvector(:,2));
 167 
        otherwise % 1-letter month names
 168 
            if islocal
 169 
                month = getmonthnamesmx('shortloc');
 170 
            else
 171 
                month = {'J';'F';'M';'A';'M';'J';'J';'A';'S';'O';'N';'D'};
 172 
            end
 173 
            dtstr = strrep(dtstr,'M','%.1s');
 174 
            month = char(month(dtvector(:,2)));
 175 
    end
      9 
 176 
    showMo = showMo(1); 
 177 
else
 178 
    month = [];
 179 
end
 180 

 181 
% Format hour.
      9 
 182 
h = dtvector(:,4); 
      9 
 183 
if wrtHr > 0 
      9 
 184 
    if wrtAmPm > 0 
 185 
        fmt = '%2d';
 186 
        dtvector(:,4) = mod(h-1,12) + 1; % replace hour column with 12h format.
      9 
 187 
    else 
      9 
 188 
        fmt = '%02d'; 
      9 
 189 
    end 
      9 
 190 
    dtstr = strrep(dtstr,'HH',fmt); 
      9 
 191 
    hour = dtvector(:,4); 
      9 
 192 
    showHr = showHr(1); 
 193 
else
 194 
    hour = [];
 195 
end
 196 

 197 
% Format AM/PM.
      9 
 198 
if wrtAmPm > 0 
 199 
    if islocal
 200 
        amPmVals = getampmtokensmx;
 201 
    else
 202 
        amPmVals = {'AM', 'PM'};
 203 
    end
 204 
    dtstr = strrep(dtstr, 'a', '%s');
 205 
    amPm(h < 12) = amPmVals(1);
 206 
    amPm(h >= 12) = amPmVals(2);
 207 
    amPm = char(amPm);
      9 
 208 
else 
      9 
 209 
    amPm = []; 
      9 
 210 
end 
 211 

 212 
% Format minute.
      9 
 213 
if wrtMin > 0 
      9 
 214 
    dtstr = strrep(dtstr,'mm','%02d'); 
      9 
 215 
	minute = dtvector(:,5); 
      9 
 216 
    showMin = showMin(1); 
 217 
else
 218 
    minute = [];
 219 
end
 220 

 221 
% Format second.
      9 
 222 
if wrtSec > 0 
      9 
 223 
    dtstr = strrep(dtstr,'SS','%02d'); 
      9 
 224 
	second = floor(dtvector(:,6)); 
      9 
 225 
    showSec = showSec(1); 
 226 
else
 227 
    second = [];
 228 
end
 229 

 230 
% Format millisecond.
      9 
 231 
if wrtMsec > 0 
 232 
    dtstr = strrep(dtstr,'FFF','%03d');
 233 
	millisecond = floor(1000*(dtvector(:,6) - floor(dtvector(:,6))));
 234 
    showMsec = showMsec(1);
      9 
 235 
else 
      9 
 236 
    millisecond = []; 
      9 
 237 
end 
 238 

 239 
% build date-time array to print
      9 
 240 
dtorder = [showYr, showQrt, showMo, showNday, showWday, ... 
 241 
           showAmPm, showHr, showMin, showSec, showMsec];
      9 
 242 
dtarray = {year, qrt, month, day, dayOfWeek, ... 
 243 
           amPm, hour, minute, second, millisecond};
      9 
 244 
dtarray = dtarray([wrtYr, wrtQrt, wrtMo, wrtNDay, wrtWDay, ... 
 245 
                   wrtAmPm, wrtHr, wrtMin, wrtSec, wrtMsec] > 0);
 246 

 247 
% sort date vector in the order of the time format fields
  0.01 
      9 
 248 
[~, dtorder] = sort(dtorder); 
 249 

 250 
% print date vector using conversion string
      9 
 251 
dtarray = dtarray(dtorder); 
      9 
 252 
nrows = size(dtvector,1); 
      9 
 253 
if nrows == 1 
 254 
    %optimize if only one member
  0.01 
      9 
 255 
    dtstrarray = sprintf(dtstr, dtarray{:}); 
 256 
else
 257 
    dtstrarray = cell(nrows,1);
 258 
    numeldtarray = length(dtarray);
 259 
    thisdate = cell(1,numeldtarray);
 260 
    for i = 1:nrows
 261 
        for j = 1:numeldtarray
 262 
            % take horzontal slice through cells
 263 
            thisdate{j} = dtarray{j}(i,:);
 264 
        end
 265 
        dtstrarray{i} = sprintf(dtstr, thisdate{:});
 266 
    end
 267 
end