; I saw the method that Borland wrote in TI 2575 and thought that
; someone might want to take a look at a different approach. I 
; wrote this about a year ago to calculate business days in a 
; trading period for an options trading application.
; Not having profiled the two, I don't know which is faster. 
; I was trying to keep as much of the manipulation as possible in 
; pointers to obtain good performance. The Borland way might be better.
; This is also in PROC form for use in a library and for calling speed.
; If you have any comments or ideas, drop me a line.
;
; Steve Blair - CIS ID 72537,2547
;
; This PROC requires two valid dates and a "HOLIDAYS" table with the following
; structure.
; 
; FIELD     Holidays    D   *
; 

proc bDays(start date, end Date) longint
var
    tcH             tCursor
    d,
    arSize          longInt
    dateVar         date
    ArDA            Array[] date
endvar

arSize = longInt(end - start) + 1    ;calculate maximum size of array

dateVar = start
ArDA.setSize(arSize)

for D from 1 to ArSize          ; fill Array DA with dates of trading period
    ArDA[D] = dateVar
    dateVar = dateVar + 1
endFor

; open holidays table and set filter to current period
if not tcH.open("HOLIDAYS.DB")
    then
        msgStop("ERROR!", "Unable to open HOLIDAYS.DB.
Contact technical support.")
    else
        tcH.setFilter(start, end)
endif
;Note, if you are now using PFW 5.0, change this to setrange, it's faster

; scan loop to eliminate holidays from Array DA
scan tcH :
    dateVar = tcH."holidays"
    if ArDA.contains(dateVar)
        then
            ArDA.removeItem(dateVar)
        endif
endscan
 
tcH.close()

;for loop to eliminate Saturdays and Sundays
dateVar = start         ;initialize loop variable
for D from 1 to ArSize
    if dowOrd(dateVar) = 7 or dowOrd(dateVar) = 1
        then
            ArDA.removeItem(dateVar)
    endif
    dateVar = dateVar + 1
endfor

return ArDA.size()

endproc
