' Calendar: day-of-week, days-in-month, leap year, month print
#COMPILE EXE
#DIM ALL

FUNCTION IsLeapYear(BYVAL year AS LONG) AS LONG
    IF year MOD 400 = 0 THEN
        FUNCTION = 1
        EXIT FUNCTION
    END IF
    IF year MOD 100 = 0 THEN
        FUNCTION = 0
        EXIT FUNCTION
    END IF
    IF year MOD 4 = 0 THEN
        FUNCTION = 1
    ELSE
        FUNCTION = 0
    END IF
END FUNCTION

FUNCTION DaysInMonth(BYVAL year AS LONG, BYVAL month AS LONG) AS LONG
    IF month = 2 THEN
        IF IsLeapYear(year) THEN FUNCTION = 29 ELSE FUNCTION = 28
    ELSEIF month = 4 OR month = 6 OR month = 9 OR month = 11 THEN
        FUNCTION = 30
    ELSE
        FUNCTION = 31
    END IF
END FUNCTION

FUNCTION DayOfWeek(BYVAL year AS LONG, BYVAL month AS LONG, BYVAL day AS LONG) AS LONG
    LOCAL y AS LONG
    LOCAL m AS LONG
    LOCAL d AS LONG
    LOCAL dow AS LONG
    LOCAL cent AS LONG
    IF month <= 2 THEN
        year = year - 1
        month = month + 12
    END IF
    y = year MOD 100
    m = month
    d = day
    cent = year / 100
    dow = (d + (13 * (m + 1)) / 5 + y + y / 4 + cent / 4 + 5 * cent) MOD 7
    FUNCTION = ((dow + 6) MOD 7) + 1
END FUNCTION

SUB DayName(BYVAL dow AS LONG, BYREF result AS STRING)
    IF dow = 1 THEN result = "Sunday"
    IF dow = 2 THEN result = "Monday"
    IF dow = 3 THEN result = "Tuesday"
    IF dow = 4 THEN result = "Wednesday"
    IF dow = 5 THEN result = "Thursday"
    IF dow = 6 THEN result = "Friday"
    IF dow = 7 THEN result = "Saturday"
END SUB

SUB MonthName(BYVAL m AS LONG, BYREF result AS STRING)
    IF m = 1 THEN result = "January"
    IF m = 2 THEN result = "February"
    IF m = 3 THEN result = "March"
    IF m = 4 THEN result = "April"
    IF m = 5 THEN result = "May"
    IF m = 6 THEN result = "June"
    IF m = 7 THEN result = "July"
    IF m = 8 THEN result = "August"
    IF m = 9 THEN result = "September"
    IF m = 10 THEN result = "October"
    IF m = 11 THEN result = "November"
    IF m = 12 THEN result = "December"
END SUB

FUNCTION DaysInYear(BYVAL year AS LONG) AS LONG
    IF IsLeapYear(year) THEN FUNCTION = 366 ELSE FUNCTION = 365
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL s AS STRING
    PRINT "=== Calendar Utility ==="
    PRINT
    PRINT "Leap year checks:"
    IF IsLeapYear(2000) THEN PRINT "  2000: leap" ELSE PRINT "  2000: common"
    IF IsLeapYear(1900) THEN PRINT "  1900: leap" ELSE PRINT "  1900: common"
    IF IsLeapYear(2024) THEN PRINT "  2024: leap" ELSE PRINT "  2024: common"
    PRINT
    PRINT "Day-of-week:"
    CALL DayName(DayOfWeek(2024, 1, 1), s)
    PRINT "  2024-01-01 is " & s
    CALL DayName(DayOfWeek(2024, 12, 25), s)
    PRINT "  2024-12-25 is " & s
    CALL DayName(DayOfWeek(2024, 7, 4), s)
    PRINT "  2024-07-04 is " & s
    PRINT
    PRINT "Days in February:"
    PRINT "  2023: " & FORMAT$(DaysInMonth(2023, 2))
    PRINT "  2024: " & FORMAT$(DaysInMonth(2024, 2))
    PRINT
    PRINT "Days in year 2024: " & FORMAT$(DaysInYear(2024))
    PRINT
    PRINT "Calendar utility tests complete."
    FUNCTION = 0
END FUNCTION
