Listing 7 mktime.c
/*
* mktime.c
* pass the month, day, and year; return
* the number of seconds from the Epoch
*/
#include <stdio.h>
#include <time.h>
void main(int argc, char **argv)
{
int mm, dd, yy;
struct tm rettm;
time_t tloc;
if(argc != 4)
{
printf("-1");
exit(1);
}
if ( (mm=atoi(argv[1])) == NULL)
{
printf("-2");
exit(1);
}
if ( (dd=atoi(argv[2])) == NULL)
{
printf("-3");
exit(1);
}
if ( (yy=atoi(argv[3])) == NULL)
{
printf("-4");
exit(1);
}
/* populate the tm structure with todays data */
time(&tloc);
rettm=*localtime(&tloc);
/* change values from passed parameters */
rettm.tm_year=yy-1900;
rettm.tm_mon=mm-1;
rettm.tm_mday=dd;
printf("%ld", mktime(&rettm));
exit(0);
}
|