-->

kshWeb isLeap
Leap Year Calculation

 

kshWeb Home | ksh Applications, Tools, Utilities | Obtain Korn Shell | Shell News Groups | Publications | Links
Korn Shell Jobs | Tip-O-The-Day | Visitor Supplied Scripts | GuestBook | Registration and Download | MicroEMACS

FREE On-Line
Business Card File
Data Entry System
FREE On-Line
Korn Shell Books Free E-mail
YourName@ UnixGuru.zzn.com
YourName@ UnixWizard.zzn.com
YourName@ MCSE.zzn.com
... and many others!!!

Cut and paste the following script into a file called "isLeap.ksh" on your system. Or click on isLeap.ksh to download a file containing the function.

#!/bin/ksh
################################################################
####    Korn Shell function to determine whether or not a specified
####    4 digit year is a leap year.  This function is designed to
####    return the result as the return status of the function.
####    The isLeap function returns 0 if the year is a leap year,
####    and 1 if it is not a leap year.
####
####    Load this file into your current environment as follows:
####    
####    . ./isLeap.ksh
####    
####    Thats "dot-space-dot-slash-isLeap-dot-ksh"
####
####    You will then be able to issue the command "isLeap"
####    from your current environment.
####
####    SYNTAX: isLeap [-d] [-y ####]
####
####                   -d    print the number of days in february to STDOUT
####
####                   -y    Require a 4 digit year to be specified.  
####                         performs leap year test against specified year.
####
####    AUTHOR: Dana French
####    EMAIL: dfrench@mtxia.com
####
################################################################
isLeap()
{
STATUS="-1"
DFLAG=""
YFLAG=""
YEAR=`date +"%Y"`
while getopts dy: NAME
do
    case "_${NAME}" in
        "_d") DFLAG="1";;
        "_y") YFLAG="1"; YEAR="${OPTARG}";;
        "_?") print "
SYNTAX: isLeap [-d] [-y ####]

        -d    print the number of days in february to STDOUT

        -y    Require a 4 digit year to be specified.  
              performs leap year test against specified year.

"; return ${STATUS};;
esac
done

if [[ "_${YEAR}" != "_" && ${#YEAR} -eq 4 ]]
then

    STATUS="1"
    [[ $(( ${YEAR} % 4 )) -eq 0 && $(( ${YEAR} % 100 )) -ne 0 ]] \
    || [[ $(( ${YEAR} % 400 )) -eq 0 ]] && STATUS="0"

    if [[ "_${DFLAG}" != "_" &&  "_${DFLAG}" = "_1" && "_${STATUS}" = "_0" ]]
    then
        print "29"
    fi
    if [[ "_${DFLAG}" != "_" &&  "_${DFLAG}" = "_1" && "_${STATUS}" = "_1" ]]
    then
        print "28"
    fi
else
    print "ERROR: Invalid year specified"
fi
return ${STATUS}
}
################################################################

kshWeb Home | ksh Applications, Tools, Utilities | Obtain Korn Shell | Shell News Groups | Publications | Links
Korn Shell Jobs | Tip-O-The-Day | Visitor Supplied Scripts | GuestBook | Registration and Download | MicroEMACS

FREE On-Line
Business Card File
Data Entry System
FREE On-Line
Korn Shell Books Free E-mail
YourName@ UnixGuru.zzn.com
YourName@ UnixWizard.zzn.com
YourName@ MCSE.zzn.com
... and many others!!!

 

For Information regarding this page, contact Dana French ( dfrench@mtxia.com )