Listing 7 is_valid_date
function is_valid_date
{
set -A DAYS_TO_MONTH NA 0 31 59 90 120 151 181 212 243 273 304 334 365
#---------------
# Get the input.
#---------------
integer Y=$1
M=$2
integer D=$3
#-------------------------------------------
# If necessary, convert month to an integer.
#-------------------------------------------
if ! is_integer $M 2> /dev/null
then
M=$(month_to_int $M)
fi
#--------------------
# Validate the input.
#--------------------
if ! is_integer $Y 2> /dev/null || (( $Y < 0 ))
then
echo "invalid year"
return 1
fi
if (( $M < 1 )) || (( $M > 12 ))
then
echo "invalid month"
return 1
fi
if ! is_integer $D 2> /dev/null || (( $D < 1 )) || (( $D > 31 ))
then
echo "invalid day"
return 1
else
if (( $D > $((${DAYS_TO_MONTH[$((M+1))]}-${DAYS_TO_MONTH[$M]})) ))
then
if (( $M == 2 )) && is_leap_year $Y && (( $D == 29 ))
then
: # OK
else
echo "invalid day"
return 1
fi
fi
fi
return 0
}
# End Listing 7
|