time | Calls | line |
---|
| | 1 | function [y,mo,d,h,mi,s] = datevec(t,varargin)
|
| | 2 | %DATEVEC Date components.
|
| | 3 | % V = DATEVEC(N) converts one or more date numbers N to date vectors V. N
|
| | 4 | % can be a scalar, vector, or multidimensional array of positive date
|
| | 5 | % numbers. DATEVEC returns an M-by-6 matrix containing M date vectors,
|
| | 6 | % where M is the total number of date numbers in N.
|
| | 7 | %
|
| | 8 | % V = DATEVEC(S,F) converts text representing one or more dates to date
|
| | 9 | % vectors V using format F. S can be a cell array of character vectors,
|
| | 10 | % string array, or a character array where each row corresponds to one
|
| | 11 | % date. All of the dates in S must have the same format which must be
|
| | 12 | % composed of date format symbols according to Table 2 in DATESTR help.
|
| | 13 | % DATEVEC returns an M-by-6 matrix of date vectors, where M is the number
|
| | 14 | % of elements (or rows of a character array) in S.
|
| | 15 | %
|
| | 16 | % Certain formats may not contain enough information to compute a date
|
| | 17 | % vector. In those cases, hours, minutes, and seconds default to 0, days
|
| | 18 | % default to 1, months default to January, and years default to the
|
| | 19 | % current year. Dates with two-character years (e.g. 5/6/03 ) are
|
| | 20 | % interpreted to be within the 100 years centered around the current
|
| | 21 | % year.
|
| | 22 | %
|
| | 23 | % V = DATEVEC(S,F,P) or V = DATEVEC(S,P,F) converts S to a date vector V,
|
| | 24 | % using the pivot year P and the date format F. The pivot year is the
|
| | 25 | % starting year of the 100-year range in which a two-character year
|
| | 26 | % resides. The default pivot year is the current year minus 50 years.
|
| | 27 | %
|
| | 28 | % [Y,MO,D,H,MI,S] = DATEVEC(...) takes any of the two syntaxes shown
|
| | 29 | % above and returns the components of the date vector as individual
|
| | 30 | % variables.
|
| | 31 | %
|
| | 32 | % V = DATEVEC(S) converts text representing one or more dates to date
|
| | 33 | % vector V. S must be in one of the date formats 0,1,2,6,13,14,15,16,23
|
| | 34 | % as defined by DATESTR. This calling syntax is provided for backward
|
| | 35 | % compatibility, and is significantly slower than the syntax which
|
| | 36 | % specifies the format character vector. If the format is known, the V =
|
| | 37 | % DATEVEC(S,F) syntax should be used.
|
| | 38 | %
|
| | 39 | % V = DATEVEC(S,P) converts S using pivot year P. If the format is known,
|
| | 40 | % the V = DATEVEC(S,F,P) or V = DATEVEC(S,P,F) syntax should be used.
|
| | 41 | %
|
| | 42 | % Note 1: If more than one input argument is used, the first argument
|
| | 43 | % must be a character vector representing a date or an array of character
|
| | 44 | % vectors representing dates.
|
| | 45 | %
|
| | 46 | % Note 2: The vectorized calling syntax can offer significant performance
|
| | 47 | % improvement for large arrays.
|
| | 48 | %
|
| | 49 | % Examples
|
| | 50 | % d = '12/24/1984';
|
| | 51 | % t = 725000.00;
|
| | 52 | % c = datevec(d) or c = datevec(t) produce c = [1984 12 24 0 0 0].
|
| | 53 | % [y,m,d,h,mi,s] = datevec(d) returns y=1984, m=12, d=24, h=0, mi=0, s=0.
|
| | 54 | % c = datevec('5/6/03') produces c = [2003 5 6 0 0 0] until 2054.
|
| | 55 | % c = datevec('5/6/03',1900) produces c = [1903 5 6 0 0 0].
|
| | 56 | % c = datevec('19.05.2000','dd.mm.yyyy') produces c = [2000 5 19 0 0 0].
|
| | 57 | %
|
| | 58 | % See also DATENUM, DATESTR, CLOCK, DATETICK.
|
| | 59 |
|
| | 60 | % Copyright 1984-2018 The MathWorks, Inc.
|
| | 61 | import matlab.internal.datatypes.stringToLegacyText
|
| | 62 |
|
< 0.001 | 10 | 63 | narginchk(1,3);
|
< 0.001 | 10 | 64 | t = stringToLegacyText(t);
|
| | 65 |
|
| | 66 | % parse input arguments
|
< 0.001 | 10 | 67 | isdatestr = ~isnumeric(t);
|
< 0.001 | 10 | 68 | isdateformat = false;
|
< 0.001 | 10 | 69 | if ~isdatestr && nargin > 1
|
| | 70 | warning(message('MATLAB:datevec:Inputs'));
|
< 0.001 | 10 | 71 | elseif nargin > 1
|
| | 72 | for i = 1:length(varargin)
|
| | 73 | varargin{i} = stringToLegacyText(varargin{i});
|
| | 74 | end
|
| | 75 | isdateformat = cellfun('isclass',varargin,'char');
|
| | 76 | if (nargin == 3)
|
| | 77 | if ~isdateformat(1)
|
| | 78 | pivotyear = varargin{1};
|
| | 79 | elseif ~isdateformat(2)
|
| | 80 | pivotyear = varargin{2};
|
| | 81 | elseif isdateformat(1) && isdateformat(2)
|
| | 82 | error(message('MATLAB:datevec:DateFormat'));
|
| | 83 | end
|
| | 84 | elseif (nargin == 2) && ~isdateformat
|
| | 85 | pivotyear = varargin{1};
|
| | 86 | end
|
< 0.001 | 10 | 87 | end
|
| | 88 |
|
< 0.001 | 10 | 89 | if isdatestr && isempty(t)
|
| | 90 | if nargout <= 1
|
| | 91 | y = zeros(0,6);
|
| | 92 | else
|
| | 93 | [y,mo,d,h,mi,s] = deal(zeros(0,0));
|
| | 94 | end;
|
| | 95 | warning(message('MATLAB:datevec:EmptyDate'));
|
| | 96 | return;
|
< 0.001 | 10 | 97 | end
|
| | 98 |
|
| | 99 | % branch to appropriate date string parser
|
< 0.001 | 10 | 100 | if isdatestr
|
| | 101 | % a date format string was specified
|
| | 102 | % map date format to ICU date format tokens
|
< 0.001 | 10 | 103 | if ischar(t)
|
| | 104 | % convert to cellstring.
|
< 0.001 | 10 | 105 | t = cellstr(t);
|
< 0.001 | 10 | 106 | end
|
< 0.001 | 10 | 107 | if ~iscellstr(t)
|
| | 108 | %At this point we should have a cell array. Otherwise error.
|
| | 109 | error(message('MATLAB:datevec:NotAStringArray'));
|
< 0.001 | 10 | 110 | end
|
< 0.001 | 10 | 111 | icu_dtformat = {};
|
< 0.001 | 10 | 112 | if ~any(isdateformat)
|
0.017 | 10 | 113 | format = getformat(t);
|
< 0.001 | 10 | 114 | if ~isempty(format)
|
0.003 | 10 | 115 | icu_dtformat = matlab.internal.datetime.cnv2icudf(format);
|
< 0.001 | 10 | 116 | end
|
| | 117 | else
|
| | 118 | icu_dtformat = matlab.internal.datetime.cnv2icudf(stringToLegacyText(varargin{isdateformat}));
|
< 0.001 | 10 | 119 | end
|
< 0.001 | 10 | 120 | if ~isempty(icu_dtformat)
|
| | 121 | % call ICU MEX function to parse date character vector to date vector
|
< 0.001 | 10 | 122 | if nargin < 2 || (nargin == 2 && any(isdateformat)) || isempty(pivotyear)
|
0.003 | 10 | 123 | y = dtstr2dtvecmx(t,icu_dtformat);
|
| | 124 | else
|
| | 125 | showyr = strfind(icu_dtformat, 'y');
|
| | 126 | if ~isempty(showyr)
|
| | 127 | wrtYr = numel(showyr);
|
| | 128 | if showyr(end) - showyr(1) >= wrtYr
|
| | 129 | error(message('MATLAB:datevec:YearFormat'));
|
| | 130 | end
|
| | 131 | switch wrtYr
|
| | 132 | case 4,
|
| | 133 | icu_dtformat = strrep(icu_dtformat,'yyyy','yy');
|
| | 134 | case 3,
|
| | 135 | icu_dtformat = strrep(icu_dtformat,'yyy','yy');
|
| | 136 | end
|
| | 137 | end
|
| | 138 | y = dtstr2dtvecmx(t,icu_dtformat,pivotyear);
|
< 0.001 | 10 | 139 | end
|
< 0.001 | 10 | 140 | if nargout > 1
|
| | 141 | mo = y(:,2);
|
| | 142 | d = y(:,3);
|
| | 143 | h = y(:,4);
|
| | 144 | mi = y(:,5);
|
| | 145 | s = y(:,6);
|
| | 146 | y = y(:,1);
|
< 0.001 | 10 | 147 | end
|
| | 148 | else
|
| | 149 | %last resort!!!
|
| | 150 | if ischar(t)
|
| | 151 | m = size(t,1);
|
| | 152 | else
|
| | 153 | m = length(t);
|
| | 154 | end
|
| | 155 | y = zeros(m,6);
|
| | 156 | t = lower(t);
|
| | 157 | ampmtokens = lower(getampmtokensmx);
|
| | 158 | amtok = ampmtokens{1};
|
| | 159 | pmtok = ampmtokens{2};
|
| | 160 | M = lower(getmonthnamesmx('shortloc'));
|
| | 161 | M0 = lower(getmonthnamesmx('short')); % list of English short month names.
|
| | 162 |
|
| | 163 | for i = 1:m
|
| | 164 | % Convert date input to date vector
|
| | 165 | % Initially, the six fields are all unknown.
|
| | 166 | c(1,1:6) = NaN;
|
| | 167 | pm = -1; % means am or pm is not in datestr
|
| | 168 | if ischar(t)
|
| | 169 | str = t(i,:);
|
| | 170 | else
|
| | 171 | str = t{i};
|
| | 172 | end
|
| | 173 | d = [' ' str ' '];
|
| | 174 |
|
| | 175 | % Replace 'a ' or 'am', 'p ' or 'pm' with ': '.
|
| | 176 | % If e is before 'p ', it could be Sep.
|
| | 177 | pat = 'a |am|(?<=[^e])p |pm';
|
| | 178 | idx = regexp(d, pat, 'start');
|
| | 179 | if ~isempty(idx)
|
| | 180 | idx = idx(end);
|
| | 181 | pm = strcmp(d(idx), 'p');
|
| | 182 | elseif (strcmp(amtok, 'am') == 0 || strcmp(pmtok, 'pm') == 0 )
|
| | 183 | %Search for local am/pm tokens
|
| | 184 | pat = [regexptranslate('escape', amtok) '|' regexptranslate('escape',pmtok) '|'];
|
| | 185 | idx = regexp(d, pat, 'start');
|
| | 186 | if ~isempty(idx)
|
| | 187 | idx = idx(end);
|
| | 188 | pm = strncmp(d(idx:end), pmtok, length(pmtok));
|
| | 189 | end
|
| | 190 | end
|
| | 191 | if ~isempty(idx)
|
| | 192 | if d(idx-1) == ' '
|
| | 193 | d(idx-1:idx+1) = ': ';
|
| | 194 | else
|
| | 195 | d(idx:idx+1) = ': ';
|
| | 196 | end
|
| | 197 | end
|
| | 198 |
|
| | 199 | % Any remaining letters must be in the month field
|
| | 200 | p = find(isletter(d));
|
| | 201 |
|
| | 202 | % Test length of character vector to catch a bogus date character vector.
|
| | 203 | % Get index of month in list of months of year
|
| | 204 | % replace with spaces, month name in date character vector.
|
| | 205 | % If English month name lookup fails, fall back on
|
| | 206 | % list of local month names.
|
| | 207 | if ~isempty(p) && numel(d)>4
|
| | 208 | k = min(p);
|
| | 209 | if length(d) >= k+ 3 && d(k+3) == '.', d(k+3) = ' '; end
|
| | 210 | monthidx = [];
|
| | 211 | if length(d) >= k+2
|
| | 212 | monthidx = ~cellfun('isempty',strfind(M0,d(k:k+2)));
|
| | 213 | if ~any(monthidx)
|
| | 214 | monthidx = ~cellfun('isempty',strfind(M,d(k:k+2)));
|
| | 215 | end
|
| | 216 | end
|
| | 217 | if ~any(monthidx)
|
| | 218 | error(message('MATLAB:datevec:MonthOfYear'));
|
| | 219 | end
|
| | 220 | c(2) = find(monthidx);
|
| | 221 | d(p) = char(' '*ones(size(p)));
|
| | 222 | end
|
| | 223 |
|
| | 224 | % Find all nonnumbers.
|
| | 225 | p = find((d < '0' | d > '9') & (d ~= '.'));
|
| | 226 |
|
| | 227 | % Pick off and classify numeric fields, one by one.
|
| | 228 | % Colons delinate hour, minutes and seconds.
|
| | 229 |
|
| | 230 | k = 1;
|
| | 231 | while k < length(p)
|
| | 232 | if d(p(k)) ~= ' ' && d(p(k)+1) == '-'
|
| | 233 | f = str2double(d(p(k)+1:p(k+2)-1));
|
| | 234 | k = k+1;
|
| | 235 | else
|
| | 236 | f = str2double(d(p(k)+1:p(k+1)-1));
|
| | 237 | end
|
| | 238 | if ~isnan(f)
|
| | 239 | if d(p(k))==':' || d(p(k+1))==':'
|
| | 240 | if isnan(c(4))
|
| | 241 | c(4) = f; % hour
|
| | 242 | % Add 12 if pm specified and hour isn't 12
|
| | 243 | if pm == 1 && f ~= 12
|
| | 244 | c(4) = f+12;
|
| | 245 | elseif pm == 0 && f == 12
|
| | 246 | c(4) = 0;
|
| | 247 | end
|
| | 248 | elseif isnan(c(5))
|
| | 249 | c(5) = f; % minutes
|
| | 250 | elseif isnan(c(6))
|
| | 251 | c(6) = f; % seconds
|
| | 252 | else
|
| | 253 | error(message('MATLAB:datevec:NumberOfTimeFields', str));
|
| | 254 | end
|
| | 255 | elseif isnan(c(2))
|
| | 256 | if f > 12
|
| | 257 | c(1) = f; %year
|
| | 258 | else
|
| | 259 | c(2) = f; % month
|
| | 260 | end
|
| | 261 | elseif isnan(c(3))
|
| | 262 | c(3) = f; % date
|
| | 263 | elseif isnan(c(1))
|
| | 264 | if (f >= 0) && (p(k+1)-p(k) == 3) % two char year
|
| | 265 | if nargin < 2
|
| | 266 | clk = clock;
|
| | 267 | pivotyear = clk(1)-50; %(current year-50 years)
|
| | 268 | end
|
| | 269 | % Moving 100 year window centered around current year
|
| | 270 | c(1) = pivotyear+rem(f+100-rem(pivotyear,100),100);
|
| | 271 | else
|
| | 272 | c(1) = f; % year
|
| | 273 | end
|
| | 274 | else
|
| | 275 | error(message('MATLAB:datevec:NumberOfDateFields', str));
|
| | 276 | end
|
| | 277 | end
|
| | 278 | k = k+1;
|
| | 279 | end
|
| | 280 |
|
| | 281 | if sum(isnan(c)) >= 5
|
| | 282 | error(message('MATLAB:datevec:ParseDateString', str));
|
| | 283 | end
|
| | 284 | % If any field has not been specified
|
| | 285 | if isnan(c(1)), clk = clock; c(1) = clk(1); end
|
| | 286 | if isnan(c(2)), c(2) = 1; end;
|
| | 287 | if isnan(c(3)), c(3) = 1; end;
|
| | 288 | if isnan(c(4)), c(4) = 0; end;
|
| | 289 | if isnan(c(5)), c(5) = 0; end;
|
| | 290 | if isnan(c(6)), c(6) = 0; end;
|
| | 291 |
|
| | 292 | % Normalize components to correct ranges.
|
| | 293 | y(i,:) = datevecmx(datenummx(c));
|
| | 294 | end
|
| | 295 | if nargout > 1
|
| | 296 | mo = y(:,2);
|
| | 297 | d = y(:,3);
|
| | 298 | h = y(:,4);
|
| | 299 | mi = y(:,5);
|
| | 300 | s = y(:,6);
|
| | 301 | y = y(:,1);
|
| | 302 | end
|
< 0.001 | 10 | 303 | end
|
| | 304 | elseif nargout <= 1
|
| | 305 | % date number was specified
|
| | 306 | y = datevecmx(t);
|
| | 307 | elseif nargout == 3
|
| | 308 | % date number was specified and first three date fields for output
|
| | 309 | [y,mo,d] = datevecmx(t);
|
| | 310 | else
|
| | 311 | % date number was specified and all six date fields for output
|
| | 312 | [y,mo,d,h,mi,s] = datevecmx(t);
|
< 0.001 | 10 | 313 | end
|
< 0.001 | 10 | 314 | end
|
Other subfunctions in this file are not included in this listing.