function t2 = dateshift(t, varargin)
%DATESHIFT Shift codistributed datetimes or generate sequences according to a calendar rule.
%   
%   T2 = DATESHIFT(T,'start',UNIT)
%   T2 = DATESHIFT(T,'end',UNIT)
%   T2 = DATESHIFT(T,'dayofweek',DOW)
%   T2 = DATESHIFT(T,'dayofweek','weekday')
%   T2 = DATESHIFT(T,'dayofweek','weekend')
%   T2 = DATESHIFT(T,...,RULE)
%   
%   See also DATETIME/DATESHIFT, CODISTRIBUTED.
%   


%   Copyright 2016-2021 The MathWorks, Inc.

% Two possible cases:
% 1. Only T is distributed, in which case gather everything else and treat
%    as element-wise in T.
% 2. A numeric rule is specified and is distributed. In this case the
%    result is binary element-wise over T and RULE.

if nargin>2 && iscodistributed(varargin{end}) && isnumeric(varargin{end})
    % Assume numeric rule and apply element-wise, gathering and binding in
    % the middle arguments.
    rule = varargin{end};
    varargin(end) = [];
    varargin = distributedutil.CodistParser.gatherElements(varargin);
    t2 = codistributed.pElementwiseOp(@(a,b) dateshift(a, varargin{:}, b), t, rule);
else
    % Not a numeric rule, so should be element-wise in the first argument
    varargin = distributedutil.CodistParser.gatherElements(varargin);
    t2 = codistributed.pElementwiseOp(@(a) dateshift(a, varargin{:}), t);
end


