' Time Zone Converter — converts between time zones using offset arithmetic, no string arrays
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL localHour AS LONG
    LOCAL localMin AS LONG
    LOCAL localOffset AS LONG
    LOCAL targetOffset AS LONG
    LOCAL targetHour AS LONG
    LOCAL targetMin AS LONG
    LOCAL targetDay AS LONG
    LOCAL utcHour AS LONG
    LOCAL utcMin AS LONG
    LOCAL totalMinutes AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL localDay AS LONG
    LOCAL zoneName AS STRING
    LOCAL targetName AS STRING
    LOCAL formattedLocal AS STRING
    LOCAL formattedUTC AS STRING
    LOCAL formattedTarget AS STRING
    LOCAL abbr AS STRING
    LOCAL fullName AS STRING
    LOCAL zone0 AS STRING
    LOCAL zone1 AS STRING
    LOCAL zone2 AS STRING
    LOCAL zone3 AS STRING
    LOCAL zone4 AS STRING
    LOCAL zone5 AS STRING
    LOCAL zone6 AS STRING
    LOCAL zone7 AS STRING
    LOCAL zone8 AS STRING
    LOCAL zone9 AS STRING
    LOCAL zone10 AS STRING
    LOCAL zone11 AS STRING
    LOCAL off0 AS LONG
    LOCAL off1 AS LONG
    LOCAL off2 AS LONG
    LOCAL off3 AS LONG
    LOCAL off4 AS LONG
    LOCAL off5 AS LONG
    LOCAL off6 AS LONG
    LOCAL off7 AS LONG
    LOCAL off8 AS LONG
    LOCAL off9 AS LONG
    LOCAL off10 AS LONG
    LOCAL off11 AS LONG
    LOCAL dstOffset AS LONG
    LOCAL dstAdjustment AS LONG
    LOCAL zoneCount AS LONG
    zoneCount = 12
    zone0 = "UTC-12"
    off0 = -12
    zone1 = "UTC-8"
    off1 = -8
    zone2 = "UTC-5"
    off2 = -5
    zone3 = "UTC"
    off3 = 0
    zone4 = "UTC+1"
    off4 = 1
    zone5 = "UTC+2"
    off5 = 2
    zone6 = "UTC+3"
    off6 = 3
    zone7 = "UTC+5:30"
    off7 = 5
    zone8 = "UTC+8"
    off8 = 8
    zone9 = "UTC+9"
    off9 = 9
    zone10 = "UTC+10"
    off10 = 10
    zone11 = "UTC+12"
    off11 = 12
    PRINT "============================================"
    PRINT "        TIME ZONE CONVERTER"
    PRINT "============================================"
    PRINT ""
    PRINT "UTC offset table:"
    PRINT ""
    PRINT " " + zone0 + " (offset " + STR$(off0) + "h)"
    PRINT " " + zone1 + " (offset " + STR$(off1) + "h)"
    PRINT " " + zone2 + " (offset " + STR$(off2) + "h)"
    PRINT " " + zone3 + " (offset " + STR$(off3) + "h)"
    PRINT " " + zone4 + " (offset " + STR$(off4) + "h)"
    PRINT " " + zone5 + " (offset " + STR$(off5) + "h)"
    PRINT " " + zone6 + " (offset " + STR$(off6) + "h)"
    PRINT " " + zone7 + " (offset " + STR$(off7) + "h)"
    PRINT " " + zone8 + " (offset " + STR$(off8) + "h)"
    PRINT " " + zone9 + " (offset " + STR$(off9) + "h)"
    PRINT " " + zone10 + " (offset " + STR$(off10) + "h)"
    PRINT " " + zone11 + " (offset " + STR$(off11) + "h)"
    PRINT ""
    PRINT "============================================"
    PRINT "     CONVERSION FROM LOCAL TIME"
    PRINT "============================================"
    PRINT ""
    localHour = 14
    localMin = 30
    localOffset = -5
    localDay = 1
    formattedLocal = STR$(localHour) + ":"
    IF localMin < 10 THEN
        formattedLocal = formattedLocal + "0" + STR$(localMin)
    ELSE
        formattedLocal = formattedLocal + STR$(localMin)
    END IF
    PRINT "Local time: " + formattedLocal + " (offset " + STR$(localOffset) + "h)"
    PRINT ""
    totalMinutes = localHour * 60 + localMin - localOffset * 60
    utcHour = totalMinutes / 60
    utcMin = totalMinutes - utcHour * 60
    IF utcHour < 0 THEN
        utcHour = utcHour + 24
        localDay = 0
    END IF
    IF utcHour >= 24 THEN
        utcHour = utcHour - 24
        localDay = 2
    END IF
    formattedUTC = STR$(utcHour) + ":"
    IF utcMin < 10 THEN
        formattedUTC = formattedUTC + "0" + STR$(utcMin)
    ELSE
        formattedUTC = formattedUTC + STR$(utcMin)
    END IF
    PRINT " UTC time: " + formattedUTC
    PRINT ""
    PRINT "Converting from " + formattedLocal + " (UTC" + STR$(localOffset) + ") to other zones:"
    PRINT ""
    FOR i = 0 TO zoneCount - 1
        IF i = 0 THEN targetName = zone0
        IF i = 0 THEN targetOffset = off0
        IF i = 1 THEN targetName = zone1
        IF i = 1 THEN targetOffset = off1
        IF i = 2 THEN targetName = zone2
        IF i = 2 THEN targetOffset = off2
        IF i = 3 THEN targetName = zone3
        IF i = 3 THEN targetOffset = off3
        IF i = 4 THEN targetName = zone4
        IF i = 4 THEN targetOffset = off4
        IF i = 5 THEN targetName = zone5
        IF i = 5 THEN targetOffset = off5
        IF i = 6 THEN targetName = zone6
        IF i = 6 THEN targetOffset = off6
        IF i = 7 THEN targetName = zone7
        IF i = 7 THEN targetOffset = off7
        IF i = 8 THEN targetName = zone8
        IF i = 8 THEN targetOffset = off8
        IF i = 9 THEN targetName = zone9
        IF i = 9 THEN targetOffset = off9
        IF i = 10 THEN targetName = zone10
        IF i = 10 THEN targetOffset = off10
        IF i = 11 THEN targetName = zone11
        IF i = 11 THEN targetOffset = off11
        totalMinutes = utcHour * 60 + utcMin + targetOffset * 60
        targetHour = totalMinutes / 60
        targetMin = totalMinutes - targetHour * 60
        targetDay = 1
        IF targetHour < 0 THEN
            targetHour = targetHour + 24
            targetDay = 0
        ELSE
            IF targetHour >= 24 THEN
                targetHour = targetHour - 24
                targetDay = 2
            END IF
        END IF
        formattedTarget = STR$(targetHour) + ":"
        IF targetMin < 10 THEN
            formattedTarget = formattedTarget + "0" + STR$(targetMin)
        ELSE
            formattedTarget = formattedTarget + STR$(targetMin)
        END IF
        IF targetDay = 0 THEN
            formattedTarget = "(prev) " + formattedTarget
        ELSE
            IF targetDay = 2 THEN
                formattedTarget = "(next) " + formattedTarget
            ELSE
                formattedTarget = "       " + formattedTarget
            END IF
        END IF
        PRINT " " + targetName + ": " + formattedTarget
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     ALL ZONES FROM UTC"
    PRINT "============================================"
    PRINT ""
    FOR localHour = 0 TO 5
        formattedLocal = ""
        IF localHour < 10 THEN
            formattedLocal = "0" + STR$(localHour)
        ELSE
            formattedLocal = STR$(localHour)
        END IF
        formattedLocal = formattedLocal + ":00 UTC"
        PRINT " " + formattedLocal
        FOR j = 0 TO zoneCount - 1
            IF j = 0 THEN targetName = zone0
            IF j = 0 THEN targetOffset = off0
            IF j = 1 THEN targetName = zone1
            IF j = 1 THEN targetOffset = off1
            IF j = 2 THEN targetName = zone2
            IF j = 2 THEN targetOffset = off2
            IF j = 3 THEN targetName = zone3
            IF j = 3 THEN targetOffset = off3
            IF j = 4 THEN targetName = zone4
            IF j = 4 THEN targetOffset = off4
            IF j = 5 THEN targetName = zone5
            IF j = 5 THEN targetOffset = off5
            IF j = 6 THEN targetName = zone6
            IF j = 6 THEN targetOffset = off6
            IF j = 7 THEN targetName = zone7
            IF j = 7 THEN targetOffset = off7
            IF j = 8 THEN targetName = zone8
            IF j = 8 THEN targetOffset = off8
            IF j = 9 THEN targetName = zone9
            IF j = 9 THEN targetOffset = off9
            IF j = 10 THEN targetName = zone10
            IF j = 10 THEN targetOffset = off10
            IF j = 11 THEN targetName = zone11
            IF j = 11 THEN targetOffset = off11
            totalMinutes = localHour * 60 + targetOffset * 60
            targetHour = totalMinutes / 60
            targetMin = totalMinutes - targetHour * 60
            IF targetMin < 0 THEN
                targetMin = targetMin + 60
                targetHour = targetHour - 1
            END IF
            IF targetHour < 0 THEN
                targetHour = targetHour + 24
            END IF
            IF targetHour >= 24 THEN
                targetHour = targetHour - 24
            END IF
            formattedTarget = STR$(targetHour) + ":"
            IF targetMin < 10 THEN
                formattedTarget = formattedTarget + "0" + STR$(targetMin)
            ELSE
                formattedTarget = formattedTarget + STR$(targetMin)
            END IF
            PRINT "   " + targetName + " " + formattedTarget
        NEXT j
    NEXT localHour
    PRINT ""
    PRINT "Done"
    FUNCTION = 0
END FUNCTION
