> -----Original Message-----
> From: Suhen Pather [mailto:[EMAIL PROTECTED]]
>
> Thanks for the reply.
> I am passing a hardcoded date.
> I am not getting a date from the
> "date" command in ksh.
>
> What my ksh does is retrieves $1 and must check if the date format
> is in DD-MON-YY eg. 02-FEB-01.
> It uses the hardcoded date (passed in as argument 1) in the script.
> If the date is in another format other than above then it must exit.
I dusted off my old C programming skills (Sorry Mr. Still I still don't know PERL that well). Why not write a "check_date" program that returns 0 for a valid date and 1 for an invalid date?
The date verification function is case-sensitive and only checks for the one format. It also assumes that two-digit years will be prefixed with "20". Modifying it to be more user-friendly is left as an exercise to the alert reader.
/*
* verify that date passed to function is a correct date
* parameter passed to function must be in the same format
* as shell command <<date +"%d-%b-%y">>
* example: check_date 26-Jan-01
* echo $?
* check_date 32-Jan-01
* echo $?
*
* program assumes that %b date format will return a string with 3 characters
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define B_DTFMT_LEN 3
#define DT_LEN 6 + B_DTFMT_LEN
#define HIGH_MM 11
#define CBASE_YEAR 1900
#define BASE_YEAR 2000
extern int main (int argc, char **argv)
{
short int mm ;
struct tm tmi ;
time_t t ;
char month[B_DTFMT_LEN + 1] = { '\0' },
day[3] = { '\0' },
compmonth[B_DTFMT_LEN + 1] = { '\0' },
compdate[DT_LEN + 1] = { '\0' } ;
if (argc != 2)
{
puts ("Usage - check_date \"date\" (date in %d-%b-%y format)") ;
return EXIT_SUCCESS ;
}
if (strlen (argv[1]) != DT_LEN || argv[1][2] != '-'
|| argv[1][3 + B_DTFMT_LEN] != '-'
|| ! isdigit (argv[1][0]) || ! isdigit (argv[1][1])
|| ! isdigit (argv[1][4 + B_DTFMT_LEN])
|| ! isdigit (argv[1][5 + B_DTFMT_LEN]) )
return EXIT_FAILURE ;
strncpy (month, argv[1] + 3, B_DTFMT_LEN) ;
memset (&tmi, '\0', sizeof tmi) ;
mm = -1 ;
while (strncmp (month, compmonth, B_DTFMT_LEN) && ++mm <= HIGH_MM)
{
tmi.tm_mday = 1 ;
tmi.tm_mon = mm ;
tmi.tm_year = 2001 ;
strftime (compmonth, B_DTFMT_LEN + 1, "%b", &tmi) ;
}
if (mm > HIGH_MM)
return EXIT_FAILURE ;
strncpy (day, argv[1], 2) ;
tmi.tm_mday = atoi (day) ;
tmi.tm_mon = mm ;
tmi.tm_year = BASE_YEAR + atoi (argv[1] + 4 + B_DTFMT_LEN) - CBASE_YEAR ;
if ((t = mktime (&tmi)) == (time_t) -1)
return EXIT_FAILURE ;
tmi = *localtime (&t) ;
strftime (compdate, DT_LEN + 1, "%d-%b-%y", &tmi) ;
if (strcmp (argv[1], compdate))
return EXIT_FAILURE ;
return EXIT_SUCCESS ;
}