1850829 Members
2889 Online
104056 Solutions
New Discussion

Is this a leap year?

 
SOLVED
Go to solution
Robert Fisher_1
Frequent Advisor

Is this a leap year?

Hi everyone,

My boss just asked me late on a Friday afternoon if I could come up with an easy way to know if tomorrow will be Feb. 29 or March 1. I plan to run a cron script on 02 28 at 8PM each year.

Any quick ideas so that I can leave?

Please help, Bob
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Is this a leap year?

Hi Bob:

Well, we could go through the year % 4, not century year unless divisdible by 400 but why bother?

if [[ $(caljd.sh -n 1 -M) != $(caljd.sh -M) ]]
then
echo "This is a leap year"
else
echo "This ain't no leap year"
fi

This does assume that you run it only on 02/28.
If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: Is this a leap year?

Hi Robert,

Search the forum for caljd.sh or caljd.pl
These are Clay's date hammers & they will do anything with dates that you could ever desire.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Rodney Hills
Honored Contributor

Re: Is this a leap year?

As I remember,

The rule of thumb has always been if the year is divisible by 4 then it is a leap year.

Unless it is divisible by 100, then it is not.

Unless it is divisible by 400, then it is.

HTH

-- Rod Hills
There be dragons...
Robert Fisher_1
Frequent Advisor

Re: Is this a leap year?

Wow that was fast!!!

Clay, the -M tells the "date hammer" to output the month. Is this correct? All you are doing is seeing if tomorrow is a different month? Now why couldn't I figure that out?

Thanks and it looks like I will be outa here soon!!!

This place rocks.

Thanks again,
Bob
A. Clay Stephenson
Acclaimed Contributor

Re: Is this a leap year?

Hi Bob:

Yes, that's all that I'm doing. Now as to why you couldn't figure that out - well, I could come up with a few possible answers but you probably wouldn't like them.

Regards, Clay
If it ain't broke, I can fix that.
Brian M Rawlings
Honored Contributor

Re: Is this a leap year?

IF =
THEN
ELSE
END

8^) --bmr
We must indeed all hang together, or, most assuredly, we shall all hang separately. (Benjamin Franklin)
Carlos Fernandez Riera
Honored Contributor

Re: Is this a leap year?

For fun...

p=$(cal 2 2003 | tail -2)
set -u $p
eval j=\$$#

if [ $j == "28]; then
echo 2003 is not leap
else
echo leap year
fi


unsupported