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. 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. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe