' Time Duration — represents and manipulates time durations
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL totalSeconds AS LONG
    LOCAL hours AS LONG
    LOCAL minutes AS LONG
    LOCAL seconds AS LONG
    LOCAL remaining AS LONG
    LOCAL h1 AS LONG
    LOCAL m1 AS LONG
    LOCAL s1 AS LONG
    LOCAL h2 AS LONG
    LOCAL m2 AS LONG
    LOCAL s2 AS LONG
    LOCAL total1 AS LONG
    LOCAL total2 AS LONG
    LOCAL sumTotal AS LONG
    LOCAL diffTotal AS LONG
    LOCAL absDiff AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL testCount AS LONG
    LOCAL passCount AS LONG
    LOCAL failCount AS LONG
    LOCAL formatStr AS STRING
    LOCAL lineStr AS STRING
    LOCAL padStr AS STRING
    testCount = 0
    passCount = 0
    failCount = 0
    PRINT "============================================"
    PRINT "          TIME DURATION UTILITY"
    PRINT "============================================"
    PRINT ""
    PRINT "Converting seconds to HH:MM:SS format"
    PRINT ""
    FOR i = 0 TO 100
        totalSeconds = i * 37
        hours = totalSeconds / 3600
        remaining = totalSeconds - hours * 3600
        minutes = remaining / 60
        seconds = remaining - minutes * 60
        formatStr = ""
        IF hours < 10 THEN
            formatStr = "0"
        END IF
        formatStr = formatStr + STR$(hours) + ":"
        IF minutes < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(minutes) + ":"
        IF seconds < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(seconds)
        testCount = testCount + 1
        IF hours * 3600 + minutes * 60 + seconds = totalSeconds THEN
            passCount = passCount + 1
        ELSE
            failCount = failCount + 1
        END IF
        PRINT " " + STR$(totalSeconds) + "s -> " + formatStr
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     TIME DURATION ARITHMETIC"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 9
        h1 = i
        m1 = i * 3
        s1 = i * 7
        h2 = 9 - i
        m2 = (9 - i) * 2
        s2 = (9 - i) * 5
        total1 = h1 * 3600 + m1 * 60 + s1
        total2 = h2 * 3600 + m2 * 60 + s2
        sumTotal = total1 + total2
        hours = sumTotal / 3600
        remaining = sumTotal - hours * 3600
        minutes = remaining / 60
        seconds = remaining - minutes * 60
        formatStr = ""
        IF hours < 10 THEN
            formatStr = "0"
        END IF
        formatStr = formatStr + STR$(hours) + ":"
        IF minutes < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(minutes) + ":"
        IF seconds < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(seconds)
        PRINT " " + STR$(total1) + " + " + STR$(total2) + " = " + formatStr
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     TIME DIFFERENCE (absolute)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 9
        h1 = i * 2
        m1 = i * 5
        s1 = i * 11
        h2 = (9 - i) * 2
        m2 = (9 - i) * 3
        s2 = (9 - i) * 7
        total1 = h1 * 3600 + m1 * 60 + s1
        total2 = h2 * 3600 + m2 * 60 + s2
        IF total1 > total2 THEN
            diffTotal = total1 - total2
        ELSE
            diffTotal = total2 - total1
        END IF
        hours = diffTotal / 3600
        remaining = diffTotal - hours * 3600
        minutes = remaining / 60
        seconds = remaining - minutes * 60
        formatStr = ""
        IF hours < 10 THEN
            formatStr = "0"
        END IF
        formatStr = formatStr + STR$(hours) + ":"
        IF minutes < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(minutes) + ":"
        IF seconds < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(seconds)
        PRINT " |" + STR$(total1) + " - " + STR$(total2) + "| = " + formatStr
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     COMPACT DURATION FORMAT"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 24
        totalSeconds = i * 3600 + (i * 7) * 60 + (i * 13)
        hours = totalSeconds / 3600
        remaining = totalSeconds - hours * 3600
        minutes = remaining / 60
        seconds = remaining - minutes * 60
        IF hours > 0 THEN
            formatStr = STR$(hours) + "h "
        ELSE
            formatStr = ""
        END IF
        IF minutes > 0 THEN
            formatStr = formatStr + STR$(minutes) + "m "
        END IF
        formatStr = formatStr + STR$(seconds) + "s"
        PRINT " " + STR$(totalSeconds) + "s -> " + formatStr
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     DURATION COMPARISON"
    PRINT "============================================"
    PRINT ""
    LOCAL durations(20) AS LONG
    LOCAL sorted(20) AS LONG
    LOCAL temp AS LONG
    FOR i = 0 TO 9
        durations(i) = (i * 137 + 53) MOD 5000
    NEXT i
    FOR i = 0 TO 9
        sorted(i) = durations(i)
    NEXT i
    FOR i = 0 TO 8
        FOR j = 0 TO 8 - i
            IF sorted(j) > sorted(j + 1) THEN
                temp = sorted(j)
                sorted(j) = sorted(j + 1)
                sorted(j + 1) = temp
            END IF
        NEXT j
    NEXT i
    PRINT "Sorted durations:"
    FOR i = 0 TO 9
        totalSeconds = sorted(i)
        hours = totalSeconds / 3600
        remaining = totalSeconds - hours * 3600
        minutes = remaining / 60
        seconds = remaining - minutes * 60
        formatStr = STR$(hours) + ":"
        IF minutes < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(minutes) + ":"
        IF seconds < 10 THEN
            formatStr = formatStr + "0"
        END IF
        formatStr = formatStr + STR$(seconds)
        PRINT " " + STR$(totalSeconds) + "s (" + formatStr + ")"
    NEXT i
    PRINT ""
    PRINT "Summary: " + STR$(passCount) + " passed, " + STR$(failCount) + " failed"
    FUNCTION = 0
END FUNCTION
