Nbr of working days
Posted in 1995
Folks, What I find really annoying is deleting mail which had a name and address which I didn't bother to save anywhere. Some worthy was just worrying about number of working days between two dates in SPL. He had already done the work of stashing the Holidays into a file and doing end_date - start_date - COUNT(*) from holidays between start_date and end_date He had yet to worry about subtracting out Sundays and Saturdays, since this is something I've wanted and should properly belong in THE date library which I volunteered to write 8 years ago, I went ahead and wrote it. I did it in 4gl, so there will be some minor conversions to make it run in SPL, but it should go quietly. As always I could have taken some short cuts, but chose instead to keep the code clear. For your pleasure: --- cut here --- ######################################################## # just to test it out ######################################################## MAIN DEFINE s_date, e_date DATETIME YEAR TO DAY, w_days INTERVAL DAY(3) TO DAY PROMPT "Enter start date" for s_date # e.g. 1995-09-02 PROMPT "Enter end date" for e_date CALL work_days(s_date, e_date) RETURNING w_days DISPLAY "Work days: ", w_days END MAIN ##################################################################### # Non-inclusive number of work days between two dates # work_days(monday, tuesday) would return 1 # Jack Parker@HP, 8-31-95. ##################################################################### FUNCTION work_days(s_date, e_date) DEFINE s_date, e_date DATETIME YEAR TO DAY, offset INTEGER, offitv, wk_days, adjust INTERVAL DAY(3) TO DAY # # Problem: when start date is a sunday or saturday things get hokey, since # these AREN'T work days, lets just avoid the problem by skipping them # # Fix start date LET offset = WEEKDAY(s_date) # What IS today? IF offset = 0 THEN LET s_date = s_date + 1 UNITS DAY END IF IF offset = 6 THEN LET s_date = s_date + 2 UNITS DAY END IF # Fix end date LET offset = WEEKDAY(e_date) # What IS today? IF offset = 0 THEN LET e_date = e_date + 1 UNITS DAY END IF IF offset = 6 THEN LET e_date = e_date + 2 UNITS DAY END IF LET offset = WEEKDAY(s_date) - 1 # step back to monday LET offitv = offset USING "-##&" # convert to interval LET s_date = s_date - offitv # we are now on monday LET wk_days = e_date - s_date # number of intervening days LET adjust = (wk_days/7)*2 # number of weeks (weekends) LET wk_days = wk_days - adjust - offitv RETURN wk_days END FUNCTION --- and here --- cheers j. _____________________________________________________________________________ Jack Parker - Hewlett Packard, BSMC Boise, Idaho, USA jparker@hpbs3645.boi.hp.com _____________________________________________________________________________ Back up my hard drive? You mean these things can actually run in reverse? _____________________________________________________________________________ Any opinions expressed herein are my own and not those of my employers. _____________________________________________________________________________