Operating System - HP-UX
1820254 Members
2818 Online
109622 Solutions
New Discussion юеВ

How to validate a parameter as a date

 
SOLVED
Go to solution
Admin.SIF
Frequent Advisor

How to validate a parameter as a date

I currently create a .sh script which will receive a date parameter (20001206).

How can I validate the parameter is a valid date. Is there a shell function to compare the input string with a date format.

Or, if there is none, how can I validate if the parameter is a valid number. Just do a test like "if [ $1 -lt 10000000 -o $1 -gt 99999999 ] gave me an error if the parameter contain alphanumerics (2000DEC06).

Thanks for your help.
Sysd. Amin. Inforef
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: How to validate a parameter as a date

Hi:

Given the format of your 'date' parameter, take it and concatenate "0000" to the end of it; 'touch' a file using the resultant string as a timestamp; and examine the return status. If the exit status of the 'touch' is zero, then the 'date' was valid. Thusly:

# ARG=${ARG}0000
# touch -c -m -t $ARG /tmp/$$
# if [ "$?" = 0 ]
# then
# echo "date_OK"
# else
# echo "invalid date: $ARG"
# fi

Note that the '-c' argument to 'touch' prevents creating any file that you have to cleanup.

...JRF...
Madhu Sudhan_1
Respected Contributor

Re: How to validate a parameter as a date

It is a good one James !

Thanks,
Madhu
Think Positive
Admin.SIF
Frequent Advisor

Re: How to validate a parameter as a date

Thanks! It works fine. One problem, it does not validate the day related with the month: If i enter "20000230", the date is accepted. But every one know there is no Feb. 30th.
Anyway, I'll take care of it. Thanks!
Sysd. Amin. Inforef
James R. Ferguson
Acclaimed Contributor

Re: How to validate a parameter as a date

Hi:

Ah, yes. You are correct, the 'touch' only verifies that the day of the month is between 1-and-31 inclusive. Hence a "month/day" of "0431" [also erroneous] would pass without further analysis. My method is clearly a "first approximation" of correctness.

...JRF...
Matt Livingston
Valued Contributor

Re: How to validate a parameter as a date

It occurred to me that you might be able to use 'at' to check the date, based on James' original suggestion of appending '0000' to the variable. But it seems that 'at' performs the same checks as 'touch'.



From an L1000 running 11.00...

sandbox:/>echo ":" | at -t 200104310000
warning: commands will be executed using /usr/bin/sh
job 988639200.a at Tue May 1 00:00:00 2001
sandbox:/>

That looks like a bug to me. What do you people think?
thinphony
Advisor

Re: How to validate a parameter as a date

Hi!
I think writting a little C program serving the purpose would be an efficient one than touch or at, and it's not too hard.
Right?
Regargs.

Thinphony!
Dan Hetzel
Honored Contributor

Re: How to validate a parameter as a date

Hi,

Here is a small C program, based on the getdate function.
Check 'man getdate' for further personalized input formats.

--- cut here
#include
#include

main(argc,argv)
int argc;
char **argv;
{
struct tm *checked_date;

if(argc != 2) {
fprintf(stderr,"Usage: $0 date_string\n");
exit(1);
}

if((checked_date=getdate(*++argv))==NULL){
fprintf(stderr, "Wrong date format: %s\n", *argv);
fprintf(stderr, "getdate_err = %d\n", getdate_err);
exit(1);
}
else
fprintf(stderr, "Like this date: %s\n", *argv);
}
--- cut here

Personalised format should be in a file pointed to the environment variable DATEMSK which, in your case, should contain the following 2 lines:

--- cut here
%Y%b%d
%Y%m%d
--- cut here

Formats accepted are for example:
2000DEC09
20001209

As getdate() checks for date validity, all input like april 31st or february 29 2001 will be rejected.

Modify the file pointed to by DATEMSK according to your needs if you want more input formats.
The c program is writing error to stderr, it should be quite straightforward to modify as well.

Best regards,

Dan

PS: This is a 'quick and dirty' check only, as I'm just verifying if getdate() is returning NULL or not. ;-)

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor
Solution

Re: How to validate a parameter as a date

Hi again,

More dirty than quick.... I forgot the exit(0) in case all went well.

Here is the modified source:

#include
#include

main(argc,argv)
int argc;
char **argv;
{
struct tm *checked_date;

if(argc != 2) {
fprintf(stderr,"Usage: $0 date_string\n");
exit(1);
}

if((checked_date=getdate(*++argv))==NULL){
fprintf(stderr, "Wrong date format: %s\n", *argv);
fprintf(stderr, "getdate_err = %d\n", getdate_err);
exit(1);
}
else {
fprintf(stderr, "Like this date: %s\n", *argv);
exit(0);
}
}


Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Admin.SIF
Frequent Advisor

Re: How to validate a parameter as a date

Thanks for the C program. I apply some minor changes due to the C Compiler we use.

--- Cut here ---
#include
#include

int main(int argc, char **argv)
{
struct tm *checked_date;
if(argc != 2)
{
fprintf(stderr,"\tUsage: check_date [date]\n\n");
return (1);
}
if((checked_date=getdate(*++argv))==NULL)
{
/* error: Invalid date or format */
return (1);
}
return (0);
}
--- End cut ---

Also, I remove unwanted fprintf to be able to use it into a sh script as a boolean result:

--- Cut Here ---
#!/usr/bin/sh
if [ ${#} = 1 ] ; then
if check_date ${1}
then
echo "Continue process"
else
echo "Invalide date. Stop process."
exit
fi
fi
--- End Cut ---
Sysd. Amin. Inforef