isLeap

Korn Shell 93 function to calculate leap year.



#!/usr/bin/ksh93
################################################################
####    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
####    
####    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@xoommail.com
####
################################################################
isLeap()
{
  typeset TRUE="0"
  typeset FALSE="1"
  typeset STATUS="-1"
  typeset DFLAG="${FALSE}"
  typeset YFLAG="${FALSE}"
  typeset YEAR=`date +"%Y"`
  typeset OPTION

  while getopts ':dy:' OPTION
  do
    case ${OPTION} in
        'd') DFLAG="${TRUE}";;
        'y') YFLAG="${TRUE}"
             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} == 4 ))
  then
    STATUS="${FALSE}"
    ( ( (( ( YEAR % 4 )   == 0 )) &&
        (( ( YEAR % 100 ) != 0 )) ) ||
        (( ( YEAR % 400 ) == 0 )) ) &&
      STATUS="${TRUE}"

    (( DFLAG == TRUE )) && (( STATUS == TRUE )) && print "29"
    (( DFLAG == TRUE )) && (( STATUS == FALSE )) && print "28"
  else
    print "ERROR: Invalid year specified"
  fi
  return ${STATUS}
}