function varargout = datevec(t)
%DATEVEC Convert datetimes to date vectors.
%   
%   Using DATEVEC to represent points in time is not recommended. 
%   Use datetime instead. To extract date and time components from a datetime,
%   use the ymd, hms, or datevec functions or the datetime properties:
%   Year, Month, Day, Hour, Minute, or Second.
%   
%   See also DATETIME.
%   


%   Copyright 2016-2023 The MathWorks, Inc.

% Convert chars or cellstr to string to ensure elementwise
if ischar(t) || iscellstr(t) %#ok<ISCLSTR>
    t = string(t);
end

% T must be a double, string, or a datetime-related type
clz = underlyingType(t);
if ~ismember(clz, ["double", "string", "datetime", "duration", "calendarDuration"])
    error(message('parallel:array:UnsupportedFunction', upper(mfilename), clz));
end
if ~iscolumn(t) 
    % For non-column input reshape to a column or matrix depending on the
    % outputs requested.
    if nargout<2
        t = colonize(t);
    else
        t = reshape(t,size(t,1),[]);
    end
end

% Always capture six separate columns so that it is an elementwise
% calculation. If the input was empty these should form a 0x6 array.
if isempty(t) || iIsEmptyString(t)
    % For empties the behaviour is a little odd. If wanting a single output
    % it should be 0x6. If multiple outputs each should match the input.
    if nargout<2
        varargout = {codistributed.zeros(0,6)};
    else
        % Size of each output should match input or be 0x0 for empty
        % strings (either string.empty or "").
        emptySz = size(t);
        if iIsEmptyString(t)
            emptySz = [0 0];
        end
        [varargout{1:nargout}] = deal(codistributed.zeros(emptySz));
    end
    return;
end

% Run the calculation independently on each element
out = cell(1,6);
[out{:}] = codistributed.pElementwiseUnaryOp(@datevec, t);

% Now package the real output
if nargout < 2
    % Single out = matrix
    varargout = {cat(2, out{:})};
else
    % Multi-out, so copy corresponding elements
    varargout(1:nargout) = out(1:nargout);
end

end

function tf = iIsEmptyString(t)
tf = isstring(t) && isscalar(t) && strlength(t(1))==0;
end
