validate timestamp

How to validate the user supplied timestamp?

Ther requirement is as follows.
While invoking the script, the parameters passed to the script are minimum and maixmum timestamps.
let's say min_tstmp and max_tstmp

ksh abc.ksh 2011-09-01-00:00:00 2011-12-01-00:00:00

so min_tstmp=2011-09-01-00:00:00
and max_tstmp=2011-12-01-00:00:00

now i need to validate the min_tstmp and max_tstmp whether it is a valid timestamp or not.
If min_tstmp is valid display "min_tstmp is valid" else "min_tstmp is invalid timestamp".
If max_tstmp is valid display "max_tstmp is valid" else "max_tstmp is invalid timestamp".

I know that it is bit difficult to achieve to validate this using shell script.

But it is possible using perl script.

can anyone help me out with the perl script to validate these two timestamps are valid or not.

Thanks
Krishnakanth

What's your system?

Validate based on what criterion?

If you have GNU date (supports -d)

This will do it:

if date -d "$(echo $1 | sed 's/-//;s/-//;s/-/ /')" > /dev/null 2>&1
then
   echo min_tstmp is valid
else
   echo min_tstmp is invalid timestamp
fi
 
if date -d "$(echo $2 | sed 's/-//;s/-//;s/-/ /')" > /dev/null 2>&1
then
   echo max_tstmp is valid
else
   echo max_tstmp is invalid timestamp
fi

or

D1=$(date -d "$(echo $1 | sed 's/-//;s/-//;s/-/ /')" +%s 2> /dev/null)
D2=$(date -d "$(echo $2 | sed 's/-//;s/-//;s/-/ /')" +%s 2> /dev/null)
if [ -n "$D1" -a -n "$D2" -a ${D1:-0} -gt ${D2:-0} ]
then
   echo "min_tstmp is after max_tstmp"
else
   [ -n "$D1" ] && echo min_tstmp is valid || echo min_tstmp is invalid timestamp 
   [ -n "$D2" ] && echo max_tstmp is valid || echo max_tstmp is invalid timestamp
fi