SWAT /

Time Of Day

Reading

Outdoors

Games

Hobbies

LEGO

Food

Code

Events

Nook

sidebar

Time Of Day

Time of Day

There's nothing fancy here, just some comments on getting the time with reasonable resolution. Fancier techniques are required to go lower than microsecond resolution, many of which are not available on older Linux kernels.

/*----------------------------------------------------------------------
 * show.c --- How to fetch time splits at microseconds resolution.
 * Authors: Joe Kopena, November 2007
 ----------------------------------------------------------------------*/

#include <stdio.h>
#include <sys/time.h>

int main(int argc, char *argv[]) {

 struct timeval currentTime;

 // Use gettimeofday() to fetch time since epoch at microseconds
 // resolution.  Nanoseconds resolution unlikely to be supported on
 // PDAs, so don't bother.
 gettimeofday(&currentTime, NULL);

 // Convert to double in case that's useful.  Note that this may be
 // costly on PDAs, which may not have floating point support, etc.
 double currTime = currentTime.tv_sec +
   ((double) currentTime.tv_usec / (double) 1000000);

 printf("%f  [%d.%06d]\n",
        currTime,             // Seconds since epoch in double format
        currentTime.tv_sec,   // Seconds component of time since epoch
        currentTime.tv_usec); // Microseconds component of time since epoch

 // end main
}
Recent Changes (All) | Edit SideBar Page last modified on December 22, 2006, at 11:11 AM Edit Page | Page History