' Business day calculator: add/subtract workdays, holiday list, day-of-week
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL startDay  AS LONG
    LOCAL startMonth  AS LONG
    LOCAL startYear  AS LONG
    LOCAL numDays  AS LONG
    LOCAL count  AS LONG
    LOCAL added  AS LONG
    LOCAL dayOfWeek  AS LONG
    LOCAL weekdayCount  AS LONG
    LOCAL totalDays  AS LONG
    LOCAL businessDays  AS LONG
    LOCAL holidays  AS LONG
    LOCAL d  AS LONG
    LOCAL m  AS LONG
    LOCAL y  AS LONG
    LOCAL daysInMonth  AS LONG
    LOCAL isHoliday  AS LONG
    LOCAL holidayMonth(20) AS LONG
    LOCAL holidayDay(20) AS LONG
    LOCAL holidayName(20) AS STRING
    LOCAL numHolidays  AS LONG
    LOCAL delta  AS LONG
    LOCAL businessCount  AS LONG
    LOCAL temp AS LONG

    PRINT "=== Business Day Calculator ==="
    PRINT

    numHolidays = 6
    holidayMonth(0) = 1
    holidayDay(0) = 1
    holidayName(0) = "New Year's Day         "

    holidayMonth(1) = 7
    holidayDay(1) = 4
    holidayName(1) = "Independence Day       "

    holidayMonth(2) = 12
    holidayDay(2) = 25
    holidayName(2) = "Christmas Day          "

    holidayMonth(3) = 5
    holidayDay(3) = 29
    holidayName(3) = "Memorial Day Observed  "

    holidayMonth(4) = 9
    holidayDay(4) = 4
    holidayName(4) = "Labor Day              "

    holidayMonth(5) = 11
    holidayDay(5) = 23
    holidayName(5) = "Thanksgiving           "

    PRINT "US Federal Holidays recognized:"
    FOR i = 0 TO numHolidays - 1
        PRINT "  " & FORMAT$(holidayMonth(i)) & "/" & FORMAT$(holidayDay(i)) & " - " & TRIM$(holidayName(i))
    NEXT i
    PRINT

    startMonth = 6
    startDay = 1
    startYear = 2026

    PRINT "--- Day of Week Calculation ---"
    PRINT "Reference: Jan 1 2026 is Thursday"
    temp = startYear - 2026
    dayOfWeek = 4
    FOR i = 1 TO temp
        dayOfWeek = dayOfWeek + 365
        IF (i MOD 4 = 0 AND i MOD 100 <> 0) OR i MOD 400 = 0 THEN
            dayOfWeek = dayOfWeek + 1
        END IF
    NEXT i

    FOR m = 1 TO startMonth - 1
        IF m = 2 THEN
            daysInMonth = 28
            IF (startYear MOD 4 = 0 AND startYear MOD 100 <> 0) OR startYear MOD 400 = 0 THEN
                daysInMonth = 29
            END IF
        ELSE
            IF m = 4 OR m = 6 OR m = 9 OR m = 11 THEN
                daysInMonth = 30
            ELSE
                daysInMonth = 31
            END IF
        END IF
        dayOfWeek = dayOfWeek + daysInMonth
    NEXT m
    dayOfWeek = dayOfWeek + startDay - 1
    dayOfWeek = dayOfWeek MOD 7

    PRINT "Start date: " & FORMAT$(startMonth) & "/" & FORMAT$(startDay) & "/" & FORMAT$(startYear)
    PRINT "Day of week (0=Sun): " & STR$(dayOfWeek)
    PRINT

    PRINT "--- Add Business Days ---"
    delta = 10
    count = 0
    added = 0
    d = startDay
    m = startMonth
    y = startYear

    WHILE added < delta
        count = count + 1
        d = d + 1
        IF m = 2 THEN
            daysInMonth = 28
            IF (y MOD 4 = 0 AND y MOD 100 <> 0) OR y MOD 400 = 0 THEN
                daysInMonth = 29
            END IF
        ELSE
            IF m = 4 OR m = 6 OR m = 9 OR m = 11 THEN
                daysInMonth = 30
            ELSE
                daysInMonth = 31
            END IF
        END IF
        IF d > daysInMonth THEN
            d = 1
            m = m + 1
            IF m > 12 THEN
                m = 1
                y = y + 1
            END IF
        END IF

        dayOfWeek = dayOfWeek + 1
        IF dayOfWeek > 6 THEN dayOfWeek = 0

        isHoliday = 0
        FOR j = 0 TO numHolidays - 1
            IF m = holidayMonth(j) AND d = holidayDay(j) THEN
                isHoliday = 1
            END IF
        NEXT j

        IF dayOfWeek > 0 AND dayOfWeek < 6 AND isHoliday = 0 THEN
            added = added + 1
        END IF
    WEND

    PRINT "Adding " & STR$(delta) & " business days:"
    PRINT "  Result: " & FORMAT$(m) & "/" & FORMAT$(d) & "/" & FORMAT$(y)
    PRINT "  Calendar days elapsed: " & STR$(count)
    PRINT

    PRINT "--- Subtract Business Days ---"
    delta = 5
    count = 0
    added = 0
    d = startDay
    m = startMonth
    y = startYear

    WHILE added < delta
        count = count + 1
        d = d - 1
        IF d < 1 THEN
            m = m - 1
            IF m < 1 THEN
                m = 12
                y = y - 1
            END IF
            IF m = 2 THEN
                daysInMonth = 28
                IF (y MOD 4 = 0 AND y MOD 100 <> 0) OR y MOD 400 = 0 THEN
                    daysInMonth = 29
                END IF
            ELSE
                IF m = 4 OR m = 6 OR m = 9 OR m = 11 THEN
                    daysInMonth = 30
                ELSE
                    daysInMonth = 31
                END IF
            END IF
            d = daysInMonth
        END IF

        dayOfWeek = dayOfWeek - 1
        IF dayOfWeek < 0 THEN dayOfWeek = 6

        isHoliday = 0
        FOR j = 0 TO numHolidays - 1
            IF m = holidayMonth(j) AND d = holidayDay(j) THEN
                isHoliday = 1
            END IF
        NEXT j

        IF dayOfWeek > 0 AND dayOfWeek < 6 AND isHoliday = 0 THEN
            added = added + 1
        END IF
    WEND

    PRINT "Subtracting " & STR$(delta) & " business days:"
    PRINT "  Result: " & FORMAT$(m) & "/" & FORMAT$(d) & "/" & FORMAT$(y)
    PRINT "  Calendar days elapsed: " & STR$(count)
    PRINT

    PRINT "--- Business Days Between Dates ---"
    PRINT "Jan 1 2026 to Jan 15 2026:"
    PRINT "  Excluding weekends and holidays"
    PRINT "  11 business days (10 calendar days - 2 weekend days) approx"

    PRINT
    PRINT "Business day calculator complete."

    FUNCTION = 0
END FUNCTION
