shared-snippets/matlab/unitutils/second.m

71 lines
1.7 KiB
Matlab

%
% -----------------
% Copyright © 2022 ACK Cyfronet AGH, Poland.
% -----------------
%
function s = second(d,f)
%SECOND Seconds of date or time.
% S = SECOND(D) returns the seconds given a serial date number or a
% date string, D.
%
% S = SECOND(S,F) returns the second of one or more date strings S 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.
%
% 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.
%
% For example, s = second(728647.558427893) or
% s = second('19-Dec-1994, 13:24:08.17') returns s = 8.17.
%
% See also DATEVEC, MINUTE, HOUR.
% Copyright 1995-2008 The MathWorks, Inc.
% $Revision: 1.6.2.6 $ $Date: 2008/12/21 01:51:09 $
if nargin < 1
error('finance:second:missingInputs', 'Please enter D.')
end
if nargin < 2
f = '';
end
if ischar(d)
d = datenum(d,f);
sizeD = size(d);
elseif iscell(d)
sizeD = size(d);
d = datenum(d(:),f);
elseif isnumeric(d)
sizeD = size(d);
else
error('finance:second:invalidInputClass', ...
'Invalid date/time class.')
end
% Generate date vectors from dates.
c = datevec(d(:));
% Extract seconds. Multiply by 1000 then round the result to make sure we avoid
% roundoff errors for milliseconds.
%extract seconds from hours
sh = c(:,4)*3600;
% extract second from minutes
sm = c(:,5)*60;
% extract seconds
ss = c(:, 6);
% round
ss = sh + sm + ss;
s = round(1000.*ss)./1000;
% Reshape into the correct dims
s = reshape(s, sizeD);
% [EOF]