[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: perl leap year function?



>function isLeapYear(year as int) as boolean
>	if remainder(year div 4) = 0 then
>		return true
>	else return false
>end
>
>I realise this isnt the only condition (theres something fishy about years
>divisible by 400 from memory) but it would do the trick.

The "fishy" part is that years divisible by 100 aren't leap years,
whereas those divisible by 400 are. So 1700, 1800 and 1900 weren't
leap years, but Y2K will be.

>Of course, hardcoding the next few will tide you over for what is most
>likely enough time, im just a sucker for flexible code.

In that case, I suggest enhancing the above pseudo code as follows: :-)

function isLeapYear(year as int) as boolean
  if ((remainder(year div 400) = 0) or (remainder(year div 100) <> 0 and
                                        remainder(year div 4) = 0)) then
	return true
  else return false
end

Greets,
Matt


Reply to: