LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2013, 08:32 AM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
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#

Last edited by Garrett85; 01-11-2013 at 08:35 AM.
 
Old 01-11-2013, 10:51 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Don't forget that the century must be equally divisible by 400; i.e., 1900 was not a leap year, 2000 was.

Hope this helps some.
 
Old 01-11-2013, 01:33 PM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
tronayne

I though I had accounted for that in my condition. It's on the right side of the or condition.

Quote:
Originally Posted by tronayne View Post
Don't forget that the century must be equally divisible by 400; i.e., 1900 was not a leap year, 2000 was.

Hope this helps some.
 
Old 01-11-2013, 03:24 PM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Oops! Got caught up in expr's. Sorry about that. For a 100% shell program, pretty darned slick.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help for date validation tucs_123 Linux - Newbie 2 02-20-2009 02:14 AM
Date Validation in java using Patterns Manashi Programming 2 07-31-2006 07:19 AM
simple date validation in PHP tooparam Programming 1 04-08-2006 01:50 PM
JavaScript inside a CGI for date validation....doesn't work!! vous Programming 3 08-27-2003 04:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration