]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/isbusday.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / isbusday.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
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} {r =} isbusday (refdate)
18 ## @deftypefnx {Function File} {r =} isbusday (refdate, holiday)
19 ## @deftypefnx {Function File} {r =} isbusday (refdate, holiday, weekend)
20 ##
21 ## Return true if the @var{refdate} is a business date @var{refdate}.
22 ## @var{holiday} is a vector of datenums that defines the holidays
23 ## observed (the holidays function is used if not given). @var{weekend}
24 ## defines the days of the week that should be considered weekends;
25 ## [1 0 0 0 0 0 1] (default) indicates that Sunday and Saturday are
26 ## weekends.
27 ##
28 ## @seealso{holidays, lbusdate, busdate, fbusdate}
29 ## @end deftypefn
30
31 function mask = isbusday (rd, hol=[], wkend=[])
32
33   if ~ isnumeric (rd)
34         rd = datenum (rd);
35   endif
36   if isempty (hol)
37     ## Get all possible holidays that could affect the output.
38     hol = holidays (min(rd), max(rd));
39   end
40   if isempty (wkend)
41     wkend = [1 0 0 0 0 0 1];
42   elseif numel (wkend) ~= 7
43     error ("wkend must have 7 elements")
44   elseif nargin > 3
45     print_usage ();
46   endif
47
48   mask = reshape (wkend (weekday (rd)), size (rd));
49   if ~ isempty (hol)
50     ## Is it a holiday?
51     mask = mask | ismember(rd, hol);
52   endif
53   mask = ~mask;
54 endfunction
55
56 ## Tests
57 ## A normal day
58 %!assert(isbusday(datenum(2008,1,2)), true())
59 ## A holiday
60 %!assert(isbusday(datenum(2008,1,1)), false())
61 %!assert(isbusday(datenum(2008,1,1), []), false())
62 ## A weekend
63 %!assert(isbusday(datenum(2008,2,2)), false())
64 ## An alternate holiday
65 %!assert(isbusday(datenum(2008,1,2), datenum(2008,1,2)), false())
66 ## An alternate weekend
67 %!assert(isbusday(datenum(2008,1,2), [], zeros(1,7)), true())
68 %!assert(isbusday(datenum(2008,1,2), [], ones(1,7)), false())
69 ## A vector
70 %!assert(isbusday([datenum(2008,1,2) datenum(2008,2,2)]), [true() false()])
71 ## A vector in the other direction
72 %!assert(isbusday([datenum(2008,1,2);datenum(2008,2,2)]), [true();false()])