time | Calls | line |
|---|
| | 1 | function n = datenum(arg1,arg2,arg3,h,min,s)
|
| | 2 | %DATENUM Serial date number.
|
| | 3 | % NOTE: While serial date numbers can represent dates and times, it is
|
| | 4 | % recommended that you use datetime values to represent points in time,
|
| | 5 | % and duration or calendarDuration values to represent elapsed times.
|
| | 6 | %
|
| | 7 | % N = DATENUM(V) converts one or more date vectors V into serial date
|
| | 8 | % numbers N. Input V can be an M-by-6 or M-by-3 matrix containing M full
|
| | 9 | % or partial date vectors respectively. DATENUM returns a column vector
|
| | 10 | % of M date numbers.
|
| | 11 | %
|
| | 12 | % A date vector contains six elements, specifying year, month, day, hour,
|
| | 13 | % minute, and second. A partial date vector has three elements, specifying
|
| | 14 | % year, month, and day. Each element of V must be a positive double
|
| | 15 | % precision number. A serial date number of 1 corresponds to Jan-1-0000.
|
| | 16 | % The year 0000 is merely a reference point and is not intended to be
|
| | 17 | % interpreted as a real year.
|
| | 18 | %
|
| | 19 | % N = DATENUM(S,F) converts text representing one or more dates to serial
|
| | 20 | % date numbers N using format F. S can be a character array or string
|
| | 21 | % array where each row corresponds to one date, or one dimensional cell
|
| | 22 | % array of strings. DATENUM returns a column vector of M date numbers,
|
| | 23 | % where M is the number of strings in S.
|
| | 24 | %
|
| | 25 | % All of the date strings in S must have the same format F, which must be
|
| | 26 | % composed of date format symbols according to Table 2 in DATESTR help.
|
| | 27 | %
|
| | 28 | % Certain formats may not contain enough information to compute a date
|
| | 29 | % number. In those cases, hours, minutes, and seconds default to 0, days
|
| | 30 | % default to 1, months default to January, and years default to the
|
| | 31 | % current year. Date strings with two-character years (e.g. 19-May-88)
|
| | 32 | % are interpreted to be within the 100 years centered around the current
|
| | 33 | % year.
|
| | 34 | %
|
| | 35 | % N = DATENUM(S,F,P) or N = DATENUM(S,P,F) uses the specified format F
|
| | 36 | % and the pivot year P to determine the date number N, from S.
|
| | 37 | % The pivot year is the starting year of the 100-year range in
|
| | 38 | % which a two-character year resides. The default pivot year is the
|
| | 39 | % current year minus 50 years.
|
| | 40 | %
|
| | 41 | % N = DATENUM(Y,MO,D) and N = DATENUM([Y,MO,D]) return the serial date
|
| | 42 | % numbers for corresponding elements of the Y,MO,D (year,month,day)
|
| | 43 | % arrays. Y, MO, and D must be arrays of the same size (or any can be a
|
| | 44 | % scalar).
|
| | 45 | %
|
| | 46 | % N = DATENUM(Y,MO,D,H,MI,S) and N = DATENUM([Y,MO,D,H,MI,S]) return the
|
| | 47 | % serial date numbers for corresponding elements of the Y,MO,D,H,MI,S
|
| | 48 | % (year,month,day,hour,minute,second) arrays. The six arguments must be
|
| | 49 | % arrays of the same size (or any can be a scalar).
|
| | 50 | %
|
| | 51 | % N = DATENUM(V) converts date represented as date vector (as defined by
|
| | 52 | % DATEVEC) V into a serial date number.
|
| | 53 | %
|
| | 54 | % N = DATENUM(S) converts date represented as text S into a serial date
|
| | 55 | % number. S must be in one of the date formats 0,1,2,6,13,14,15,16,23 as
|
| | 56 | % defined by DATESTR. This calling syntax is provided for backward
|
| | 57 | % compatibility, and is significantly slower than the syntax which
|
| | 58 | % specifies the format. If the format is known, the N =
|
| | 59 | % DATENUM(S,F) syntax should be used.
|
| | 60 | %
|
| | 61 | % N = DATENUM(S,P) converts date represented as text S, using pivot year
|
| | 62 | % P. If the format is known, the N = DATENUM(S,F,P) or N = DATENUM(S,P,F)
|
| | 63 | % syntax should be used.
|
| | 64 | %
|
| | 65 | % Note: The vectorized calling syntax can offer significant performance
|
| | 66 | % improvement for large arrays.
|
| | 67 | %
|
| | 68 | % Examples:
|
| | 69 | % n = datenum('19-May-2000') returns n = 730625.
|
| | 70 | % n = datenum(2001,12,19) returns n = 731204.
|
| | 71 | % n = datenum(2001,12,19,18,0,0) returns n = 731204.75.
|
| | 72 | % n = datenum('19.05.2000','dd.mm.yyyy') returns n = 730625.
|
| | 73 | %
|
| | 74 | % See also DATETIME, DURATION, CALENDARDURATION, NOW, DATESTR, DATEVEC, DATETICK.
|
| | 75 |
|
| | 76 | % Copyright 1984-2020 The MathWorks, Inc.
|
| | 77 |
|
< 0.001 | 3 | 78 | narginchk(1, 6);
|
| | 79 |
|
| | 80 | % Shallow adoption of string
|
| 3 | 81 | arg1 = convertStringsToChars(arg1);
|
| | 82 |
|
| | 83 | % parse input arguments
|
| 3 | 84 | isdatestr = ~isnumeric(arg1);
|
| 3 | 85 | isdateformat = false;
|
< 0.001 | 3 | 86 | if nargin == 2
|
| | 87 | arg2 = convertStringsToChars(arg2);
|
| | 88 | isdateformat = ischar(arg2);
|
< 0.001 | 3 | 89 | elseif nargin == 3
|
| | 90 | arg2 = convertStringsToChars(arg2);
|
| | 91 | arg3 = convertStringsToChars(arg3);
|
| | 92 | isdateformat = [ischar(arg2), ischar(arg3)];
|
| 3 | 93 | end
|
| | 94 |
|
< 0.001 | 3 | 95 | if isdatestr && isempty(arg1)
|
| | 96 | n = zeros(0,1);
|
| | 97 | warning(message('MATLAB:datenum:EmptyDate'));
|
| | 98 | return;
|
| 3 | 99 | end
|
| | 100 |
|
| | 101 | % try to convert date character vector or date vector to a date number
|
| 3 | 102 | try
|
< 0.001 | 3 | 103 | switch nargin
|
< 0.001 | 3 | 104 | case 1
|
| 3 | 105 | if isdatestr
|
| | 106 | n = datenummx(datevec(arg1));
|
< 0.001 | 3 | 107 | elseif ((size(arg1,2)==3) || (size(arg1,2)==6)) && ...
|
| 3 | 108 | any(abs(arg1(:,1) - 2000) < 10000)
|
| 3 | 109 | if ~isreal(arg1)
|
| | 110 | error(message("MATLAB:datenum:InputMustBeReal"))
|
| 3 | 111 | end
|
< 0.001 | 3 | 112 | n = datenummx(arg1);
|
| | 113 | else
|
| | 114 | n = arg1;
|
< 0.001 | 3 | 115 | end
|
| | 116 | case 2
|
| | 117 | if isdateformat
|
| | 118 | if ischar(arg1)
|
| | 119 | arg1 = cellstr(arg1);
|
| | 120 | end
|
| | 121 | if ~iscellstr(arg1) %#ok<ISCLSTR>
|
| | 122 | %At this point we should have a cell array. Otherwise error.
|
| | 123 | error(message('MATLAB:datenum:NotAStringArray'));
|
| | 124 | end
|
| | 125 | if isempty(arg2)
|
| | 126 | n = datenummx(datevec(arg1));
|
| | 127 | else
|
| | 128 | n = dtstr2dtnummx(arg1,matlab.internal.datetime.cnv2icudf(arg2));
|
| | 129 | end
|
| | 130 | else
|
| | 131 | n = datenummx(datevec(arg1,arg2));
|
| | 132 | end
|
| | 133 | case 3
|
| | 134 | if any(isdateformat)
|
| | 135 | if isdateformat(1)
|
| | 136 | format = arg2;
|
| | 137 | pivot = arg3;
|
| | 138 | elseif isdateformat(2)
|
| | 139 | format = arg3;
|
| | 140 | pivot = arg2;
|
| | 141 | end
|
| | 142 | if ischar(arg1)
|
| | 143 | arg1 = cellstr(arg1);
|
| | 144 | end
|
| | 145 | if ~iscellstr(arg1)
|
| | 146 | %At this point we should have a cell array. Otherwise error.
|
| | 147 | error(message('MATLAB:datenum:NotAStringArray'));
|
| | 148 | end
|
| | 149 | icu_dtformat = matlab.internal.datetime.cnv2icudf(format);
|
| | 150 | showyr = strfind(icu_dtformat,'y');
|
| | 151 | if ~isempty(showyr)
|
| | 152 | wrtYr = numel(showyr);
|
| | 153 | checkYr = diff(showyr);
|
| | 154 | if any(checkYr~=1)
|
| | 155 | error(message('MATLAB:datenum:YearFormat'));
|
| | 156 | end
|
| | 157 | switch wrtYr
|
| | 158 | case 4
|
| | 159 | icu_dtformat = strrep(icu_dtformat,'yyyy','yy');
|
| | 160 | case 3
|
| | 161 | icu_dtformat = strrep(icu_dtformat,'yyy','yy');
|
| | 162 | end
|
| | 163 | end
|
| | 164 | if (isempty(format))
|
| | 165 | n = datenummx(datevec(arg1,pivot));
|
| | 166 | else
|
| | 167 | if (isempty(pivot))
|
| | 168 | n = dtstr2dtnummx(arg1,icu_dtformat);
|
| | 169 | else
|
| | 170 | n = dtstr2dtnummx(arg1,icu_dtformat,pivot);
|
| | 171 | end
|
| | 172 | end
|
| | 173 | else
|
| | 174 | verifySizes(arg1,arg2,arg3);
|
| | 175 | if ~isreal(arg1) || ~isreal(arg2) || ~isreal(arg3)
|
| | 176 | error(message("MATLAB:datenum:InputMustBeReal"))
|
| | 177 | end
|
| | 178 | n = datenummx(arg1,arg2,arg3);
|
| | 179 | end
|
| | 180 | case 6
|
| | 181 | verifySizes(arg1,arg2,arg3,h,min,s);
|
| | 182 | if ~isreal(arg1) || ~isreal(arg2) || ~isreal(arg3) || ~isreal(h) || ~isreal(min) || ~isreal(s)
|
| | 183 | error(message("MATLAB:datenum:InputMustBeReal"))
|
| | 184 | end
|
| | 185 | n = datenummx(arg1,arg2,arg3,h,min,s);
|
| | 186 | otherwise
|
| | 187 | error(message('MATLAB:datenum:Nargin'));
|
< 0.001 | 3 | 188 | end
|
| | 189 | catch exception
|
| | 190 | if (nargin == 1 && ~isdatestr)
|
| | 191 | identifier = 'MATLAB:datenum:ConvertDateNumber';
|
| | 192 | elseif (nargin == 1 && isdatestr) || (isdatestr && any(isdateformat))
|
| | 193 | identifier = 'MATLAB:datenum:ConvertDateString';
|
| | 194 | elseif (nargin > 1) && ~isdatestr && ~any(isdateformat)
|
| | 195 | identifier = 'MATLAB:datenum:ConvertDateVector';
|
| | 196 | else
|
| | 197 | identifier = exception.identifier;
|
| | 198 | end
|
| | 199 | newExc = MException( identifier,'%s',getString(message('MATLAB:datenum:Failed')));
|
| | 200 | newExc = newExc.addCause(exception);
|
| | 201 | throw(newExc);
|
< 0.001 | 3 | 202 | end
|
Other subfunctions in this file are not included in this listing.