Tuesday, September 25, 2012

ksh Date Validation.

#!/bin/ksh
# usage: vdate.shl where date = month/day/year
# valid for date = or > 10/04/1582 -- start of Gregorian calendar
#http://www.unix.com/shell-programming-scripting/108048-validate-date-format-argument.html

isleapyr()
{
    isleap=0
    if [[ $(( ${1} % 400 )) -eq 0  ||
          $(( ${1} % 4 )) -eq 0 && $(( ${1} % 100 )) -ne 0 ]]
    then
       isleap=1
    fi
}

isvaliddate()
{
     typeset -i day=$1
     typeset -i month=$2
     typeset -i year=$3
    set -A arr 31 28 31 30 31 30 31 31 30 31 30 31
   
     if [[ $year -lt 34 ]] ; then  # assume 2000
         year=$(( $year + 2000 ))
     fi
     if [[ $year -gt 33 && $year -lt 100 ]] ; then  # assume 1900
         year=$(( $year + 1900 ))
     fi
    
     isleapyr $year
    if [[ $isleap -eq 1 ]] ; then
        arr[1]=29;
    fi

     if [[ $month -gt 0 && $month -lt 13 ]] ; then
         month=$(( $month - 1 ))
         if [[ $day -gt 0 && $day -le ${arr[month]} ]] ; then
             return 0
         fi
     fi
     return 1
}

#echo "$1" | tr -s '/' ' ' | read month day year
echo "date = "$1
month =`$1| cut -c1-2`
echo " month  = "$month
#isvaliddate $day $month $year
#if [[ $? -eq 0 ]] ; then
# echo "$1 ok"
#else
# echo "$1 not ok"
#fi
#


No comments:

Post a Comment