sandra_henrystocker
Unix Dweeb

Determining leap years on Linux

How-To
18 Mar 20244 mins
Linux

Leap years are not quite as regular as we might have believed. A fairly simple Linux script can tell you whether a year will be a leap year or not.

Top view of multiracial young creative people in modern office. Group of young business people are working together with laptop, tablet, smart phone, notebook. Successful hipster team in coworking.
Credit: 4 PM production / Shutterstock

While it’s easy to contemplate every fourth February having 29 days instead of 28, this isn’t exactly true. In reality, the situation is a bit more complicated. Every fourth year (years which, divided by 4, leave no remainder), like 2024, is a leap year, except those evenly divisible by 100 unless they’re also evenly divisible by 400. Does this seem like news? This rule has been in effect since 1582 when the Gregorian calendar was first introduced.

So, if you’re still around in February 2100, you’ll likely notice that it will last only 28 days. February 2400, on the other hand, like Feb 2000, will have 29. Given the sparsity of 28-day Februaries in years divisible by 4, it should come as no surprise if most of us never notice that they sometimes have only 28 days. February 2000 wasn’t all that long ago but, evenly divisible by both 100 and 400, it managed to hold onto that 29th day. February 2100 will not.

    February 2000          February 2100          February 2400
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
       1  2  3  4  5       1  2  3  4  5  6          1  2  3  4  5
 6  7  8  9 10 11 12    7  8  9 10 11 12 13    6  7  8  9 10 11 12
13 14 15 16 17 18 19   14 15 16 17 18 19 20   13 14 15 16 17 18 19
20 21 22 23 24 25 26   21 22 23 24 25 26 27   20 21 22 23 24 25 26
27 28 29               28                     27 28 29

The reason for what may seem like an odd way of determining which Februaries get an extra day and which don’t is actually both clever and mathematically reasonable. We might imagine that the Earth takes 365 days to orbit the Sun, but even this isn’t quite accurate. It actually takes something like 365 days, 5 hours, 48 minutes and 56 seconds to orbit the sun. The periodic – not quite every 4 years – addition of that extra day helps to keep the seasons of the year relatively consistent.

While none of this specifically impacts Linux, you can put together a fairly simple script to tell you whether a year will be a leap year (with a 29-day February) or not. Here’s an example script that I concocted.

#!/bin/bash

# prompt for year if not provided
if [ $# == 0 ]; then
  echo -n "year> "
  read year
  leap="pending"
else
  year=$1
fi

# make sure year is numeric
re='^[0-9]+$'
if ! [[ $year =~ $re ]] ; then
  echo "Oops! That is not a year."
  exit
fi

# check if it’s a leap year
if [ $(( year % 400)) -eq 0 ] ; then
  echo ===================
  echo $year IS a leap year
  echo ===================
elif [ $(( year % 100)) -eq 0 ] ; then
  echo =======================
  echo $year is NOT a leap year
  echo =======================
elif [ $(( year % 4)) -eq 0 ] ; then
  echo ===================
  echo $year IS a leap year
  echo ===================
else
  echo =======================
  echo $year is NOT a leap year
  echo =======================
fi

cal Feb $year

The first group of lines simply checks if an argument has been provided and, if not, prompts for one.  The second group of lines verifies that the argument provided is numeric by checking it against a regular expression that represents numbers.

If the year provided is evenly divisible by 400, the script acknowledges that it is a leap year and then jumps to the bottom to confirm this by displaying a calendar for that year’s February. Otherwise, it checks if the number is evenly divisible by 100. If it is, then the year is NOT a leap year. Next, it checks if the year is evenly divisible by 4. Since it’s already ruled out years evenly divisible by 100, but not 400, any positive response to this check ensures the year is a leap year. Otherwise, the script confirms that the year is, once again, NOT a leap year.

All in all, the logic is fairly simple if you take it one step at a time and understand that leap years are not quite as regular as we might have believed. Why they’re not is actually quite interesting.

Exit mobile version