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} {@var{bdates} =} busdays (@var{sdate}, @var{edate})
18 ## @deftypefnx {Function File} {@var{bdates} =} busdays (@var{sdate}, @var{edate}, @var{bdmode})
19 ## @deftypefnx {Function File} {@var{bdates} =} busdays (@var{sdate}, @var{edate}, @var{bdmode}, @var{holvec})
20 ## Generate a list of business dates at the end of the periods defined
21 ## between (including) @var{sdate} and @var{edate}.
23 ## @var{sdate} is the starting date, @var{edate} is the ending date,
24 ## both are in serial date format (see datenum). @var{bdmode} is the
25 ## business day frequency ("daily", "weekly", "monthly", "quarterly",
26 ## "semiannual", or "annual"); these can be abbreviated by the first
27 ## letter and they may also use an integer corresponding to the order in
28 ## the above list (i.e. "daily" = 1). @var{holvec} is an optional list
29 ## of holidays. If the holidays are not given, then the holidays
31 ## @seealso{holidays, busdate, lbusdate, isbusday, fbusdate, datenum}
34 function bd = busdays (sd, ed, mode=1, hol=[])
36 if nargin < 2 || nargin > 4
46 error ("busdays: the start date must be less than the end date")
49 ## make the holidays take into account the whole ending year because
50 ## the day may extend beyond the actual ending date
53 hol = holidays (sd, datenum (edtmp));
55 ## Convert the mode to the numeric
58 mode = find (lower (mode(1)) == modestr);
60 error ("busdays: mode must be one of '%s'", modestr)
62 elseif isnumeric (mode)
63 if mode < 1 || mode > length (modestr)
64 error ("busdays: mode must be between 1 and %d", length (modestr))
67 error ("busdays: mode must be a number or string")
73 bd = (sd:ed)'(isbusday (sd:ed, hol));
76 ## weekly make the start and end dates Fridays and then move back
78 wd = weekday ([sd;ed]);
80 ## there are generally not more than one week of holidays at a
81 ## time, but the call to unique will make certain of that.
82 bd = unique (busdate ([d(1):7:d(2)]', -1, hol));
84 d = datevec ([sd:ed]);
85 ## unique year and month list within the date range
86 ym = unique (d(:,1:2), "rows");
88 ## monthly, do nothing to the ym list
91 if mod (ym(end), 3) != 0
92 ## make the last month an end of quarter month
93 ym(end) = ym(end) + 3 - mod (ym(end), 3);
95 ym(mod (ym(:,2), 3) != 0, :) = [];
98 if mod (ym(end), 6) != 0
99 ## make the last month an end of semi-annual month (6, 12)
100 ym(end) = ym(end) + 6 - mod (ym(end), 6);
102 ym(mod (ym(:,2), 6) != 0, :) = [];
104 bd = lbusdate (ym(:,1), ym(:,2), hol);
108 d = datevec ([sd;ed]);
109 bd = lbusdate ((d(1,1):d(2,1))', 12, hol);
111 ## this should have been caught before now
112 error ("busdays: invalid mode")
118 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 12)), datenum (2008, 1, [2;3;4;7;8;9;10;11]))
119 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 12), "d"), datenum (2008, 1, [2;3;4;7;8;9;10;11]))
120 %!assert (busdays (datenum (2001, 1, 2), datenum (2001, 1, 9), "w"), datenum (2001, 1, [5;12]))
121 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 2), "m"), datenum (2008, 1, 31))
122 %!assert (busdays (datenum (2008, 1, 1), datenum (2010, 5, 2), "m"), lbusdate ([2008*ones(12,1);2009*ones(12,1);2010*ones(5,1)], [1:12 1:12 1:5]'))
123 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 2), "q"), datenum (2008, 3, 31))
124 %!assert (busdays (datenum (2008, 1, 1), datenum (2010, 5, 2), "q"), lbusdate ([2008*ones(4,1);2009*ones(4,1);2010*ones(2,1)], [3:3:12 3:3:12 3 6]'))
125 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 2), "s"), datenum (2008, 6, 30))
126 %!assert (busdays (datenum (2008, 1, 1), datenum (2010, 5, 2), "s"), lbusdate ([2008;2008;2009;2009;2010], [6 12 6 12 6]'))
127 %!assert (busdays (datenum (2008, 1, 1), datenum (2011, 1, 2), "a"), datenum ([2008;2009;2010;2011], [12;12;12;12], [31;31;30;30]))