function varargout = datevecfix(t,varargin)
%DATEVECFIX Date components with rounded seconds to specified precision.
%
% SYNTAX:
% V = DATEVEC(N);
% V = DATEVEC(S);
% V = DATEVEC(S,F);
% V = DATEVEC(S,P);
% V = DATEVEC(S,P,F);
% V = DATEVEC(S,F,P);
% V = DATEVEC(...,'Precision',K);
% [Y,MO,D,H,MI,S] = DATEVEC(...);
%
% INPUT:
% N - Serial date (see DATENUM).
% S - String date (see DATESTR).
% F - Format date (see DATESTR).
% P - Pivot date (see DATESTR).
% K - Seconds precision. Must be a positive integer or zero.
% DEFAULT: 0 (rounds up to seconds)
%
% OUTPUT:
% V - 6 colums matrix with [Y MO D H MI S] values.
% Y - Years.
% MO - Months.
% D - Days.
% H - Hours.
% MI - Minutes.
% S - Seconds.
%
%
% DESCRIPTION:
% This program is the same as DATEVEC but rounds the seconds up to the
% specified precision and fixes the problem with the 60 seconds not
% rounded value.
%
% NOTE:
% * Optional inputs use its DEFAULT value when not given or [].
% * Option argument name 'Precision' may be a single 'P'.
%
% EXAMPLE:
% datevec( 366.9999999999999), % returns: [0 12 31 23 59 60]
% 60-ans(6), % returns: 1.1176e-008
% datevecfix(366.9999999999999), % returns: [1 1 1 0 0 0]
% 0-ans(6), % returns: 0
%
% SEE ALSO:
% DATEVEC
% and
% TLABEL by Carlos Vargas
% at http://www.mathworks.com/matlabcentral/fileexchange
%
%
% ---
% MFILE: datevecfix.m
% VERSION: 2.0 (Jun 08, 2009) (<a href="matlab:web('http://www.mathworks.com/matlabcentral/fileexchange/authors/11258')">download</a>)
% MATLAB: 7.7.0.471 (R2008b)
% AUTHOR: Carlos Adrian Vargas Aguilera (MEXICO)
% CONTACT: nubeobscura@hotmail.com
% REVISIONS:
% 1.0 Released. (Mar 04, 2008)
% 2.0 Added precision optional input. (Jun 08, 2009)
% DISCLAIMER:
% datevecfix.m is provided "as is" without warranty of any kind, under
% the revised BSD license.
% Copyright (c) 2008-2009 Carlos Adrian Vargas Aguilera
% INPUTS CHECK-IN
% -------------------------------------------------------------------------
% Defaults:
K = 0;
% Checks number of inputs:
if nargin<1
error('CVARGAS:datevecfix:notEnoughInputs',...
'At least 1 input is required.')
elseif nargout>7
error('CVARGAS:datevecfix:tooManyOutputs',...
'At most 6 outputs are allowed.')
end
% Checks precision:
if ((nargin>2) && (~isempty(varargin{end-1}) && ...
strncmpi(varargin{end-1},'precision',min(length(varargin{end-1}),9))))
K = varargin{end};
if (~isempty(K) && isnumeric(K) && (numel(K)==1))
K = round(abs(K));
else
error('CVARGAS:datevecfix:incorrectPrecisionInput',...
'Precision must be a single integer.')
end
varargin(end-1:end) = [];
end
% -------------------------------------------------------------------------
% MAIN
% -------------------------------------------------------------------------
% Find vector:
x = datevec(t,varargin{:});
% Rounds seconds up to precision:
x(:,6) = round(x(:,6)*10^K);
% Check seconds:
ibad = (x(:,6)>=60*10^K);
x(ibad,6) = x(ibad,6)-60*10^K;
x(:,6) = x(:,6)/10^K;
x(ibad,5) = x(ibad,5)+1;
% Check minutes:
ibad = (x(:,5)>=60);
x(ibad,5) = x(ibad,5)-60;
x(ibad,4) = x(ibad,4)+1;
% Check hours:
ibad = (x(:,4)>=24);
x(ibad,4) = x(ibad,4)-24;
x(ibad,3) = x(ibad,3)+1;
% Check days, months and years:
jbad = x(ibad,3)>=28; % only problematic days
if any(jbad)
y = datevec(datenum(x(ibad(jbad),1:3)));
x(ibad(jbad),1:3) = y(:,1:3);
end
% OUTPUTS CHECK-OUT
% -------------------------------------------------------------------------
if nargout>1
for k = 1:nargout
varargout{k} = x(:,k);
end
else
varargout{1} = x;
end
% [EOF] datevecfix.m