' LEAPYEAR.BAS - checks if a given year is a leapyear ' Author : Egbert Zijlema (E.Zijlema@uni4nn.iaf.nl) ' (up)Date : October 3, 1996 ' Language : Power Basic (3.2) ' Copyright status : Public Domain ' A leap year is a year that can be divided by 4, but not always. ' A 'round' century is only a leap if it can be divided by 400 ' (e.g.: 1900 was not a leap year, 2000 will be). ' So, we have to make a rather complicated IF-structure to test for ' the divisions needed. ' This is a more sophisticated method: just compute the Julian for ' and convert it into a valid date$. If this formula ' really returns "mm-29-yyyy" we have a leap year, for sure. ' Overkill? Keep in mind that you'll always need FUNCTION Julian& and ' SUB JulToDate within routines which are supposed to manipulate ' calendar dates. DEFINT A - Z FUNCTION Julian& (InDate$) ' converts a date into its Julian number IF LEN(InDate$) < 10 THEN EXIT FUNCTION ' format "mm-dd-yyyy" Y& = VAL( MID$(InDate$, 7) ) ' year M& = VAL( LEFT$(InDate$, 2) ) ' month D& = VAL( MID$(InDate$, 4, 2) ) ' day temp& = (M& - 14) \ 12 JulPart& = D& - 32075 + (1461 * (Y& + 4800 + temp&) \ 4) JulPart& = JulPart& + (367 * (M& - 2 - temp& * 12) \ 12) FUNCTION = JulPart& - (3 * ((Y& + 4900 + temp&) \ 100) \ 4) END FUNCTION SUB JulToDate (JN&, ResultDate$) ' converts a date's Julian number into its string format ("mm-dd-yyyy") JulNumber& = JN& + 68569 help& = 4 * JulNumber& \ 146097 JulNumber& = JulNumber& - (146097 * help& + 3) \ 4 TempYear& = 4000 * (JulNumber& + 1) \ 1461001 JulNumber& = JulNumber& - (1461 * TempYear& \ 4) + 31 TempMonth& = 80 * JulNumber& \ 2447 day = CINT(JulNumber& - (2447 * TempMonth& \ 80)) month = CINT(TempMonth& + 2 - (12 * (TempMonth& \ 11))) year = CINT(100 * (help& - 49) + TempYear& + (TempMonth& \ 11)) month$ = RIGHT$("00" + LTRIM$(RTRIM$(STR$(month))), 2) + "-" day$ = RIGHT$("00" + LTRIM$(RTRIM$(STR$(day))), 2) + "-" year$ = RIGHT$("0000" + LTRIM$(RTRIM$(STR$(year))), 4) ResultDate$ = month$ + day$ + year$ END SUB FUNCTION LeapYear (TestYear$) ' tests if a given year is a leap year JulNumber& = Julian&("02-28-" + TestYear$) INCR JulNumber& ' the next day we need! JulToDate JulNumber&, Result$ ' convert to stringformat IF LEFT$(Result$, 5) = "02-29" THEN FUNCTION = -1 ELSE FUNCTION = 0 END IF END FUNCTION ' calling module CLS PRINT "1600 was "; IF LeapYear("1600") = 0 THEN PRINT "not "; PRINT "a leap year" PRINT "1995 was "; IF LeapYear("1995") = 0 THEN PRINT "not" ELSE PRINT "a leap year as well" END IF PRINT "2001 will "; IF LeapYear("2001") = 0 THEN PRINT "not "; PRINT "be a leap year" PRINT PRINT "And this is how the formula works:" PRINT "Initial date: 02-28-1996"; JulToDate Julian&("02-28-1996") + 1, result$ PRINT " / Next day: ";result$;" = leap year" PRINT "Initial date: 02-28-1997"; JulToDate Julian&("02-28-1997") + 1, result$ PRINT " / Next day: ";result$;" = not a leap year" END