Listing 1: getutctime.c
/*
getutctime.c
Print the current value of the system time counter, or if an argument
of the form hh:mm:ss:m:d:year is specified on the command line, print the
system clock value for that time. The program does not do any range
checking of a user specified date and time.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <langinfo.h>
#include <limits.h>
#include <string.h>
/* Prototypes */
void time_now();
int store_time(char *);
/* The tm struct is declared in time.h. */
struct tm usertime;
void main (int argc, char *argv[])
{
time_t user_supplied_time;
if( argc == 1)
{
time_now();
}
/* Place the time and date from the command line into the tm struct. */
if( argc == 2 )
{
store_time(argv[1]);
user_supplied_time = mktime(&usertime);
printf("%ld.\n", user_supplied_time);
}
}
int store_time(char *user_time)
{
const char fmt[18]="%d:%d:%d:%d:%d:%d";
/* put the values into the usertime struct */
sscanf(user_time, fmt,
&usertime.tm_hour,
&usertime.tm_min,
&usertime.tm_sec,
&usertime.tm_mon,
&usertime.tm_mday,
&usertime.tm_year );
/* Because mktime counts months starting with 0. */
usertime.tm_mon -= 1;
/* The counter still doesn't overflow after year 2000. */
usertime.tm_year -= 1900;
return(0);
}
void time_now()
{
time_t now;
now = time(NULL);
printf("%ld.\n", now);
}
/* End of File */
|