Why not pick off the last item of dateItems for this routine instead of doing all the work that you do?
I think that code would be something like
function libDate_dayOfWeek pYear,pMonth,pDay
put pYear,pMonth,pDay,0,0,0,0 into theDate
convert theDate to dateItems
return last item of theDate -1 #dateItems goes 1..7 for Sun..Sat
end libDate_dayOfWeek
Because the whole idea of libDate is to avoid convert, due to century cutoff and epoch timestamp problems you may or may nnot be facing when needing to work with dates either before 19670 or after 2030. :-)
Why not pick off the last item of dateItems for this routine instead of doing all the work that you do? I think that code would be something like