int main(
int argc,
char *argv[])
{
int retval = 0;
char systemTime[32];
time_t timep;
struct tm *p;
struct timeval tv1, tv2;
while((retval = getopt(argc, argv, "h")) != -1)
{
switch(retval)
{
case '?':
case 'h':
default:
printf("Time program.\n\n");
printf("Usage: ./time\n\n");
return 0;
}
}
time(&timep);
p = gmtime(&timep);
printf("UTC Time: %d/%d/%d %d:%d:%d\r\n", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
p = localtime(&timep);
printf("Local Time: %d/%d/%d %d:%d:%d\r\n", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
memset(&tv1, 0, sizeof(tv1));
gettimeofday(&tv1 , NULL);
sleep(3);
memset(&tv2, 0, sizeof(tv2));
gettimeofday(&tv2 , NULL);
dSecond = tv2.tv_sec - tv1.tv_sec;
dMicroSecond = tv2.tv_usec - tv1.tv_usec;
printf("Seconds = %d\r\n", dSecond + (dMicroSecond / (1000 * 1000)));
tv2.tv_sec -= 60 * 60 * 24;
settimeofday(&tv2, NULL);
system("date");
memset(systemTime, 0, sizeof(systemTime));
sprintf(systemTime, "date -s %d.%d.%d-%d:%d:%d", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
system(systemTime);
system("date");
return 0;
}