X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Ffinancial-0.4.0%2Fdaysact.m;fp=octave_packages%2Ffinancial-0.4.0%2Fdaysact.m;h=69e6d5272ae025c659d05697e1fb2926abe5ae73;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/financial-0.4.0/daysact.m b/octave_packages/financial-0.4.0/daysact.m new file mode 100644 index 0000000..69e6d52 --- /dev/null +++ b/octave_packages/financial-0.4.0/daysact.m @@ -0,0 +1,76 @@ +## Copyright (C) 2007 David Bateman +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {} daysact (@var{d1}) +## @deftypefnx {Function File} {} daysact (@var{d1}, @var{d2}) +## Calculates the number of days between two dates. If the second date is not +## given, calculate the number of days since 1-Jan-0000. The variables @var{d1} +## and @var{d2} can either be strings or an @var{n}-row string matrix. If both +## @var{d1} and @var{d2} are string matrices, then the number of rows must +## match. An example of the use of @code{daysact} is +## +## @example +## @group +## daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"]) +## @result{} 9 +## 53 +## 203 +## @end group +## @end example +## @seealso{datenum} +## @end deftypefn + +function days = daysact (d1, d2) + if (nargin == 1) + nr = size (d1, 1); + if (nr != 1) + days = zeros (nr,1); + for i = 1 : nr + days (i) = datenum (d1 (i,:)); + endfor + else + days = datenum(d1); + endif + elseif (nargin == 2) + nr1 = size (d1, 1); + nr2 = size (d2, 1); + if (nr1 != nr2 && nr1 != 1 && nr2 != 1) + error ("daysact: size mismatch"); + endif + if (nr1 == 1 && nr2 == 1) + days = datenum (d2) - datenum(d1); + elseif (nr1 == 1) + days = zeros (nr2, 1); + for i = 1 : nr2 + days(i) = datenum (d2 (i,:)) - datenum (d1); + endfor + elseif (nr2 == 1) + days = zeros (nr1, 1); + for i = 1 : nr1 + days(i) = datenum (d2) - datenum (d1 (i,:)); + endfor + else + days = zeros (nr1, 1); + for i = 1 : nr1 + days(i) = datenum (d2 (i, :)) - datenum (d1 (i,:)); + endfor + endif + else + print_usage(); + endif +endfunction + +%!assert (daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"]),[9;53;203])