Software, your way.
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

The HTML Q Tag

Create a style then use the HTML q tag to apply the style. This is about as easy as it gets for attracting attention to quotes.

Plain Text To HTML Converter

Here is a function to convert plain text from a form textarea field into HTML for publishing on a web page or in an email.

HTML for Meme Making

No fancy image creation software is needed to make memes. An easy way is to create a web page with text over an image or color background and screenshot it.

Image Color Saturation

The CSS filter:saturate() declaration can be used to lesson color saturation and to intensify color saturation of images.

PDF to Text Conversion

If you can use a PDF to text converter and your website is on a Linux machine, this one should do you well.

Text Emphasis

When your writing can be improved with the correct emphasis, this CSS may be used.

Dropdown Links

I used to see many dropdown links during the earlier part of my internet experience.

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

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