I promise that this is really not intended to sound sarcastic (I’m a nice chap, really): since this obviously isn’t straightforward, would it be an interesting exercise for forum members to collaborate until we’d got the algorithm/pseudocode to do this in some sort of shape?
I suspect it may have to do with a kind of ‘Date of Easter’ conundrum whereby a cut off date after which the rules became unmanageable… the whether or not 2100 is a Leap Year, for example, frustrates linear logic.
I’m using a wonderful piece of financial management software nowadays which seems to have given up and made a decision to pick the year 4000 as its greater limit in date filters, rather than try to model infinity. It’s not elegant but it works perfectly.
Other than that - and this would be oh so useful - isn’t it really a mass of Case… statements respecting the Modulus of the way in which any one day in the week falls with respect to the start and end dates of the month in question?