This is a static copy of a profile report

Home

timefun/private/formatdate (121 calls, 0.120 sec)
Generated 05-Aug-2011 13:01:28 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/timefun/private/formatdate.m
Copy to new window for comparing multiple runs

Parents (calling functions)

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

Line NumberCodeCallsTotal Time% TimeTime Plot
80
[starts, ends] = regexp(dtstr,...
1210.033 s27.3%
90
showWeekDay = [];
1210.022 s18.2%
263
dtstrarray = sprintf(dtstr,dta...
1210.011 s9.1%
252
end
1210.011 s9.1%
247
else
1210.011 s9.1%
All other lines  0.033 s27.3%
Totals  0.120 s100% 
Children (called functions)
No children
Code Analyzer results
No Code Analyzer messages.
Coverage results
[ Show coverage for parent directory ]
Total lines in function282
Non-code lines (comments, blank lines)74
Code lines (lines that can run)208
Code lines that did run89
Code lines that did not run119
Coverage (did run/can run)42.79 %
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 formated 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 day of month
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-2009 The MathWorks, Inc.
42
121 43 if isempty(dtvector) || isempty(formatstr)
44 dtstrarray = '';
45 return
121 46 else
121 47 dtstr = formatstr;
121 48 end
49
121 50 notAMPM = isempty(strfind(formatstr,'AM')) && isempty(strfind(formatstr,'PM'));
121 51 year = []; month = []; day = []; dayOfWeek= []; hour = []; minute = []; second = [];
121 52 millisecond = [];
121 53 wrtWeekday = 0; wrtday = 0;
54
55 % make sure days are capital D and seconds are capital second, so as not to
56 % confuse d for day with d as in %d when building conversion string.
121 57 dtstr = strrep(dtstr,'d','D');
121 58 dtstr = strrep(dtstr,'s','S');
121 59 dtstr = strrep(dtstr,'Y','y');
121 60 if notAMPM
61 else
62 if islocal
63 ampm = getampmtokensmx;
64 else
65 ampm = {'AM','PM'};
66 end
67 dtstr = strrep(dtstr,'AM',''); % remove AM to avoid confusion below
68 dtstr = strrep(dtstr,'PM',''); % remove PM to avoid confusion below
69 end
70
71 %All indices for sorting are computed before modifying the length of the
72 %string
121 73 showyr = strfind(dtstr,'y'); wrtYr = numel(showyr);
121 74 showmo = strfind(dtstr,'m'); wrtMo = numel(showmo);
121 75 showhr = strfind(dtstr,'H'); wrtHr = numel(showhr);
121 76 showmin = strfind(dtstr,'M'); wrtMin = numel(showmin);
121 77 showsec = strfind(dtstr,'S'); wrtSec = numel(showsec);
121 78 showMsec = strfind(dtstr,'F'); wrtMsec = numel(showMsec);
121 79 showqrt = strfind(dtstr,'Q'); wrtQrt = numel(showqrt);
0.03 121 80 [starts, ends] = regexp(dtstr, 'D{1,4}', 'start', 'end');
81
82
83 %Replace the various day and Weekday formats with the corresponding format
84 %specifications.
121 85 dtstr = strrep(dtstr, 'DDDD', '%s');
121 86 dtstr = strrep(dtstr, 'DDD', '%s');
121 87 dtstr = strrep(dtstr, 'DD', '%02d');
121 88 dtstr = strrep(dtstr, 'D', '%s');
121 89 showday = [];
0.02 121 90 showWeekDay = [];
121 91 for i = 1: length(starts)
121 92 if ends(i) - starts(i) == 1
121 93 if ~isempty(showday) %Only one numeric day subformat allowed
94 error(message('MATLAB:formatdate:dayFormat', formatstr));
95 end
121 96 wrtday = 1;
121 97 showday = starts(i);
121 98 day = abs(dtvector(:,3));
99 else
100 if ~isempty(showWeekDay) %Only one weekday subformat allowed
101 error(message('MATLAB:formatdate:dayFormat', formatstr));
102 end
103 showWeekDay = starts(i);
104 wrtWeekday = 1;
105 if islocal
106 locale = 'local';
107 else
108 locale = 'en_us';
109 end
110 switch ends(i) - starts(i)
111 case 3,
112 %long month names
113 [daynr,dayOfWeek] = weekday(datenum(dtvector), 'long', locale);%#ok
114 case {0, 2},
115 [daynr,dayOfWeek] = weekday(datenum(dtvector), locale);%#ok
116 if (ends(i) == starts(i))
117 dayOfWeek = dayOfWeek(:,1);
118 end
119 end
120 end
121 121 end
122
123 %Calculating year may truncate the first element of the datevector to two
124 %digits, thus it must be done after any weekday calculations.
121 125 if wrtYr > 0
121 126 if showyr(end) - showyr(1) >= wrtYr
127 error('MATLAB:datestr:yearFormat','Unrecognized year format. Format string: %s.', formatstr);
128 end
121 129 switch wrtYr
121 130 case 4,
121 131 dtstr = strrep(dtstr,'yyyy','%.4d');
132 case 2,
133 dtstr = strrep(dtstr,'yy','%02d');
134 dtvector(:,1) = mod(abs(dtvector(:,1)),100);
135 otherwise
136 error(message('MATLAB:formatdate:yearFormat', formatstr));
137 end
121 138 showyr = showyr(1);
0.01 121 139 year = mod(dtvector(:,1),10000);
121 140 end
141
142 % Format quarter. Must happen after wrtday and wrtWeekday are set.
121 143 if wrtQrt > 0
144 if wrtQrt~= 2 || showqrt(end) - showqrt(1) >= wrtQrt
145 error('MATLAB:formatdate:quarterFormat','Unrecognized quarter format. Format string: %s.', formatstr);
146 end
147 dtstr = strrep(dtstr,'QQ','Q%1d');
148 if wrtMo > 0 || wrtday > 0 || wrtWeekday || wrtHr > 0 || wrtMin > 0 || wrtSec > 0
149 error('MATLAB:formatdate:quarterFormat',...
150 'Cannot use other time and date fields besides year and quarter. Format string: %s.', formatstr);
151 end
152 showqrt = showqrt(1);
153 qrt = floor((dtvector(:,2)-1)/3)+1;
154 end
121 155 if wrtMo > 0
121 156 if showmo(end) - showmo(1) >= wrtMo
157 error(message('MATLAB:formatdate:monthFormat', formatstr));
158 end
121 159 switch wrtMo
121 160 case 4,
161 %long month names
162 if islocal
163 month = getmonthnamesmx('longloc');
164 else
165 month = getmonthnamesmx('long');
166 end
167 monthfmt = '%s';
168 dtstr = strrep(dtstr,'mmmm',monthfmt);
169 month = char(month(dtvector(:,2)));
121 170 case 3,
121 171 if islocal
172 month = getmonthnamesmx('shortloc');
121 173 else
121 174 month = {'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'};
121 175 end
0.01 121 176 monthfmt = '%s';
0.01 121 177 dtstr = strrep(dtstr,'mmm',monthfmt);
121 178 month = char(month(dtvector(:,2)));
179 case 2,
180 dtstr = strrep(dtstr,'mm','%02d');
181 month = abs(dtvector(:,2));
182 case 1,
183 if islocal
184 month = getmonthnamesmx('shortloc');
185 else
186 month = {'J';'F';'M';'A';'M';'J';'J';'A';'S';'O';'N';'D'};
187 end
188 dtstr = strrep(dtstr,'m','%.1s');
189 month = char(month(dtvector(:,2)));
190 otherwise
191 error(message('MATLAB:formatdate:monthFormat', formatstr));
192 end
121 193 showmo = showmo(1);
121 194 end
195
196 % Format time
121 197 if wrtHr > 0
121 198 if wrtHr ~= 2 || showhr(end) - showhr(1) >= wrtHr
199 error(message('MATLAB:formatdate:hourFormat', formatstr));
200 end
121 201 if notAMPM
121 202 fmt = '%02d';
203 else
204 fmt = '%2d';
205 h = dtvector(:,4);
206 c(h<12) = ampm(1);
207 c(h>=12) = ampm(2);
208 dtvector(:,4) = mod(h-1,12) + 1; % replace hour column with 12h format.
209 dtstr = [dtstr '%s']; % append conversion string for AM or PM
210 end
121 211 dtstr = strrep(dtstr,'HH',fmt);
121 212 hour = dtvector(:,4);
121 213 showhr = showhr(1);
121 214 end
215
121 216 if wrtMin > 0
121 217 if wrtMin ~= 2 || showmin(end) - showmin(1) >= wrtMin
218 error(message('MATLAB:formatdate:minuteFormat', formatstr));
219 end
121 220 dtstr = strrep(dtstr,'MM','%02d');
121 221 minute = dtvector(:,5);
121 222 showmin = showmin(1);
121 223 end
224
121 225 if wrtSec > 0
121 226 if wrtSec ~= 2 || showsec(end) - showsec(1) >= wrtSec
227 error(message('MATLAB:formatdate:secondFormat', formatstr));
228 end
121 229 dtstr = strrep(dtstr,'SS','%02d');
121 230 second = floor(dtvector(:,6));
121 231 showsec = showsec(1);
121 232 end
233
121 234 if wrtMsec > 0
235 if wrtMsec ~= 3 || showMsec(end) - showMsec(1) >= wrtMsec
236 error(message('MATLAB:formatdate:millisecondFormat', formatstr));
237 end
238 dtstr = strrep(dtstr,'FFF','%03d');
239 millisecond = floor(1000*(dtvector(:,6) - floor(dtvector(:,6))));
240 showMsec = showMsec(1);
241 end
242 % build date-time array to print
121 243 if wrtQrt > 0
244 dtorder = [showyr, showqrt];
245 dtarray = [{year} {qrt}];
246 dtarray = dtarray([(wrtYr>0) (wrtQrt>0)]);
0.01 121 247 else
121 248 dtorder = [showyr, showmo, showday, showWeekDay, showhr, showmin, showsec, showMsec];
121 249 dtarray = [{year} {month} {day} {dayOfWeek} {hour} {minute} {second} {millisecond}];
121 250 dtarray = dtarray([(wrtYr>0) (wrtMo>0) (wrtday>0) (wrtWeekday>0) (wrtHr>0) ...
251 (wrtMin>0) (wrtSec>0) (wrtMsec>0)]);
0.01 121 252 end
253
254 % sort date vector in the order of the time format fields
121 255 [tmp,dtorder] = sort(dtorder);%#ok
256
257 % print date vector using conversion string
121 258 dtarray = dtarray(dtorder);
121 259 rows = size(dtvector,1);
121 260 if (rows == 1)
261 %optimize if only one member
121 262 if notAMPM
0.01 121 263 dtstrarray = sprintf(dtstr,dtarray{:});
264 else
265 dtstrarray = sprintf(dtstr,dtarray{:},char(c));
266 end
267 else
268 dtstrarray = cell(rows,1);
269 numeldtarray = length(dtarray);
270 thisdate = cell(1,numeldtarray);
271 for i = 1:rows
272 for j = 1:numeldtarray
273 % take horzontal slice through cells
274 thisdate{j} = dtarray{j}(i,:);
275 end
276 if notAMPM
277 dtstrarray{i} = sprintf(dtstr,thisdate{:});
278 else
279 dtstrarray{i} = sprintf(dtstr,thisdate{:},char(c{i}));
280 end
281 end
282 end