time | calls | line |
---|
| | 1 | function dtstr = cnv2icudf(formatstr)
|
| | 2 | % CNV2ICUDF maps date format tokens to ICU date format tokens
|
| | 3 | % ICUFORMAT = CNV2ICUDF(MLFORMAT) turns the date format into a date
|
| | 4 | % format that uses the format tokens of the ICU Libraries
|
| | 5 | %
|
| | 6 | % INPUT PARAMETERS:
|
| | 7 | % MLFORMAT: char string containing a user specified date format
|
| | 8 | % string. See NOTE 1.
|
| | 9 | %
|
| | 10 | % RETURN PARAMETERS:
|
| | 11 | % ICUFORMAT: char string, containing date and, optionally, time formatted
|
| | 12 | % as per user specified format string.
|
| | 13 | %
|
| | 14 | % NOTE 1: The format specifier allows free-style date format, within the
|
| | 15 | % following limits -
|
| | 16 | % ddd => day is formatted as abbreviated name of weekday
|
| | 17 | % dd => day is formatted as two digit day of month
|
| | 18 | % d => day is formatted as first letter of name of weekday
|
| | 19 | % mmm => month is formatted as three letter abbreviation of name of month
|
| | 20 | % mm => month is formatted as two digit month of year
|
| | 21 | % m => month is formatted as one or two digit month of year
|
| | 22 | % yyyy => year is formatted as four digit year
|
| | 23 | % yy => year is formatted as two digit year
|
| | 24 | % HH => hour is formatted as two digit hour of the day
|
| | 25 | % MM => minute is formatted as two digit minute of the hour
|
| | 26 | % SS => second is formatted as two digit second of the minute
|
| | 27 | % The user may use any separator and other delimiters of his liking, but
|
| | 28 | % must confine himself to the above format tokens regarding day, month,
|
| | 29 | % year, hour, minute and second.
|
| | 30 | %
|
| | 31 | %
|
| | 32 | %------------------------------------------------------------------------------
|
| | 33 |
|
| | 34 | % Copyright 2002-2011 The MathWorks, Inc.
|
| | 35 |
|
| 15 | 36 | dtstr = formatstr;
|
| | 37 |
|
| | 38 | % Replace AM/PM with 'a' to avoid confusion with months and minutes.
|
| 15 | 39 | showAmPm = [strfind(lower(dtstr), 'am'), strfind(lower(dtstr), 'pm')];
|
| 15 | 40 | wrtAmPm = numel(showAmPm);
|
| 15 | 41 | if wrtAmPm > 0
|
| | 42 | if wrtAmPm > 1
|
| | 43 | error(message('MATLAB:formatdate:ampmFormat', formatstr));
|
| | 44 | end
|
| | 45 | dtstr(showAmPm) = [];
|
| | 46 | dtstr(showAmPm) = 'a';
|
| | 47 | end
|
| | 48 |
|
| | 49 | % Ensure that days, hours, milliseconds, quarters, seconds, and year are
|
| | 50 | % case-correct.
|
| 15 | 51 | dtstr = strrep(dtstr, 'd', 'D');
|
| 15 | 52 | dtstr = strrep(dtstr, 'f', 'F');
|
| 15 | 53 | dtstr = strrep(dtstr, 'h', 'H');
|
| 15 | 54 | dtstr = strrep(dtstr, 'q', 'Q');
|
| 15 | 55 | dtstr = strrep(dtstr, 'S', 's');
|
| 15 | 56 | dtstr = strrep(dtstr, 'Y', 'y');
|
| | 57 |
|
| 15 | 58 | showYr = strfind(dtstr,'y'); wrtYr = numel(showYr);
|
| 15 | 59 | showMo = strfind(dtstr,'m'); wrtMo = numel(showMo);
|
| 15 | 60 | showDay = strfind(dtstr,'D'); wrtDay = numel(showDay);
|
| 15 | 61 | showHr = strfind(dtstr,'H'); wrtHr = numel(showHr);
|
| 15 | 62 | showMin = strfind(dtstr,'M'); wrtMin = numel(showMin);
|
| 15 | 63 | showSec = strfind(dtstr,'s'); wrtSec = numel(showSec);
|
| 15 | 64 | showMsec = strfind(dtstr,'F'); wrtMsec = numel(showMsec);
|
| 15 | 65 | showQrt = strfind(dtstr,'Q'); wrtQrt = numel(showQrt);
|
| 15 | 66 | showT = strfind(dtstr,'T'); wrtT = numel(showT);
|
| | 67 |
|
| 15 | 68 | dtstr = strrep(dtstr,'M','N'); % to avoid confusion with ICU month tokens
|
| | 69 |
|
| | 70 | % Escape T.
|
| 15 | 71 | if wrtT > 0
|
| | 72 | dtstr = strrep(dtstr, 'T', '''T''');
|
| | 73 | end
|
| | 74 |
|
| | 75 | % Format date
|
| 15 | 76 | if wrtYr > 0
|
| 15 | 77 | if (wrtYr ~= 4 && wrtYr ~= 2) || showYr(end) - showYr(1) >= wrtYr
|
| | 78 | error(message('MATLAB:formatdate:yearFormat', formatstr));
|
| | 79 | end
|
| 15 | 80 | end
|
| 15 | 81 | if wrtQrt > 0
|
| | 82 | if wrtQrt ~= 2 || showQrt(2) - showQrt(1) > 1
|
| | 83 | error(message('MATLAB:formatdate:quarterFormat', formatstr));
|
| | 84 | end
|
| | 85 | if any([wrtMo, wrtDay, wrtAmPm, wrtHr, wrtMin, wrtSec, wrtMsec] > 0)
|
| | 86 | error(message('MATLAB:formatdate:quarterFormatMismatch',formatstr));
|
| | 87 | end
|
| | 88 | dtstr = strrep(dtstr, 'QQ', 'QQQ');
|
| | 89 | end
|
| 15 | 90 | if wrtMo > 0
|
| 15 | 91 | if wrtMo > 4 || showMo(end) - showMo(1) >= wrtMo
|
| | 92 | error(message('MATLAB:formatdate:monthFormat', formatstr));
|
| | 93 | end
|
| 15 | 94 | dtstr = strrep(dtstr,'m','M');
|
| 15 | 95 | end
|
| 15 | 96 | if wrtDay > 0
|
| 15 | 97 | dtstr = strrep(dtstr, 'DDDDDD', 'EEEEdd');
|
| 15 | 98 | dtstr = strrep(dtstr, 'DDDDD', 'EEEdd');
|
| 15 | 99 | dtstr = strrep(dtstr, 'DDDD', 'EEEE');
|
| 15 | 100 | dtstr = strrep(dtstr, 'DDD', 'EEE');
|
| 15 | 101 | dtstr = strrep(dtstr, 'DD', 'dd');
|
| 15 | 102 | dtstr = strrep(dtstr, 'D', 'E');
|
| 15 | 103 | showNday = strfind(dtstr,'d'); wrtNday = numel(showNday);
|
| 15 | 104 | if wrtNday > 0
|
| 15 | 105 | if wrtNday ~= 2 || showNday(2) - showNday(1) ~= 1
|
| | 106 | error(message('MATLAB:formatdate:dayFormat', formatstr));
|
| | 107 | end
|
| 15 | 108 | end
|
| 15 | 109 | showWday = strfind(dtstr,'E'); wrtWday = numel(showWday);
|
| 15 | 110 | if wrtWday > 0
|
| | 111 | if wrtWday > 4 || showWday(end) - showWday(1) >= wrtWday
|
| | 112 | error(message('MATLAB:formatdate:dayFormat', formatstr));
|
| | 113 | end
|
| | 114 | end
|
| 15 | 115 | end
|
| | 116 |
|
| | 117 | % Format time
|
| 15 | 118 | if wrtHr > 0
|
| 15 | 119 | if wrtHr == 2 && showHr(2) - showHr(1) == 1
|
| 15 | 120 | if wrtAmPm
|
| | 121 | dtstr = strrep(dtstr,'H','h');
|
| | 122 | end
|
| | 123 | else
|
| | 124 | error(message('MATLAB:formatdate:hourFormat', formatstr));
|
| | 125 | end
|
| 15 | 126 | end
|
| 15 | 127 | if wrtMin > 0
|
| 15 | 128 | if wrtMin == 2 && showMin(2) - showMin(1) == 1
|
| 15 | 129 | dtstr = strrep(dtstr,'N','m');
|
| | 130 | else
|
| | 131 | error(message('MATLAB:formatdate:minuteFormat', formatstr));
|
| | 132 | end
|
| 15 | 133 | end
|
| 15 | 134 | if wrtSec > 0
|
| 15 | 135 | if wrtSec ~= 2 || showSec(2) - showSec(1) ~= 1
|
| | 136 | error(message('MATLAB:formatdate:secondFormat', formatstr));
|
| | 137 | end
|
| 15 | 138 | end
|
| 15 | 139 | if wrtMsec > 0
|
| | 140 | if wrtMsec == 3 && showMsec(3) - showMsec(1) == 2
|
| | 141 | dtstr = strrep(dtstr,'F','S');
|
| | 142 | else
|
| | 143 | error(message('MATLAB:formatdate:millisecondFormat', formatstr));
|
| | 144 | end
|
| | 145 | end
|