Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMasterBlog > Perl

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

How To Calculate Leap Years with Perl

I prefer to use the localtime and timelocal functions when doing calendar routines. It's so much easier.

However, these functions can only work on dates between December 31, 1969 and January 18, 2038.

While programming the new Your Daily Lucky Numbers web page, the daily lucky numbers charts could span up to a year and include traversing from one year to the next. Although the site is free, I could not see restricting users to charts of years 1970 through 2037.

So I made my own code. And I thought I would share the leap year calculation subroutine with you.

First I'll present the code, then the rules for calculating which years are leap years, and finally the code again but with extensive comments interspersed.

The Code

sub IsLeapYear
{
   my $year = shift;
   return 0 if $year % 4;
   return 1 if $year % 100;
   return 0 if $year % 400;
   return 1;
}

The IsLeapYear subroutine is called with a year number, like
IsLeapYear(2006);

The function then returns a number 1 if the year number is a leap year. It returns a number 0 if it is not a leap year.

This line will provide the $February value with the correct number of days for the given year:
$February = IsLeapYear(2006) ? 29 : 28;

The Rules

If the 4-digit year is not evenly divisible by 4, it is not a leap year.

If it is evenly divisible by 4 and also evenly divisible by 100, it is not a leap year — unless it is also evenly divisible by 400.

Therefore:

  • If it is evenly divisible by 4 and not evenly divisible by 100, it is a leap year.

  • If it is evenly divisible by 4 and is also evenly divisible by 400, it is a leap year.

  • If it is evenly divisible by 4 and also evenly divisible by 100 and also evenly divisible by 400, it is a leap year.

  • However, if it is evenly divisible by 4 and also evenly divisible by 100 and not further evenly divisible by 400, then it is not a leap year.

1600 was a leap year. 1700, 1800, and 1900 were not leap years. 2000 was a leap year.

The Comments

sub IsLeapYear
{
   # Either the value 0 or the value 1 is returned. 
   #     If 0, it is not a leap year. If 1, it is a 
   #     leap year. (Works for Julian calendar, 
   #     established in 1582)

   # Accept the value sent to this subroutine.

   my $year = shift;

   # If $year is not evenly divisible by 4, it is 
   #     not a leap year; therefore, we return the 
   #     value 0 and do no further calculations in 
   #     this subroutine. ("$year % 4" provides the 
   #     remainder when $year is divided by 4. 
   #     If there is a remainder then $year is 
   #     not evenly divisible by 4.)

   return 0 if $year % 4;

   # At this point, we know $year is evenly divisible 
   #     by 4. Therefore, if it is not evenly 
   #     divisible by 100, it is a leap year -- 
   #     we return the value 1 and do no further 
   #     calculations in this subroutine.

   return 1 if $year % 100;

   # At this point, we know $year is evenly divisible 
   #     by 4 and also evenly divisible by 100. Therefore, 
   #     if it is not also evenly divisible by 400, it is 
   #     not leap year -- we return the value 0 and do no 
   #     further calculations in this subroutine.

   return 0 if $year % 400;

   # Now we know $year is evenly divisible by 4, evenly 
   #     divisible by 100, and evenly divisible by 400. 
   #     We return the value 1 because it is a leap year.

   return 1;
}

It's a handy little subroutine.

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Keeping Image Location Secret

The URL of an image embedded in a web page may be kept secret.

Easier Reading of JSON Data

For easier reading of JSON data, convert the JSON into an array.

Fixed Position Image

Position an image within a browser window that won't move even with page scroll.

Visually Centering Images

Sometimes an image that is technically centered doesn't look quite centered when viewed.

Cookie Directory Protection

Protecting subdirectories with a cookie can be an especially good method when access needs to be allowed from various internet connections.

Check SSL Certificate

An easy-to-use SSL checker to see when your secure certificate expires.

Strong Form Protection From Bots

A web page form that is invisible to spam bots.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC