Home > . > datenum_uvmat.m

datenum_uvmat

PURPOSE ^

'datenum_uvmat': like the Matlab function datenum.m but gives empty output instead of stopping for input error

SYNOPSIS ^

function n = datenum_uvmat(arg1,arg2,arg3,h,min,s)

DESCRIPTION ^

'datenum_uvmat': like the Matlab function datenum.m but gives empty output instead of stopping for input error
--------------------------------------------------------------------------
DATENUM Serial date number.
    N = DATENUM(V) converts one or more date vectors V into serial date 
    numbers N. Input V can be an M-by-6 or M-by-3 matrix containing M full 
    or partial date vectors respectively.  DATENUM returns a column vector
    of M date numbers.

    A date vector contains six elements, specifying year, month, day, hour, 
    minute, and second. A partial date vector has three elements, specifying 
    year, month, and day.  Each element of V must be a positive double 
    precision number.  A serial date number of 1 corresponds to Jan-1-0000.  
    The year 0000 is merely a reference point and is not intended to be 
    interpreted as a real year.

    N = DATENUM(S,F) converts one or more date strings S to serial date 
    numbers N using format string F. S can be a character array where each
    row corresponds to one date string, or one dimensional cell array of 
    strings.  DATENUM returns a column vector of M date numbers, where M is 
    the number of strings in S. 

    All of the date strings in S must have the same format F, which must be
    composed of date format symbols according to Table 2 in DATESTR help.
    Formats with 'Q' are not accepted by DATENUM.  

    Certain formats may not contain enough information to compute a date
    number.  In those cases, hours, minutes, and seconds default to 0, days
    default to 1, months default to January, and years default to the
    current year. Date strings with two character years are interpreted to
    be within the 100 years centered around the current year.

    N = DATENUM(S,F,P) or N = DATENUM(S,P,F) uses the specified format F
    and the pivot year P to determine the date number N, given the date
    string S.  The pivot year is the starting year of the 100-year range in 
    which a two-character year resides.  The default pivot year is the 
    current year minus 50 years.

    N = DATENUM(Y,MO,D) and N = DATENUM([Y,MO,D]) return the serial date
    numbers for corresponding elements of the Y,MO,D (year,month,day)
    arrays. Y, MO, and D must be arrays of the same size (or any can be a
    scalar).

    N = DATENUM(Y,MO,D,H,MI,S) and N = DATENUM([Y,MO,D,H,MI,S]) return the
    serial date numbers for corresponding elements of the Y,MO,D,H,MI,S
    (year,month,day,hour,minute,second) arrays.  The six arguments must be
    arrays of the same size (or any can be a scalar).

    N = DATENUM(S) converts the string or date vector (as defined by 
    DATEVEC) S into a serial date number.  If S is a string, it must be in 
    one of the date formats 0,1,2,6,13,14,15,16,23 as defined by DATESTR.
    This calling syntax is provided for backward compatibility, and is
    significantly slower than the syntax which specifies the format string.
    If the format is known, the N = DATENUM(S,F) syntax should be used.

    N = DATENUM(S,P) converts the date string S, using pivot year P. If the 
    format is known, the N = DATENUM(S,F,P) or N = DATENUM(S,P,F) syntax 
    should be used.

    Note:  The vectorized calling syntax can offer significant performance
    improvement for large arrays.

    Examples:
        n = datenum('19-May-2000') returns n = 730625. 
        n = datenum(2001,12,19) returns n = 731204. 
        n = datenum(2001,12,19,18,0,0) returns n = 731204.75. 
        n = datenum('19.05.2000','dd.mm.yyyy') returns n = 730625.

    See also NOW, DATESTR, DATEVEC, DATETICK.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %'datenum_uvmat': like the Matlab function datenum.m but gives empty output instead of stopping for input error
0002 %--------------------------------------------------------------------------
0003 function n = datenum_uvmat(arg1,arg2,arg3,h,min,s)
0004 %DATENUM Serial date number.
0005 %    N = DATENUM(V) converts one or more date vectors V into serial date
0006 %    numbers N. Input V can be an M-by-6 or M-by-3 matrix containing M full
0007 %    or partial date vectors respectively.  DATENUM returns a column vector
0008 %    of M date numbers.
0009 %
0010 %    A date vector contains six elements, specifying year, month, day, hour,
0011 %    minute, and second. A partial date vector has three elements, specifying
0012 %    year, month, and day.  Each element of V must be a positive double
0013 %    precision number.  A serial date number of 1 corresponds to Jan-1-0000.
0014 %    The year 0000 is merely a reference point and is not intended to be
0015 %    interpreted as a real year.
0016 %
0017 %    N = DATENUM(S,F) converts one or more date strings S to serial date
0018 %    numbers N using format string F. S can be a character array where each
0019 %    row corresponds to one date string, or one dimensional cell array of
0020 %    strings.  DATENUM returns a column vector of M date numbers, where M is
0021 %    the number of strings in S.
0022 %
0023 %    All of the date strings in S must have the same format F, which must be
0024 %    composed of date format symbols according to Table 2 in DATESTR help.
0025 %    Formats with 'Q' are not accepted by DATENUM.
0026 %
0027 %    Certain formats may not contain enough information to compute a date
0028 %    number.  In those cases, hours, minutes, and seconds default to 0, days
0029 %    default to 1, months default to January, and years default to the
0030 %    current year. Date strings with two character years are interpreted to
0031 %    be within the 100 years centered around the current year.
0032 %
0033 %    N = DATENUM(S,F,P) or N = DATENUM(S,P,F) uses the specified format F
0034 %    and the pivot year P to determine the date number N, given the date
0035 %    string S.  The pivot year is the starting year of the 100-year range in
0036 %    which a two-character year resides.  The default pivot year is the
0037 %    current year minus 50 years.
0038 %
0039 %    N = DATENUM(Y,MO,D) and N = DATENUM([Y,MO,D]) return the serial date
0040 %    numbers for corresponding elements of the Y,MO,D (year,month,day)
0041 %    arrays. Y, MO, and D must be arrays of the same size (or any can be a
0042 %    scalar).
0043 %
0044 %    N = DATENUM(Y,MO,D,H,MI,S) and N = DATENUM([Y,MO,D,H,MI,S]) return the
0045 %    serial date numbers for corresponding elements of the Y,MO,D,H,MI,S
0046 %    (year,month,day,hour,minute,second) arrays.  The six arguments must be
0047 %    arrays of the same size (or any can be a scalar).
0048 %
0049 %    N = DATENUM(S) converts the string or date vector (as defined by
0050 %    DATEVEC) S into a serial date number.  If S is a string, it must be in
0051 %    one of the date formats 0,1,2,6,13,14,15,16,23 as defined by DATESTR.
0052 %    This calling syntax is provided for backward compatibility, and is
0053 %    significantly slower than the syntax which specifies the format string.
0054 %    If the format is known, the N = DATENUM(S,F) syntax should be used.
0055 %
0056 %    N = DATENUM(S,P) converts the date string S, using pivot year P. If the
0057 %    format is known, the N = DATENUM(S,F,P) or N = DATENUM(S,P,F) syntax
0058 %    should be used.
0059 %
0060 %    Note:  The vectorized calling syntax can offer significant performance
0061 %    improvement for large arrays.
0062 %
0063 %    Examples:
0064 %        n = datenum('19-May-2000') returns n = 730625.
0065 %        n = datenum(2001,12,19) returns n = 731204.
0066 %        n = datenum(2001,12,19,18,0,0) returns n = 731204.75.
0067 %        n = datenum('19.05.2000','dd.mm.yyyy') returns n = 730625.
0068 %
0069 %    See also NOW, DATESTR, DATEVEC, DATETICK.
0070 
0071 %   Copyright 1984-2004 The MathWorks, Inc.
0072 %   $Revision: 1.24.4.7 $  $Date: 2004/10/27 23:53:57 $
0073 
0074 if (nargin<1) || (nargin>6)
0075     error('MATLAB:datenumr:Nargin',nargchk(1,6,nargin));
0076 end
0077 
0078 % parse input arguments
0079 isdatestr = ~isnumeric(arg1);
0080 isdateformat = false;
0081 if nargin == 2
0082     isdateformat = ischar(arg2);
0083 elseif nargin == 3
0084     isdateformat = [ischar(arg2),ischar(arg3)];
0085 end
0086 % try to convert date string or date vector to a date number
0087 try
0088     switch nargin
0089         case 1 
0090             if isdatestr
0091                 n = datenummx(datevec(arg1));
0092             elseif (size(arg1,2)==3) || (size(arg1,2)==6)
0093                 n = datenummx(arg1);
0094             else
0095                 n = arg1;
0096             end
0097         case 2
0098             if isdateformat
0099                 if ischar(arg1)
0100                     arg1 = cellstr(arg1);
0101                 end
0102                 n = dtstr2dtnummx(arg1,cnv2icudf(arg2));
0103             else
0104                 n = datenummx(datevec(arg1,arg2));
0105             end
0106         case 3
0107             if any(isdateformat)
0108                 if isdateformat(1) 
0109                     format = arg2;
0110                     pivot = arg3;
0111                 elseif isdateformat(2)
0112                     format = arg3;
0113                     pivot = arg2;
0114                 end
0115                 if ischar(arg1)
0116                     arg1 = cellstr(arg1);
0117                 end
0118                 icu_dtformat = cnv2icudf(format);
0119                 showyr =  strfind(icu_dtformat,'y'); 
0120                 if ~isempty(showyr)
0121                     wrtYr =  numel(showyr);
0122                     checkYr = diff(showyr);
0123                     if any(checkYr~=1)
0124                         error('MATLAB:datenum:YearFormat','Unrecognized year format');
0125                     end
0126                     switch wrtYr
0127                         case 4,
0128                             icu_dtformat = strrep(icu_dtformat,'yyyy','yy');
0129                         case 3,
0130                             icu_dtformat = strrep(icu_dtformat,'yyy','yy');
0131                     end
0132                 end
0133                 n = dtstr2dtnummx(arg1,icu_dtformat,pivot);
0134             else
0135                 n = datenummx(arg1,arg2,arg3);
0136             end
0137         case 6, n = datenummx(arg1,arg2,arg3,h,min,s);
0138         otherwise, error('MATLAB:datenum:Nargin',...
0139                          'Incorrect number of arguments');
0140     end
0141 catch
0142     err = lasterror;
0143     err.message = sprintf('DATENUM failed.\n%s',err.message);
0144     
0145     if (nargin == 1 && ~isdatestr)
0146         err.identifier = 'MATLAB:datenum:ConvertDateNumber';
0147     elseif (nargin == 1 && isdatestr) || (isdatestr && any(isdateformat))
0148         err.identifier = 'MATLAB:datenum:ConvertDateString';
0149     elseif (nargin > 1) && ~isdatestr && ~any(isdateformat)
0150         err.identifier = 'MATLAB:datenum:ConvertDateVector';
0151     end
0152  
0153     rethrow(err);
0154 end
0155 end
0156 
0157 function [y,mo,d,h,mi,s] = datevec(t,varargin)
0158 %DATEVEC Date components.
0159 %    V = DATEVEC(N) converts one or more date numbers N to date vectors V. N
0160 %    can be a scalar, vector, or multidimensional array of positive date
0161 %    numbers. DATEVEC returns an M-by-6 matrix containing M date vectors,
0162 %    where M is the total number of date numbers in N.
0163 %
0164 %    V = DATEVEC(S,F) converts one or more date strings S to date vectors
0165 %    V using format string F to interpret the date strings in S. S can be a
0166 %    cell array of strings or a character array where each row corresponds
0167 %    to one date string. All of the date strings in S must have the same
0168 %    format which must be composed of date format symbols according to
0169 %    Table 2 in DATESTR help. Formats with 'Q' are not accepted by DATEVEC.
0170 %    DATEVEC returns an M-by-6 matrix of date vectors, where M is the number
0171 %    of date strings in S.
0172 %
0173 %    Certain formats may not contain enough information to compute a date
0174 %    vector.  In those cases, hours, minutes, and seconds default to 0, days
0175 %    default to 1, months default to January, and years default to the
0176 %    current year. Date strings with two character years are interpreted to
0177 %    be within the 100 years centered around the current year.
0178 %
0179 %    V = DATEVEC(S,F,P) or V = DATEVEC(S,P,F) converts the date string S to
0180 %    a date vector V, using the pivot year P and the date format F.  The
0181 %    pivot year is the starting year of the 100-year range in which a
0182 %    two-character year resides.  The default pivot year is the current year
0183 %    minus 50 years.
0184 %
0185 %    [Y,MO,D,H,MI,S] = DATEVEC(...) takes any of the two syntaxes shown
0186 %    above and returns the components of the date vector as individual
0187 %    variables.
0188 %
0189 %    V = DATEVEC(S) converts date string S to date vector V. S must be in
0190 %    one of the date formats 0,1,2,6,13,14,15,16,23 as defined by DATESTR.
0191 %    This calling syntax is provided for backward compatibility, and is
0192 %    significantly slower than the syntax which specifies the format string.
0193 %    If the format is known, the V = DATEVEC(S,F) syntax should be used.
0194 %
0195 %    V = DAVEVEC(S,P) converts the date string S using pivot year P.
0196 %    If the format is known, the V = DATEVEC(S,F,P) or V = DATEVEC(S,P,F)
0197 %    syntax should be used.
0198 %
0199 %    Note 1:  If more than one input argument is used, the first argument
0200 %    must be a date string or array of date strings.
0201 %
0202 %    Note 2:  The vectorized calling syntax can offer significant performance
0203 %    improvement for large arrays.
0204 %
0205 %    Examples
0206 %        d = '12/24/1984';
0207 %        t = 725000.00;
0208 %        c = datevec(d) or c = datevec(t) produce c = [1984 12 24 0 0 0].
0209 %        [y,m,d,h,mi,s] = datevec(d) returns y=1984, m=12, d=24, h=0, mi=0, s=0.
0210 %        c = datevec('5/6/03') produces c = [2003 5 6 0 0 0] until 2054.
0211 %        c = datevec('5/6/03',1900) produces c = [1903 5 6 0 0 0].
0212 %        c = datevec('19.05.2000','dd.mm.yyyy') produces c = [2000 5 19 0 0 0].
0213 %
0214 %    See also DATENUM, DATESTR, CLOCK, DATETICK.
0215 
0216 %    Copyright 1984-2004 The MathWorks, Inc.
0217 %    $Revision: 1.28.4.11 $  $Date: 2004/10/27 23:53:59 $
0218 
0219 if (nargin<1) || (nargin>3)
0220 %     error('MATLAB:datevec:Nargin',nargchk(1,3,nargin));
0221     return
0222 end
0223 
0224 % parse input arguments
0225 isdatestr = ~isnumeric(t);
0226 isdateformat = false;
0227 if ~isdatestr && nargin > 1
0228     warning('MATLAB:datevec:Inputs', ...
0229         'Unless the first input argument is a date string, all subsequent\narguments will be ignored.');
0230 elseif nargin > 1
0231     isdateformat = cellfun('isclass',varargin,'char');
0232     if (nargin == 3)
0233         if ~isdateformat(1)
0234             pivotyear = varargin{1};
0235         elseif ~isdateformat(2)
0236             pivotyear = varargin{2};
0237         elseif isdateformat(1) && isdateformat(2)
0238 %             error('MATLAB:datevec:DateFormat',...
0239 %                 'You specified two date format strings.\nThere can only be one.');
0240            return
0241         end 
0242     elseif (nargin == 2) && ~isdateformat
0243         pivotyear = varargin{1};
0244     end
0245 end
0246 
0247 if isdatestr && isempty(t)
0248     if nargout <= 1
0249         y = zeros(0,6);
0250     else
0251         [y,mo,d,h,mi,s] = deal(zeros(0,0));
0252     end;
0253     warning('MATLAB:datevec:EmptyDate',...
0254         'Usage of DATEVEC with empty date strings is not supported.\nResults may change in future versions.');
0255     return;
0256 end
0257 
0258 % branch to appropriate date string parser
0259 if  isdatestr
0260     % a date format string was specified
0261     % map date format to ICU date format tokens
0262     if ischar(t)
0263         % convert to cellstring.
0264         t = cellstr(t);
0265     end
0266     icu_dtformat = {};
0267     if ~any(isdateformat)
0268         format = getformat(t);
0269         if ~isempty(format)
0270             icu_dtformat = cnv2icudf(format);
0271         end
0272     else
0273         icu_dtformat = cnv2icudf(varargin{isdateformat});
0274     end
0275     if ~isempty(icu_dtformat) 
0276         % call ICU MEX function to parse date string to date vector
0277         try
0278             if nargin < 2 || (nargin == 2 && any(isdateformat))
0279                 y = dtstr2dtvecmx(t,icu_dtformat);
0280             else
0281                 showyr =  findstr(icu_dtformat,'y'); 
0282                 if ~isempty(showyr)
0283                     wrtYr =  numel(showyr);
0284                     if showyr(end) - showyr(1) >= wrtYr
0285 %                         error('MATLAB:datevec:YearFormat','Unrecognized year format');
0286                         return
0287                     end
0288                     switch wrtYr
0289                         case 4,
0290                             icu_dtformat = strrep(icu_dtformat,'yyyy','yy');
0291                         case 3,
0292                             icu_dtformat = strrep(icu_dtformat,'yyy','yy');
0293                     end
0294                 end
0295                 y = dtstr2dtvecmx(t,icu_dtformat,pivotyear);
0296             end
0297             if nargout > 1
0298                 mo = y(:,2);
0299                 d  = y(:,3);
0300                 h  = y(:,4);
0301                 mi = y(:,5);
0302                 s  = y(:,6);
0303                 y  = y(:,1);
0304             end
0305         catch
0306             err = lasterror;
0307             err.identifier = 'MATLAB:datevec:dtstr2dtvecmx';
0308             err.message = sprintf(['DATEVEC failed, calling DTSTR2DTVECMX.\n'... 
0309                     '%s'],err.message);
0310             rethrow(err);
0311         end 
0312     else
0313         %last resort!!!
0314        if ischar(t)
0315           m = size(t,1);
0316        else
0317           m = length(t);
0318        end
0319        y = zeros(m,6);
0320        t = lower(t);
0321 %        ampmtokens = lower(getampmtokensmx);
0322         amtok = 'am'; %ampmtokens{1};
0323        amtok0 = 'am';
0324         pmtok = 'pm';%ampmtokens{2};
0325        pmtok0 = 'pm';
0326        M={'jan';'feb';'mar';'apr';'may';'jun';'jul';'aug';'sept';'oct';'nov';'dec'};
0327        M0=M;
0328 %        M = lower(getmonthnames);
0329 %        M0 = lower(getmonthnames(0)); % fall-back list of English short month names.
0330        try
0331            for i = 1:m
0332                % Convert date input to date vector
0333                % Initially, the six fields are all unknown.
0334                c(1,1:6) = NaN;
0335                pm = -1; % means am or pm is not in datestr
0336                if ischar(t)
0337                    str = t(i,:);
0338                else
0339                    str = t{i};
0340                end
0341                d = [' ' str ' '];
0342 
0343                % Replace 'a ' or 'am', 'p ' or 'pm' with ': '.
0344                p = max(find(d == amtok(1) | d == pmtok(1) | ...
0345                             d == amtok0(1)| d == pmtok0(1)));
0346                if ~isempty(p)
0347                    if (d(p+1) == amtok(2) | ...
0348                        d(p+1) == amtok0(2)| isspace(d(p+1))) & ...
0349                        d(p-1) ~= lower('e')
0350                        pm = (d(p) == pmtok(1) | d(p) == pmtok0(1));
0351                        if d(p-1) == ' '
0352                            d(p-1:p+1) = ':  ';
0353                        else
0354                            d(p:p+1) = ': ';
0355                        end
0356                    end
0357                end
0358 
0359                % Any remaining letters must be in the month field
0360                p = find(isletter(d));
0361 
0362                % Test length of string to catch a bogus date string.
0363                % Get index of month in list of months of year
0364                % replace with spaces, month name in date string.
0365                % If native month name lookup fails, fall back on
0366                % list of English month names.
0367                if ~isempty(p) && numel(d)>4
0368                    k = min(p);
0369                    if d(k+3) == '.', d(k+3) = ' '; end
0370                    monthidx = ~cellfun('isempty',strfind(M,d(k:k+2)));
0371                    if ~any(monthidx)
0372                        monthidx = ~cellfun('isempty',strfind(M0,d(k:k+2)));
0373                        if ~any(monthidx)
0374 %                            error('MATLAB:datevec:MonthOfYear',...
0375 %                                'Failed to lookup month of year.');
0376                           return
0377                        end
0378                    end
0379                    c(2) = find(monthidx);
0380                    d(p) = char(' '*ones(size(p)));
0381                end
0382 
0383                % Find all nonnumbers.
0384                p = find((d < '0' | d > '9') & (d ~= '.'));
0385 
0386                % Pick off and classify numeric fields, one by one.
0387                % Colons delinate hour, minutes and seconds.
0388 
0389                k = 1;
0390                while k < length(p)
0391                    if d(p(k)) ~= ' ' && d(p(k)+1) == '-'
0392                        f = str2double(d(p(k)+1:p(k+2)-1));
0393                        k = k+1;
0394                    else
0395                        f = str2double(d(p(k)+1:p(k+1)-1));
0396                    end
0397                    if ~isnan(f)
0398                        if d(p(k))==':' || d(p(k+1))==':'
0399                            if isnan(c(4))
0400                                c(4) = f;             % hour
0401                                % Add 12 if pm specified and hour isn't 12
0402                                if pm == 1 && f ~= 12 
0403                                    c(4) = f+12;
0404                                elseif pm == 0 && f == 12
0405                                    c(4) = 0;
0406                                end
0407                            elseif isnan(c(5))
0408                                c(5) = f;             % minutes
0409                            elseif isnan(c(6)) 
0410                                c(6) = f;             % seconds
0411                            else
0412 %                                error('MATLAB:datevec:NumberOfTimeFields',...
0413 %                                    'Too many time fields in %s', str);
0414                                 return
0415                            end
0416                        elseif isnan(c(2))
0417                            if f > 12
0418 %                                error('MATLAB:datevec:IllegalDateField',...
0419 %                                    '%s is too large to be a month.',num2str(f));
0420                                 return
0421                            end
0422                            c(2) = f;                % month
0423                        elseif isnan(c(3))
0424                            c(3) = f;                % date
0425                        elseif isnan(c(1))
0426                            if (f >= 0) & (p(k+1)-p(k) == 3) % two char year
0427                                if nargin < 2
0428                                    clk = clock;
0429                                    pivotyear = clk(1)-50;  %(current year-50 years)
0430                                end
0431                                % Moving 100 year window centered around current year
0432                                c(1) = pivotyear+rem(f+100-rem(pivotyear,100),100);
0433                            else
0434                                c(1) = f;             % year
0435                            end
0436                        else
0437 %                            error('MATLAB:datevec:NumberOfDateFields',...
0438 %                                'Too many date fields in %s', str);
0439                             return
0440                        end
0441                    end
0442                    k = k+1;
0443                end
0444 
0445                if sum(isnan(c)) >= 5
0446 %                    error('MATLAB:datevec:ParseDateString',...
0447 %                        'Cannot parse date %s', str);
0448                     return
0449                end
0450               % If any field has not been specified
0451                if isnan(c(1)), clk = clock; c(1) = clk(1); end
0452                if isnan(c(2)), c(2) = 1; end;
0453                if isnan(c(3)), c(3) = 1; end;
0454                if isnan(c(4)), c(4) = 0; end;               
0455                if isnan(c(5)), c(5) = 0; end;                   
0456                if isnan(c(6)), c(6) = 0; end;
0457 
0458                % Normalize components to correct ranges.
0459                y(i,:) = datevecmx(datenummx(c));
0460            end
0461        catch
0462            err = lasterror;
0463            err.message = sprintf('Failed to parse date string.\n%s',...
0464                                  err.message);
0465            rethrow(err);
0466        end 
0467        if nargout > 1
0468            mo = y(:,2);
0469            d  = y(:,3);
0470            h  = y(:,4);
0471            mi = y(:,5);
0472            s  = y(:,6);
0473            y  = y(:,1);
0474        end
0475     end
0476 elseif nargout <= 1
0477    % date number was specified
0478    y = datevecmx(t);
0479 elseif nargout == 3
0480     % date number was specified and first three date fields for output
0481    [y,mo,d] = datevecmx(t);
0482 else
0483    % date number was specified and all six date fields for output
0484    [y,mo,d,h,mi,s] = datevecmx(t);
0485 end
0486 end
0487 %--------------------------------------------------------------------------
0488 %--
0489 function [format] = getformat(str)
0490   format = '';
0491   formatstr = cell(11,1);
0492   formatstr(1) = {'dd-mmm-yyyy HH:MM:SS'};
0493   formatstr(2) = {'dd-mmm-yyyy'};
0494   formatstr(3) = {'mm/dd/yy'};
0495   formatstr(4) = {'mm/dd'};
0496   formatstr(5) = {'HH:MM:SS'};
0497   formatstr(6) = {'HH:MM:SS PM'};
0498   formatstr(7) = {'HH:MM'};
0499   formatstr(8) = {'HH:MM PM'};
0500   formatstr(9) = {'mm/dd/yyyy'};
0501   formatstr(10) = {'dd-mmm-yyyy HH:MM'};  %used by finance
0502   formatstr(11) = {'dd-mmm-yy'};  %used by finance
0503   
0504   AlphaFormats = [1 1 0 0 0 1 0 1 0 1 1];
0505   %[1 2 6 8 10 11];
0506   SlashFormats = [ 0 0 1 1 0 0 0 0 1 0 0];
0507   %[3 4 9];
0508   TwoSlashFormats = [ 0 0 1 0 0 0 0 0 1 0 0];
0509   %[3 9];
0510   DashFormats = [ 1 1 0 0 0 0 0 0 0 1 1];
0511   %[1 2 10 11];
0512   ColonFormats = [1 0 0 0 1 1 1 1 0 1 0];
0513   %[1 5 6 7 8 10];
0514   TwoColonFormats = [1 0 0 0 1 1 0 0 0 0 0];
0515   %[1 5 6];
0516   SpaceFormats = [1 0 0 0 0 1 0 1 0 1 0];
0517   %[1 6 8 10];
0518   
0519   bMask = [ 1 1 1 1 1 1 1 1 1 1 1];
0520   
0521   if length(str) > 1
0522       str = str(1,1);
0523   end
0524   str = strtrim(char(str));
0525   slashes = strfind(str, '/');
0526   if ~isempty(slashes)
0527       bMask = bMask & SlashFormats;
0528       if (length(slashes) > 0 && slashes(1) == 2)
0529           if (length(slashes) > 1 && slashes(2) == 4)
0530               str = ['0' str(1:slashes(1)) '0' str(slashes(1)+1:end)];
0531           else
0532               str = ['0' str];
0533           end
0534       elseif (length(slashes) > 1 && slashes(2) - slashes(1) == 2)
0535           str = [str(1:slashes(1)) '0' str(slashes(1)+1:end)];
0536       end
0537       if length(slashes) > 1
0538           bMask = bMask & TwoSlashFormats;
0539       else
0540           bMask = bMask & ~TwoSlashFormats;
0541       end
0542   else
0543       bMask = bMask & ~SlashFormats;
0544   end
0545   
0546   dashes = strfind(str,'-');
0547   if ~isempty(dashes)
0548       bMask = bMask & DashFormats;
0549       if (length(dashes) > 0 && dashes(1) == 2)
0550         str = ['0' str];
0551       end
0552   else
0553       bMask = bMask & ~DashFormats;      
0554   end
0555   
0556   colons = strfind(str,':');
0557   if ~isempty(colons)
0558       bMask = bMask & ColonFormats;
0559       if (length(colons) > 0) && (colons(1) == 2) && (length(str) - colons(end) > 3)
0560         str = ['0' str];
0561       end
0562       if length(colons) > 1
0563           bMask = bMask & TwoColonFormats;
0564       else
0565           bMask = bMask & ~TwoColonFormats;
0566       end      
0567   else
0568       bMask = bMask & ~ColonFormats;
0569   end      
0570   
0571   spaces = strfind(str,' ');
0572   if ~isempty(spaces)
0573       bMask = bMask & SpaceFormats;
0574   else
0575       bMask = bMask & ~SpaceFormats;
0576   end
0577   
0578   for i = 1:11
0579       if bMask(i)
0580           try
0581               str1 = dateformverify(str,char(formatstr(i)));
0582             if (strcmpi(str, strtrim(str1)) == 1)
0583                 format = char(formatstr(i));
0584                 break;
0585             end
0586           catch
0587                lasterr('');
0588           end
0589           if AlphaFormats(i)
0590               try
0591                 str1 = dateformverify(str,char(formatstr(i)),'local');
0592                 if (strcmpi(str, strtrim(str1)) == 1)
0593                     format = char(formatstr(i));
0594                     break;
0595                 end
0596               catch
0597                 lasterr('');
0598               end       
0599           end
0600       end
0601   end
0602  end

Generated on Fri 13-Nov-2009 11:17:03 by m2html © 2003