Post Reply 
Casio FC-200 Financial Consultant
12-27-2020, 04:22 PM
Post: #16
RE: Casio FC-200 Financial Consultant
Here is an implementation without sign(), and very fast.
Code:
function networkdays(A, B)  -- assumed date A <= B
    local D = B-A
    local E = floor(D/7)    -- weeks
    A = A%7                 -- weekday(A), range 0 to 6
    B = D-7*E + A           -- weekday(B), range 0 to 12
    if A>0  then A=1 end    -- default, for B<6
    if B==6 then A=A-1 end  -- Sat -> Fri
    if B>6  then A=-1 end   -- remove Sun
    return D-E-E + A
end

Sunday to Saturday is the tricky one, where my previous attempts failed.
FYI, Dave's posted code might failed this too. Please confirm.

lua> networkdays(day(2021,2,28), day(2021,3,5)) -- Sun to Fri
5
lua> networkdays(day(2021,2,28), day(2021,3,6)) -- Sun to Sat
5
lua> networkdays(day(2021,2,28), day(2021,3,7)) -- Sun to Sun
5
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Casio FC-200 Financial Consultant - Gamo - 12-15-2020, 12:07 PM
RE: Casio FC-200 Financial Consultant - Albert Chan - 12-27-2020 04:22 PM



User(s) browsing this thread: 1 Guest(s)