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