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 text representing the date, 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 vector containing a user specified date format. See NOTE 1.
|
| | 10 | %
|
| | 11 | % RETURN PARAMETERS:
|
| | 12 | % DATESTRING: char vector, containing date and, optionally, time formated
|
| | 13 | % as per user specified format.
|
| | 14 | %
|
| | 15 | % EXAMPLES:
|
| | 16 | % The date vector [2002 10 01 16 8] reformed as text representing date and time,
|
| | 17 | % using a user format, 'dd-mm-yyyy HH:MM', will display as
|
| | 18 | % 01-10-2002 16:08 .
|
| | 19 | %
|
| | 20 | % NOTE 1: The format specifier allows free-style date format, within the
|
| | 21 | % following limits -
|
| | 22 | % ddd => day is formatted as abbreviated name of weekday
|
| | 23 | % dd => day is formatted as two digit day of month
|
| | 24 | % d => day is formatted as first letter of name of weekday
|
| | 25 | % mmm => month is formatted as three letter abbreviation of name of month
|
| | 26 | % mm => month is formatted as two digit month of year
|
| | 27 | % m => month is formatted as one or two digit month of year
|
| | 28 | % yyyy => year is formatted as four digit year
|
| | 29 | % yy => year is formatted as two digit year
|
| | 30 | % HH => hour is formatted as two digit hour of the day
|
| | 31 | % MM => minute is formatted as two digit minute of the hour
|
| | 32 | % SS => second is formatted as two digit second of the minute
|
| | 33 | % The user may use any separator and other delimiters of his liking, but
|
| | 34 | % must confine himself to the above format tokens regarding day, month,
|
| | 35 | % year, hour, minute and second.
|
| | 36 | %
|
| | 37 | %
|
| | 38 | %------------------------------------------------------------------------------
|
| | 39 |
|
| | 40 | % Copyright 2003-2019 The MathWorks, Inc.
|
| | 41 |
|
< 0.001 | 30 | 42 | if isempty(dtvector) || isempty(formatstr)
|
| | 43 | dtstrarray = '';
|
| | 44 | return;
|
< 0.001 | 30 | 45 | else
|
< 0.001 | 30 | 46 | dtstr = formatstr;
|
< 0.001 | 30 | 47 | end
|
| | 48 |
|
0.001 | 30 | 49 | showAmPm = [strfind(lower(dtstr), 'am'), strfind(lower(dtstr), 'pm')];
|
< 0.001 | 30 | 50 | wrtAmPm = numel(showAmPm);
|
| | 51 |
|
| | 52 | % Canonicalize and error-check dtstr by converting it to ICU format.
|
0.013 | 30 | 53 | dtstr = matlab.internal.datetime.cnv2icudf(dtstr,false);
|
| | 54 |
|
| | 55 | % To assist with formatting, use 'D' and 'S' for day and second, respectively,
|
| | 56 | % so as not to confuse the 'd' for day with '%d' and the 's' for second with
|
| | 57 | % '%s' when building the conversion character vector. Because millisecond is
|
| | 58 | % already 'S', replace that with 'F' first. None of these are "unused" chars, so
|
| | 59 | % we're not stepping on anything else.
|
< 0.001 | 30 | 60 | dtstr = strrep(dtstr, 'S', 'F');
|
< 0.001 | 30 | 61 | dtstr = strrep(dtstr, 'd', 'D');
|
< 0.001 | 30 | 62 | dtstr = strrep(dtstr, 's', 'S');
|
| | 63 |
|
| | 64 | % Use 'H' for all hour formats, this function looks for AM/PM to decide if
|
| | 65 | % it should display 24-hour or 12-hour (unlike ICU).
|
< 0.001 | 30 | 66 | dtstr = strrep(dtstr, 'h', 'H');
|
| | 67 |
|
< 0.001 | 30 | 68 | showYr = strfind(dtstr,'y'); wrtYr = numel(showYr);
|
< 0.001 | 30 | 69 | showMo = strfind(dtstr,'M'); wrtMo = numel(showMo);
|
< 0.001 | 30 | 70 | showNday = strfind(dtstr,'D'); wrtNDay = numel(showNday); % ICU's 'd'
|
< 0.001 | 30 | 71 | showWday = strfind(dtstr,'E'); wrtWDay = numel(showWday); % ICU's 'e'
|
< 0.001 | 30 | 72 | showHr = strfind(dtstr,'H'); wrtHr = numel(showHr);
|
< 0.001 | 30 | 73 | showMin = strfind(dtstr,'m'); wrtMin = numel(showMin);
|
< 0.001 | 30 | 74 | showSec = strfind(dtstr,'S'); wrtSec = numel(showSec); % ICU's 's'
|
< 0.001 | 30 | 75 | showMsec = strfind(dtstr,'F'); wrtMsec = numel(showMsec); % ICU's 'S'
|
< 0.001 | 30 | 76 | showQrt = strfind(dtstr,'Q'); wrtQrt = numel(showQrt);
|
| | 77 |
|
| | 78 | % Format day.
|
< 0.001 | 30 | 79 | if wrtNDay > 0
|
< 0.001 | 30 | 80 | dtstr = strrep(dtstr, 'DD', '%02d');
|
< 0.001 | 30 | 81 | day = abs(dtvector(:,3));
|
< 0.001 | 30 | 82 | showNday = showNday(1);
|
| | 83 | else
|
| | 84 | day = [];
|
< 0.001 | 30 | 85 | end
|
| | 86 |
|
| | 87 | % Format weekday.
|
< 0.001 | 30 | 88 | if wrtWDay > 0
|
| | 89 | if islocal
|
| | 90 | locale = 'local';
|
| | 91 | else
|
| | 92 | locale = 'en_us';
|
| | 93 | end
|
| | 94 | [~, dayOfWeek] = weekday(datenum(dtvector), 'long', locale);
|
| | 95 | switch wrtWDay
|
| | 96 | case 4 % long weeday names (e.g., 'Monday')
|
| | 97 | dtstr = strrep(dtstr, 'EEEE', '%s');
|
| | 98 | case 3 % short weekday names (e.g., 'Mon')
|
| | 99 | dtstr = strrep(dtstr, 'EEE', '%s');
|
| | 100 | dayOfWeek = dayOfWeek(:, 1:3);
|
| | 101 | case 1 % 1-letter weekday names (e.g., 'M')
|
| | 102 | dtstr = strrep(dtstr, 'E', '%s');
|
| | 103 | dayOfWeek = dayOfWeek(:, 1);
|
| | 104 | otherwise % cases where a literal E was misinterpreted
|
| | 105 | error(message('MATLAB:formatdate:dayFormat', formatstr));
|
| | 106 | end
|
| | 107 | showWday = showWday(1);
|
< 0.001 | 30 | 108 | else
|
< 0.001 | 30 | 109 | dayOfWeek = [];
|
< 0.001 | 30 | 110 | end
|
| | 111 |
|
| | 112 | % Format year.
|
| | 113 | % Calculating year may truncate the first element of the datevector to two
|
| | 114 | % digits, thus it must be done after any weekday calculations.
|
< 0.001 | 30 | 115 | if wrtYr > 0
|
< 0.001 | 30 | 116 | if wrtYr == 4
|
< 0.001 | 30 | 117 | dtstr = strrep(dtstr,'yyyy','%.4d');
|
| | 118 | else % wrtYr == 2
|
| | 119 | dtstr = strrep(dtstr,'yy','%02d');
|
| | 120 | dtvector(:,1) = mod(abs(dtvector(:,1)),100);
|
< 0.001 | 30 | 121 | end
|
< 0.001 | 30 | 122 | year = mod(dtvector(:,1),10000);
|
< 0.001 | 30 | 123 | showYr = showYr(1);
|
| | 124 | else
|
| | 125 | year = [];
|
< 0.001 | 30 | 126 | end
|
| | 127 |
|
| | 128 | % Format quarter.
|
| | 129 | % This must happen after wrtNDay and wrtWDay are set.
|
< 0.001 | 30 | 130 | if wrtQrt > 0
|
| | 131 | dtstr = strrep(dtstr,'QQQ', 'Q%1d');
|
| | 132 | qrt = floor((dtvector(:,2)-1)/3)+1;
|
| | 133 | showQrt = showQrt(1);
|
< 0.001 | 30 | 134 | else
|
< 0.001 | 30 | 135 | qrt = [];
|
< 0.001 | 30 | 136 | end
|
| | 137 |
|
| | 138 |
|
| | 139 | % Format month.
|
< 0.001 | 30 | 140 | if wrtMo > 0
|
< 0.001 | 30 | 141 | switch wrtMo
|
< 0.001 | 30 | 142 | case 4 %long month names
|
| | 143 | if islocal
|
| | 144 | month = getmonthnamesmx('longloc');
|
| | 145 | else
|
| | 146 | month = {'January';'February';'March';'April';'May'; ...
|
| | 147 | 'June';'July';'August';'September';'October'; ...
|
| | 148 | 'November';'December'};
|
| | 149 | end
|
| | 150 | monthfmt = '%s';
|
| | 151 | dtstr = strrep(dtstr,'MMMM',monthfmt);
|
| | 152 | month = char(month(dtvector(:,2)));
|
< 0.001 | 30 | 153 | case 3 % short month names
|
< 0.001 | 30 | 154 | if islocal
|
| | 155 | month = getmonthnamesmx('shortloc');
|
< 0.001 | 30 | 156 | else
|
< 0.001 | 30 | 157 | month = {'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'};
|
< 0.001 | 30 | 158 | end
|
< 0.001 | 30 | 159 | monthfmt = '%s';
|
< 0.001 | 30 | 160 | dtstr = strrep(dtstr,'MMM',monthfmt);
|
0.002 | 30 | 161 | month = char(strrep(month(dtvector(:,2)), '.', '')); %remove period
|
| | 162 | case 2 % two-digit month number
|
| | 163 | dtstr = strrep(dtstr,'MM','%02d');
|
| | 164 | month = abs(dtvector(:,2));
|
| | 165 | otherwise % 1-letter month names
|
| | 166 | if islocal
|
| | 167 | month = getmonthnamesmx('shortloc');
|
| | 168 | else
|
| | 169 | month = {'J';'F';'M';'A';'M';'J';'J';'A';'S';'O';'N';'D'};
|
| | 170 | end
|
| | 171 | dtstr = strrep(dtstr,'M','%.1s');
|
| | 172 | month = char(month(dtvector(:,2)));
|
< 0.001 | 30 | 173 | end
|
< 0.001 | 30 | 174 | showMo = showMo(1);
|
| | 175 | else
|
| | 176 | month = [];
|
< 0.001 | 30 | 177 | end
|
| | 178 |
|
| | 179 | % Format hour.
|
< 0.001 | 30 | 180 | h = dtvector(:,4);
|
< 0.001 | 30 | 181 | if wrtHr > 0
|
< 0.001 | 30 | 182 | if wrtAmPm > 0
|
| | 183 | fmt = '%2d';
|
| | 184 | dtvector(:,4) = mod(h-1,12) + 1; % replace hour column with 12h format.
|
< 0.001 | 30 | 185 | else
|
< 0.001 | 30 | 186 | fmt = '%02d';
|
< 0.001 | 30 | 187 | end
|
< 0.001 | 30 | 188 | dtstr = strrep(dtstr,'HH',fmt);
|
< 0.001 | 30 | 189 | hour = dtvector(:,4);
|
< 0.001 | 30 | 190 | showHr = showHr(1);
|
| | 191 | else
|
| | 192 | hour = [];
|
< 0.001 | 30 | 193 | end
|
| | 194 |
|
| | 195 | % Format AM/PM.
|
< 0.001 | 30 | 196 | if wrtAmPm > 0
|
| | 197 | if islocal
|
| | 198 | amPmVals = getampmtokensmx;
|
| | 199 | else
|
| | 200 | amPmVals = {'AM', 'PM'};
|
| | 201 | end
|
| | 202 | dtstr = strrep(dtstr, 'a', '%s');
|
| | 203 | amPm(h < 12) = amPmVals(1);
|
| | 204 | amPm(h >= 12) = amPmVals(2);
|
| | 205 | amPm = char(amPm);
|
< 0.001 | 30 | 206 | else
|
< 0.001 | 30 | 207 | amPm = [];
|
< 0.001 | 30 | 208 | end
|
| | 209 |
|
| | 210 | % Format minute.
|
< 0.001 | 30 | 211 | if wrtMin > 0
|
< 0.001 | 30 | 212 | dtstr = strrep(dtstr,'mm','%02d');
|
< 0.001 | 30 | 213 | minute = dtvector(:,5);
|
< 0.001 | 30 | 214 | showMin = showMin(1);
|
| | 215 | else
|
| | 216 | minute = [];
|
< 0.001 | 30 | 217 | end
|
| | 218 |
|
| | 219 | % Format second.
|
< 0.001 | 30 | 220 | if wrtSec > 0
|
< 0.001 | 30 | 221 | dtstr = strrep(dtstr,'SS','%02d');
|
< 0.001 | 30 | 222 | second = floor(dtvector(:,6));
|
< 0.001 | 30 | 223 | showSec = showSec(1);
|
| | 224 | else
|
| | 225 | second = [];
|
< 0.001 | 30 | 226 | end
|
| | 227 |
|
| | 228 | % Format millisecond.
|
< 0.001 | 30 | 229 | if wrtMsec > 0
|
| | 230 | dtstr = strrep(dtstr,'FFF','%03d');
|
| | 231 | millisecond = floor(1000*(dtvector(:,6) - floor(dtvector(:,6))));
|
| | 232 | showMsec = showMsec(1);
|
< 0.001 | 30 | 233 | else
|
< 0.001 | 30 | 234 | millisecond = [];
|
< 0.001 | 30 | 235 | end
|
| | 236 |
|
| | 237 | % build date-time array to print
|
< 0.001 | 30 | 238 | dtorder = [showYr, showQrt, showMo, showNday, showWday, ...
|
| 30 | 239 | showAmPm, showHr, showMin, showSec, showMsec];
|
< 0.001 | 30 | 240 | dtarray = {year, qrt, month, day, dayOfWeek, ...
|
| 30 | 241 | amPm, hour, minute, second, millisecond};
|
< 0.001 | 30 | 242 | dtarray = dtarray([wrtYr, wrtQrt, wrtMo, wrtNDay, wrtWDay, ...
|
| 30 | 243 | wrtAmPm, wrtHr, wrtMin, wrtSec, wrtMsec] > 0);
|
| | 244 |
|
| | 245 | % sort date vector in the order of the time format fields
|
< 0.001 | 30 | 246 | [~, dtorder] = sort(dtorder);
|
| | 247 |
|
| | 248 | % print date vector using conversion character vector
|
< 0.001 | 30 | 249 | dtarray = dtarray(dtorder);
|
< 0.001 | 30 | 250 | nrows = size(dtvector,1);
|
< 0.001 | 30 | 251 | if nrows == 1
|
| | 252 | %optimize if only one member
|
0.004 | 30 | 253 | dtstrarray = sprintf(dtstr, dtarray{:});
|
| | 254 | else
|
| | 255 | dtstrarray = cell(nrows,1);
|
| | 256 | numeldtarray = length(dtarray);
|
| | 257 | thisdate = cell(1,numeldtarray);
|
| | 258 | for i = 1:nrows
|
| | 259 | for j = 1:numeldtarray
|
| | 260 | % take horzontal slice through cells
|
| | 261 | thisdate{j} = dtarray{j}(i,:);
|
| | 262 | end
|
| | 263 | dtstrarray{i} = sprintf(dtstr, thisdate{:});
|
| | 264 | end
|
< 0.001 | 30 | 265 | end
|
Other subfunctions in this file are not included in this listing.