1 ## Copyright (C) 2007 David Bateman
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
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
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/>.
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
27 ## daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"])
36 function days = daysact (d1, d2)
42 days (i) = datenum (d1 (i,:));
50 if (nr1 != nr2 && nr1 != 1 && nr2 != 1)
51 error ("daysact: size mismatch");
53 if (nr1 == 1 && nr2 == 1)
54 days = datenum (d2) - datenum(d1);
56 days = zeros (nr2, 1);
58 days(i) = datenum (d2 (i,:)) - datenum (d1);
61 days = zeros (nr1, 1);
63 days(i) = datenum (d2) - datenum (d1 (i,:));
66 days = zeros (nr1, 1);
68 days(i) = datenum (d2 (i, :)) - datenum (d1 (i,:));
76 %!assert (daysact ("01-Jan-2007", ["10-Jan-2007"; "23-Feb-2007"; "23-Jul-2007"]),[9;53;203])