Find the answer to your Linux question:
Results 1 to 3 of 3
The program below determines if a date is valid or not taking into account things like how many days are in that month, including February as well as accounting for ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122

    Date Validation Success


    The program below determines if a date is valid or not taking into account things like how many days are in that month, including February as well as accounting for leap year. I've been working on it for a few weeks now.

    Code:
    #!/bin/bash
    #set -x
    
    CALENDAR=(31 28 31 30 31 30 31 31 30 31 30 31)
    
    #############################
    ### FUNCTION-is_leap_year ###
    #############################
    
    is_leap_year() {
    	local PART1=$(expr ${1} % 4)
    	local PART2=$(expr ${1} % 100)
    	local RESULT="false"
    
    	if [[ 0 -eq ${PART1} ]] && [[ 0 -ne ${PART2} ]] || [[ 0 -eq $(expr ${1} % 400) ]]; then #{
    		RESULT="true"
    	fi #}
    
    	echo ${RESULT}
    }
    
    ####################
    ### END FUNCTION ###
    ####################
    
    ########################
    ### FUNCTION-get_day ###
    ########################
    
    get_day() {
    	local MM=${1}
    	local DD=${2}
    	local FIRSTDIGIT=${MM:0:1}
    	local SECONDDIGIT=${MM:1:1}
    	local DAY="BAD"
    
    	if [[ ${FIRSTDIGIT} -eq 0 ]]; then #{
    		if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDAR[SECONDDIGIT-1]} ]]; then #{
    			DAY="GOOD"
    		fi #}
    	else #} {
    		if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDAR[MM-1]} ]]; then #{
    			DAY="GOOD"
    		fi #}
    	fi #}
    
    	echo ${DAY}
    }
    
    ####################
    ### END FUNCTION ###
    ####################
    
    KEEPGOING=1
    while [[ ${KEEPGOING} -eq 1 ]]; do #{
    	read -p "Enter a date for validation: " DATE
    
    	# establish the varible LEN to hold the number of characters in Date, 8 is the only valid number
    	LEN=$(echo ${#DATE})
    
    	if [[ "${DATE}" == exit ]]; then #{
    		$KEEPGOING=0
    	elif [ $LEN -eq 8 ]; then # } {
    		# set date dariables MM, DD, & YY
    		MM=${DATE:0:2}
    		DD=${DATE:2:2}
    		YY=${DATE:4:4}
    
    		if [ ${YY} -gt 0 ]; then #{
    			if [ ${MM} -gt 0 ] && [ ${MM} -lt 13 ]; then #{
    				if [ ${MM} -eq 02 ]; then #{
    					result=$(is_leap_year ${YY})
    					if [ "$result" == "true" ]; then #{
    						if [ ${DD} -gt 0 ] && [ ${DD} -le 29 ]; then #{
    							echo "${DATE} is a valid date!"
    						else #} {
    							echo "${DD} is invalid for this date!"
    						fi #}
    					else #} {
    						DAY=$(get_day ${MM} ${DD})
    						if [ "${DAY}" == "GOOD" ]; then #{
    							echo "${DATE} is a valid date!"
    						else #} {
    							echo "${DD} is invalid for this date!"
    						fi #}
    					fi #}
    				else #} {
    					DAY=$(get_day ${MM} ${DD})
    					if [ "${DAY}" == "GOOD" ]; then #{
    						echo "${DATE} is a valid date!"
    					else # } {
    						echo "${DD} is invalid for this date!"
    					fi #}
    				fi #}
    
    			else # } {
    				echo "${MM} is invalid for this date!"
    			fi #}
    
    		else # } {
    			echo "${YY} is invalid for this date!"
    		fi #}
    
    	else # } {
    		echo "Invalid number of digits for a date!"
    	fi #}
    done #}
    #END#

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    and then?

    do we have to tell all again what we already told you?

  3. #3
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122
    Sorry watael, I was just showing that the program was successfull and if anyone else out there was working on such a program this might be of help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •