1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
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} {b =} busdate (refdate)
18 ## @deftypefnx {Function File} {b =} busdate (refdate, direction)
19 ## @deftypefnx {Function File} {b =} busdate (refdate, direction, holiday)
20 ## @deftypefnx {Function File} {b =} busdate (refdate, direction, holiday, weekend)
22 ## Return the datenum of the next or previous business day from
23 ## @var{refdate}. @var{direction} indicates the next day (default) if 1
24 ## and the previous day if -1. @var{holiday} is a vector of datenums
25 ## that defines the holidays observed (the holidays function is used if
26 ## not given). @var{weekend} defines the days of the week that should
27 ## be considered weekends; [1 0 0 0 0 0 1] (default) indicates that
28 ## Sunday and Saturday are holidays.
30 ## If any of the optional inputs (@var{direction}, @var{holiday},
31 ## @var{weekend}) are empty, then the default is used.
33 ## @seealso{holidays, lbusdate, isbusday, fbusdate}
36 function rd = busdate (rd, d, hol, wkend)
41 if nargin < 2 || isempty (d)
43 elseif ~ all (abs (d) == 1)
44 ## People could use other numbers to skip days, but that is not
46 error ("directions must all be either 1 or -1.")
58 mask = ~isbusday (rd, hol, wkend);
60 ## Only recompute for the days that are not yet business days
66 mask(mask) = ~isbusday (rd(mask), hol, wkend);
73 %!assert(busdate(datenum(2008,1,2)), datenum(2008,1,3))
75 %!assert(busdate(datenum(2007,12,31)), datenum(2008,1,2))
76 ## Go over a weekend and start in a weekend
77 %!assert(busdate(datenum(2007,1,5)), datenum(2007,1,8))
78 %!assert(busdate(datenum(2007,1,6)), datenum(2007,1,8))
80 %!assert(busdate(datenum(2008,1,3), -1), datenum(2008,1,2))
82 %!assert(busdate(datenum(2008,1,2), -1), datenum(2007,12,31))
83 ## Backward with alternate holidays
84 %!assert(busdate(datenum(2008,1,2), -1, datenum(2007,1,1):datenum(2008,1,1)), datenum(2006,12,29))
85 ## Multiple dates in both orientations
86 %!assert(busdate([datenum(2008,1,2) datenum(2007,1,1)]), [datenum(2008,1,3) datenum(2007,1,2)])
87 %!assert(busdate([datenum(2008,1,2) datenum(2007,1,1)], [1 1]), [datenum(2008,1,3) datenum(2007,1,2)])
88 %!assert(busdate([datenum(2008,1,2) datenum(2007,1,1)], 1), [datenum(2008,1,3) datenum(2007,1,2)])
89 %!assert(busdate([datenum(2008,1,2);datenum(2007,1,1)], [1;1]), [datenum(2008,1,3);datenum(2007,1,2)])
90 ## Multiple dates with opposite directions holidays and weekends
91 %!assert(busdate([datenum(2008,1,2);datenum(2007,1,2)], [1;-1]), [datenum(2008,1,3);datenum(2006,12,29)])
93 %!assert(busdate(datenum(2008,1,2), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,3))
94 %!assert(busdate(datenum(2008,1,4), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,5))
95 %!assert(busdate(datenum(2008,1,5), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,7))
96 %!assert(busdate(datenum(2008,1,6), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,7))
97 %!assert(busdate(datenum(2008,1,1), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 1 1 1 1 1 0]), datenum(2008,1,5))