| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <sys/time.h> |
| 9 |
#include <time.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
void print_time () |
| 13 |
{ |
| 14 |
struct timeval tv; |
| 15 |
struct tm* ptm; |
| 16 |
char time_string[40]; |
| 17 |
long milliseconds; |
| 18 |
|
| 19 |
/* Obtain the time of day, and convert it to a tm struct. */ |
| 20 |
gettimeofday (&tv, NULL); |
| 21 |
ptm = localtime (&tv.tv_sec); |
| 22 |
/* Format the date and time, down to a single second. */ |
| 23 |
strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm); |
| 24 |
/* Compute milliseconds from microseconds. */ |
| 25 |
milliseconds = tv.tv_usec / 1000; |
| 26 |
/* Print the formatted time, in seconds, followed by a decimal point |
| 27 |
and the milliseconds. */ |
| 28 |
printf ("%s.%03ld\n", time_string, milliseconds); |
| 29 |
} |