]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/daysact.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / daysact.m
1 ## Copyright (C) 2007 David Bateman
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} daysact (@var{d1})
18 ## @deftypefnx {Function File} {} daysact (@var{d1}, @var{d2})
19 ## Calculates the number of days between two dates. If the second date is not
20 ## given, calculate the number of days since 1-Jan-0000. The variables @var{d1}
21 ## and @var{d2} can either be strings or an @var{n}-row string matrix. If both
22 ## @var{d1} and @var{d2} are string matrices, then the number of rows must 
23 ## match. An example of the use of @code{daysact} is
24 ##
25 ## @example
26 ## @group
27 ## daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"])
28 ## @result{}      9
29 ##        53
30 ##       203
31 ## @end group
32 ## @end example
33 ## @seealso{datenum}
34 ## @end deftypefn
35
36 function days = daysact (d1, d2)
37  if (nargin == 1)
38    nr = size (d1, 1);
39    if (nr != 1)
40      days = zeros (nr,1);
41      for i = 1 : nr
42        days (i) = datenum (d1 (i,:));
43      endfor
44    else
45      days = datenum(d1);
46    endif
47  elseif (nargin == 2)
48    nr1 = size (d1, 1);
49    nr2 = size (d2, 1);   
50    if (nr1 != nr2 && nr1 != 1 && nr2 != 1)
51      error ("daysact: size mismatch");
52    endif
53    if (nr1 == 1 && nr2 == 1)
54      days = datenum (d2) - datenum(d1);
55    elseif (nr1 == 1)
56      days = zeros (nr2, 1);
57      for i = 1 : nr2
58        days(i) = datenum (d2 (i,:)) - datenum (d1);
59      endfor
60    elseif (nr2 == 1)
61      days = zeros (nr1, 1);
62      for i = 1 : nr1
63        days(i) = datenum (d2) - datenum (d1 (i,:));
64      endfor
65    else
66      days = zeros (nr1, 1);
67      for i = 1 : nr1
68        days(i) = datenum (d2 (i, :)) - datenum (d1 (i,:));
69      endfor
70    endif
71  else
72    print_usage();
73   endif
74 endfunction
75
76 %!assert (daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"]),[9;53;203])