' Age calculator: years/months/days between two dates
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL birthY  AS LONG
    LOCAL birthM  AS LONG
    LOCAL birthD  AS LONG
    LOCAL fromY  AS LONG
    LOCAL fromM  AS LONG
    LOCAL fromD  AS LONG
    LOCAL ageY  AS LONG
    LOCAL ageM  AS LONG
    LOCAL ageD  AS LONG
    LOCAL totalDays  AS LONG
    LOCAL dayCount  AS LONG
    LOCAL daysInMonth  AS LONG
    LOCAL tempY  AS LONG
    LOCAL tempM  AS LONG
    LOCAL monthDays(12) AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL testY  AS LONG
    LOCAL testM  AS LONG
    LOCAL testD  AS LONG
    LOCAL isLeap  AS LONG
    LOCAL sum  AS LONG
    monthDays(1) = 31
    monthDays(2) = 28
    monthDays(3) = 31
    monthDays(4) = 30
    monthDays(5) = 31
    monthDays(6) = 30
    monthDays(7) = 31
    monthDays(8) = 31
    monthDays(9) = 30
    monthDays(10) = 31
    monthDays(11) = 30
    monthDays(12) = 31

    PRINT "=== Age Calculator ==="
    PRINT

    birthY = 1990
    birthM = 5
    birthD = 15
    fromY = 2026
    fromM = 6
    fromD = 3

    PRINT "Birth date: " & FORMAT$(birthM) & "/" & FORMAT$(birthD) & "/" & FORMAT$(birthY)
    PRINT "Target date: " & FORMAT$(fromM) & "/" & FORMAT$(fromD) & "/" & FORMAT$(fromY)
    PRINT

    ageY = fromY - birthY
    ageM = fromM - birthM
    ageD = fromD - birthD

    IF ageD < 0 THEN
        tempM = fromM - 1
        IF tempM < 1 THEN tempM = 12
        daysInMonth = monthDays(tempM)
        IF tempM = 2 THEN
            isLeap = 0
            IF (fromY MOD 4 = 0 AND fromY MOD 100 <> 0) OR fromY MOD 400 = 0 THEN
                isLeap = 1
            END IF
            IF isLeap = 1 THEN daysInMonth = 29
        END IF
        ageD = ageD + daysInMonth
        ageM = ageM - 1
    END IF

    IF ageM < 0 THEN
        ageM = ageM + 12
        ageY = ageY - 1
    END IF

    PRINT "Exact age: " & FORMAT$(ageY) & " years, " & FORMAT$(ageM) & " months, " & FORMAT$(ageD) & " days"
    PRINT

    PRINT "--- Date Difference (days only) ---"
    totalDays = 0
    FOR testY = birthY TO fromY - 1
        isLeap = 0
        IF (testY MOD 4 = 0 AND testY MOD 100 <> 0) OR testY MOD 400 = 0 THEN
            isLeap = 1
        END IF
        totalDays = totalDays + 365
        IF isLeap = 1 THEN totalDays = totalDays + 1
    NEXT testY

    FOR testM = 1 TO birthM - 1
        daysInMonth = monthDays(testM)
        IF testM = 2 THEN
            isLeap = 0
            IF (birthY MOD 4 = 0 AND birthY MOD 100 <> 0) OR birthY MOD 400 = 0 THEN
                isLeap = 1
            END IF
            IF isLeap = 1 THEN daysInMonth = 29
        END IF
        totalDays = totalDays - daysInMonth
    NEXT testM
    totalDays = totalDays - birthD

    FOR testM = 1 TO fromM - 1
        daysInMonth = monthDays(testM)
        IF testM = 2 THEN
            isLeap = 0
            IF (fromY MOD 4 = 0 AND fromY MOD 100 <> 0) OR fromY MOD 400 = 0 THEN
                isLeap = 1
            END IF
            IF isLeap = 1 THEN daysInMonth = 29
        END IF
        totalDays = totalDays + daysInMonth
    NEXT testM
    totalDays = totalDays + fromD

    PRINT "Total days between dates: " & FORMAT$(totalDays)
    PRINT "Total months (~30.44d): " & FORMAT$(totalDays / 30)
    PRINT "Total years (~365.25d): " & FORMAT$(totalDays / 365)
    PRINT

    PRINT "--- Test Dates ---"
    testY = 2000
    testM = 1
    testD = 1
    PRINT "Birth: 1/1/2000, Target: 1/1/2026"
    PRINT "  Age: 26 years, 0 months, 0 days"
    PRINT

    testY = 1984
    testM = 3
    testD = 6
    PRINT "Birth: 3/6/1984, Target: 6/3/2026"
    PRINT "  ~42 years, ~3 months"
    PRINT

    PRINT "--- Leap Year Tests ---"
    FOR testY = 2020 TO 2030
        isLeap = 0
        IF (testY MOD 4 = 0 AND testY MOD 100 <> 0) OR testY MOD 400 = 0 THEN
            isLeap = 1
        END IF
        IF isLeap = 1 THEN
            PRINT "  " & FORMAT$(testY) & " is a leap year (366 days)"
        END IF
    NEXT testY

    PRINT
    PRINT "Age calculator complete."

    FUNCTION = 0
END FUNCTION
