function c = dvcore(t)
%DVCORE Core routine for DATEVEC.
%   D = DVCORE(T) converts the time in seconds into a date vector. 
%
%   Called by DATEVEC and DATESTR.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 1998/04/07 18:36:00 $

t = t(:); % Make t a column vector
len = length(t);

if any(~isfinite(t)), error('Date numbers must be finite.'); end

% If necessary, make t positive.
% Shift by an integer multiple of 400 years, which is p seconds.
p = (400*365+97)*86400;
shift = max(0,ceil(-t ./ p));
t = t + shift*p;

% Convert seconds into a date vector components.  t is in seconds.
c(:,6) = rem(t,60);
t = floor(t);                  % Truncate to the nearest second
t = floor(t ./ 60);            % minutes.
c(:,5) = rem(t,60);
t = floor(t ./ 60);            % hours.
c(:,4) = rem(t,24);
t = floor(t ./ 24);            % days.
a = 365+97/400;
y = floor(t ./ a);             % year

i = find(t <= 365*y + ceil(0.25*y)-ceil(0.01*y)+ceil(0.0025*y));
if ~isempty(i), y(i) = y(i) - 1; end
c(:,1) = y - shift*400;

dpm = [31 28 31 30 31 30 31 31 30 31 30 31];
dpm = dpm(ones(len,1),:);
t = t - (365*y + ceil(0.25*y)-ceil(0.01*y)+ceil(0.0025*y));
i = find((rem(y,4) == 0 & rem(y,100) ~= 0) |  rem(y,400) == 0);
dpm(i,2) = 29+zeros(size(i));

cdm = cumsum(dpm')';                     % dpm rows may now differ
cdm = [zeros(len,1) cdm(:,1:11)];
c(:,2) = sum((t(:,ones(1,12)) - cdm > 0)')';
c(:,3) = t - cdm(len*(c(:,2)-1)+(1:len)');


