Code archives/Algorithms/Leap years

This code has been declared by its author to be Public Domain code.

Download source code

Leap years by xlsior2007
Pretty self-explanatory: Pass a year, and find out whether or not it's a Leap Year.

The date range in this example was chosen to show that it correctly identifies 1900 as being not a leap year. (The year 2000 on the other hand is one)
'
' IsLeap - Returns TRUE for Leap Years, FALSE for non-Leap Years
'
' By Marc van den Dikkenberg
'
Strict

For Local Year:Int=1890 To 1920
	Print "Year "+year+" "+IsLeap(Year)
Next

Function IsLeap(SomeDate:String)   ' Pass either just the year, or a "DD MMM YYYY" BlitzMax data string
	Local Jaar:Int=Int(Right$(SomeDate,4))
		If ((Jaar Mod 4) = 0 And (Jaar Mod 100) <> 0) Or ((Jaar Mod 4) = 0 And (Jaar Mod 400) = 0) Then 
		' Leap Year	-- Any year divisible by 4, except the centuries unless they are multiples of 400
		Return True
	Else
		' Not a Leap Year
		Return False
	End If
End Function

Comments

Yo! Wazzup?2008
Converted to BlitzBasic, wasn't hard :D

I'm getting the hang of Blitzmax so doing this is helpful for me.

Had to take out some comments, sorry.

I tested by typing in 2008 then 2007, works, I think...
;
; IsLeap - Returns True For Leap Years, False For non-Leap Years
;
; By Marc van den Dikkenberg
; Conversion to BlitzBasic by Mortifer Nex
;

year$=Input("Enter in a year: ")
If IsLeap(year$) Print "That year is a leap year" Else Print "That year is not a leap year."

Function IsLeap(SomeDate$)
	Local Jaar%=Right$(SomeDate,4)
Return ((Jaar Mod 4) = 0 And (Jaar Mod 100) <> 0) Or ((Jaar Mod 4) = 0 And (Jaar Mod 400) = 0)
End Function

The following code gets the date using CurrentDate() and uses it...

If IsLeap(CurrentDate()) Print "This year is a leap year" Else Print "This year is not a leap year."

Function IsLeap(SomeDate$)
	Local Jaar%=Right$(SomeDate,4)
Return ((Jaar Mod 4) = 0 And (Jaar Mod 100) <> 0) Or ((Jaar Mod 4) = 0 And (Jaar Mod 400) = 0)
End Function



_PJ_2010
I would add a (* Jaar) to the Return values, to prevent failing by giving a True result for year '0'.


Floyd2010
The year 0 is a leap year according to the current rules.

A leap year is a year divible by 4. An exception is made for "century years", meaning the year is divisible by 100. In that case you divide by 100 and apply the rule to what remains. Thus 2000 is a leap year but 1900 is not, as is seen by considering 20 and 19.

That makes 0 a leap year. Of course, you could argue that leap years don't really apply prior to 1600 because the Gregorian calendar was not yet in effect.


Warpy2010
Many people make the claim that there was no year 0 - It went from 1BC straight to 1AD. These are the same people who were at home being boring on Millennium Eve, though...


Streaksy2010
I was home bored on milennium eve like a sad bugger but i know there was a year 0 in gregorian calanders ;/


_PJ_2010
Certainly the math is correct, and by those rules, yes, ywear 0 would be a leaop year, had it existed, but Warpy's right. There was no 'Year 0'.

Though my reasons for omitting it were more aimed at error prevention in case of null values being passed in, rather than specific checks for a year '0'.

There's another, possible issue with attempts to identify BC years with negative numbers, since these would be incorrect.
For example, 1600 BC was NOT a leap year, but 1599 BC was.


Jimmy2011
Nice trick, you could also shorten that without modifying it like so:

If ((year Mod 4)=0) And ((year Mod 100)<>0) Or ((year Mod 400)=0) Then Print "Leapyear!"


_PJ_2011
The prooblem is, after a while, making things too short makes them unreadable for when trying to understand what the code is doing.
If (Not((year Mod 4)*(year Mod 400)) * (year Mod 100)) Then Print "Leapyear!"



Code Archives Forum