This is a static copy of a profile reportHome
datevec (40 calls, 0.109 sec)
Generated 05-Aug-2011 13:00:27 using cpu time.
function in file /usr/local/MATLAB/R2011a/toolbox/matlab/timefun/datevec.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Function Name | Function Type | Calls |
datenum | function | 40 |
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
113 | icu_dtformat = cnv2icudf(forma... | 40 | 0.044 s | 40.0% |  |
111 | format = getformat(t); | 40 | 0.044 s | 40.0% |  |
121 | y = dtstr2dtvecmx(t,icu_dtform... | 40 | 0.011 s | 10.0% |  |
98 | if isdatestr | 40 | 0.011 s | 10.0% |  |
312 | end | 40 | 0 s | 0% |  |
All other lines | | | 0 s | 0% |  |
Totals | | | 0.109 s | 100% | |
Children (called functions)
Code Analyzer results
Line number | Message |
64 | Writing ERROR(NARGCHK(...)) is the appropriate way to call this function. |
123 | FINDSTR will be removed in a future release. Use STRFIND instead. |
Coverage results
[ Show coverage for parent directory ]
Total lines in function | 312 |
Non-code lines (comments, blank lines) | 103 |
Code lines (lines that can run) | 209 |
Code lines that did run | 22 |
Code lines that did not run | 187 |
Coverage (did run/can run) | 10.53 % |
Function listing
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. Formats with 'Q' are not accepted by DATEVEC.
14 % DATEVEC returns an M-by-6 matrix of date vectors, where M is the number
15 % of date strings in S.
16 %
17 % Certain formats may not contain enough information to compute a date
18 % vector. In those cases, hours, minutes, and seconds default to 0, days
19 % default to 1, months default to January, and years default to the
20 % current year. Date strings with two character years are interpreted to
21 % be within the 100 years centered around the current year.
22 %
23 % V = DATEVEC(S,F,P) or V = DATEVEC(S,P,F) converts the date string S to
24 % a date vector V, using the pivot year P and the date format F. The
25 % pivot year is the starting year of the 100-year range in which a
26 % two-character year resides. The default pivot year is the current year
27 % minus 50 years.
28 %
29 % [Y,MO,D,H,MI,S] = DATEVEC(...) takes any of the two syntaxes shown
30 % above and returns the components of the date vector as individual
31 % variables.
32 %
33 % V = DATEVEC(S) converts date string S to date vector V. S must be in
34 % one of the date formats 0,1,2,6,13,14,15,16,23 as defined by DATESTR.
35 % This calling syntax is provided for backward compatibility, and is
36 % significantly slower than the syntax which specifies the format string.
37 % If the format is known, the V = DATEVEC(S,F) syntax should be used.
38 %
39 % V = DATEVEC(S,P) converts the date string S using pivot year P.
40 % If the format is known, the V = DATEVEC(S,F,P) or V = DATEVEC(S,P,F)
41 % syntax should be used.
42 %
43 % Note 1: If more than one input argument is used, the first argument
44 % must be a date string or array of date strings.
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-2009 The MathWorks, Inc.
61 % $Revision: 1.28.4.21 $ $Date: 2010/08/23 23:13:39 $
62
40 63 if (nargin<1) || (nargin>3)
64 error('MATLAB:datevec:Nargin',nargchk(1,3,nargin));
65 end
66
67 % parse input arguments
40 68 isdatestr = ~isnumeric(t);
40 69 isdateformat = false;
40 70 if ~isdatestr && nargin > 1
71 warning(message('MATLAB:datevec:Inputs'));
40 72 elseif nargin > 1
73 isdateformat = cellfun('isclass',varargin,'char');
74 if (nargin == 3)
75 if ~isdateformat(1)
76 pivotyear = varargin{1};
77 elseif ~isdateformat(2)
78 pivotyear = varargin{2};
79 elseif isdateformat(1) && isdateformat(2)
80 error(message('MATLAB:datevec:DateFormat'));
81 end
82 elseif (nargin == 2) && ~isdateformat
83 pivotyear = varargin{1};
84 end
85 end
86
40 87 if isdatestr && isempty(t)
88 if nargout <= 1
89 y = zeros(0,6);
90 else
91 [y,mo,d,h,mi,s] = deal(zeros(0,0));
92 end;
93 warning(message('MATLAB:datevec:EmptyDate'));
94 return;
95 end
96
97 % branch to appropriate date string parser
0.01 40 98 if isdatestr
99 % a date format string was specified
100 % map date format to ICU date format tokens
40 101 if ischar(t)
102 % convert to cellstring.
40 103 t = cellstr(t);
40 104 end
40 105 if ~iscellstr(t)
106 %At this point we should have a cell array. Otherwise error.
107 error(message('MATLAB:datevec:NotAStringArray'));
108 end
40 109 icu_dtformat = {};
40 110 if ~any(isdateformat)
0.04 40 111 format = getformat(t);
40 112 if ~isempty(format)
0.04 40 113 icu_dtformat = cnv2icudf(format);
40 114 end
115 else
116 icu_dtformat = cnv2icudf(varargin{isdateformat});
117 end
40 118 if ~isempty(icu_dtformat)
119 % call ICU MEX function to parse date string to date vector
40 120 if nargin < 2 || (nargin == 2 && any(isdateformat)) || isempty(pivotyear)
0.01 40 121 y = dtstr2dtvecmx(t,icu_dtformat);
122 else
123 showyr = findstr(icu_dtformat,'y');
124 if ~isempty(showyr)
125 wrtYr = numel(showyr);
126 if showyr(end) - showyr(1) >= wrtYr
127 error(message('MATLAB:datevec:YearFormat'));
128 end
129 switch wrtYr
130 case 4,
131 icu_dtformat = strrep(icu_dtformat,'yyyy','yy');
132 case 3,
133 icu_dtformat = strrep(icu_dtformat,'yyy','yy');
134 end
135 end
136 y = dtstr2dtvecmx(t,icu_dtformat,pivotyear);
137 end
40 138 if nargout > 1
139 mo = y(:,2);
140 d = y(:,3);
141 h = y(:,4);
142 mi = y(:,5);
143 s = y(:,6);
144 y = y(:,1);
145 end
146 else
147 %last resort!!!
148 if ischar(t)
149 m = size(t,1);
150 else
151 m = length(t);
152 end
153 y = zeros(m,6);
154 t = lower(t);
155 ampmtokens = lower(getampmtokensmx);
156 amtok = ampmtokens{1};
157 pmtok = ampmtokens{2};
158 M = lower(getmonthnamesmx('shortloc'));
159 M0 = lower(getmonthnamesmx('short')); % list of English short month names.
160
161 for i = 1:m
162 % Convert date input to date vector
163 % Initially, the six fields are all unknown.
164 c(1,1:6) = NaN;
165 pm = -1; % means am or pm is not in datestr
166 if ischar(t)
167 str = t(i,:);
168 else
169 str = t{i};
170 end
171 d = [' ' str ' '];
172
173 % Replace 'a ' or 'am', 'p ' or 'pm' with ': '.
174 % If e is before 'p ', it could be Sep.
175 pat = 'a |am|(?<=[^e])p |pm';
176 idx = regexp(d, pat, 'start');
177 if ~isempty(idx)
178 idx = idx(end);
179 pm = strcmp(d(idx), 'p');
180 elseif (strcmp(amtok, 'am') == 0 || strcmp(pmtok, 'pm') == 0 )
181 %Search for local am/pm tokens
182 pat = [regexptranslate('escape', amtok) '|' regexptranslate('escape',pmtok) '|'];
183 idx = regexp(d, pat, 'start');
184 if ~isempty(idx)
185 idx = idx(end);
186 pm = strncmp(d(idx:end), pmtok, length(pmtok));
187 end
188 end
189 if ~isempty(idx)
190 if d(idx-1) == ' '
191 d(idx-1:idx+1) = ': ';
192 else
193 d(idx:idx+1) = ': ';
194 end
195 end
196
197 % Any remaining letters must be in the month field
198 p = find(isletter(d));
199
200 % Test length of string to catch a bogus date string.
201 % Get index of month in list of months of year
202 % replace with spaces, month name in date string.
203 % If English month name lookup fails, fall back on
204 % list of local month names.
205 if ~isempty(p) && numel(d)>4
206 k = min(p);
207 if length(d) >= k+ 3 && d(k+3) == '.', d(k+3) = ' '; end
208 monthidx = [];
209 if length(d) >= k+2
210 monthidx = ~cellfun('isempty',strfind(M0,d(k:k+2)));
211 if ~any(monthidx)
212 monthidx = ~cellfun('isempty',strfind(M,d(k:k+2)));
213 end
214 end
215 if ~any(monthidx)
216 error(message('MATLAB:datevec:MonthOfYear'));
217 end
218 c(2) = find(monthidx);
219 d(p) = char(' '*ones(size(p)));
220 end
221
222 % Find all nonnumbers.
223 p = find((d < '0' | d > '9') & (d ~= '.'));
224
225 % Pick off and classify numeric fields, one by one.
226 % Colons delinate hour, minutes and seconds.
227
228 k = 1;
229 while k < length(p)
230 if d(p(k)) ~= ' ' && d(p(k)+1) == '-'
231 f = str2double(d(p(k)+1:p(k+2)-1));
232 k = k+1;
233 else
234 f = str2double(d(p(k)+1:p(k+1)-1));
235 end
236 if ~isnan(f)
237 if d(p(k))==':' || d(p(k+1))==':'
238 if isnan(c(4))
239 c(4) = f; % hour
240 % Add 12 if pm specified and hour isn't 12
241 if pm == 1 && f ~= 12
242 c(4) = f+12;
243 elseif pm == 0 && f == 12
244 c(4) = 0;
245 end
246 elseif isnan(c(5))
247 c(5) = f; % minutes
248 elseif isnan(c(6))
249 c(6) = f; % seconds
250 else
251 error(message('MATLAB:datevec:NumberOfTimeFields', str));
252 end
253 elseif isnan(c(2))
254 if f > 12
255 c(1) = f; %year
256 else
257 c(2) = f; % month
258 end
259 elseif isnan(c(3))
260 c(3) = f; % date
261 elseif isnan(c(1))
262 if (f >= 0) && (p(k+1)-p(k) == 3) % two char year
263 if nargin < 2
264 clk = clock;
265 pivotyear = clk(1)-50; %(current year-50 years)
266 end
267 % Moving 100 year window centered around current year
268 c(1) = pivotyear+rem(f+100-rem(pivotyear,100),100);
269 else
270 c(1) = f; % year
271 end
272 else
273 error(message('MATLAB:datevec:NumberOfDateFields', str));
274 end
275 end
276 k = k+1;
277 end
278
279 if sum(isnan(c)) >= 5
280 error(message('MATLAB:datevec:ParseDateString', str));
281 end
282 % If any field has not been specified
283 if isnan(c(1)), clk = clock; c(1) = clk(1); end
284 if isnan(c(2)), c(2) = 1; end;
285 if isnan(c(3)), c(3) = 1; end;
286 if isnan(c(4)), c(4) = 0; end;
287 if isnan(c(5)), c(5) = 0; end;
288 if isnan(c(6)), c(6) = 0; end;
289
290 % Normalize components to correct ranges.
291 y(i,:) = datevecmx(datenummx(c));
292 end
293 if nargout > 1
294 mo = y(:,2);
295 d = y(:,3);
296 h = y(:,4);
297 mi = y(:,5);
298 s = y(:,6);
299 y = y(:,1);
300 end
301 end
302 elseif nargout <= 1
303 % date number was specified
304 y = datevecmx(t);
305 elseif nargout == 3
306 % date number was specified and first three date fields for output
307 [y,mo,d] = datevecmx(t);
308 else
309 % date number was specified and all six date fields for output
310 [y,mo,d,h,mi,s] = datevecmx(t);
311 end
40 312 end
Other subfunctions in this file are not included in this listing.