gettimeofday and localtime millisecond timestamps example


This is a handy example that shows how to use the gettimeofday and localtime functions to calculate elapsed time in milliseconds.


gettimeofday and localtime usage example

We will be use the gettimeofday and localtime function to store a time-stamp in milliseconds at the beginning of the code block then another time-stamp at the end. The elapsed time in milliseconds is the difference between the two time-stamps.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
 
uint32_t         stampstart();
uint32_t         stampstop(uint32_t start);
 
int
main()
{
 
	uint32_t         start, stop;
 
	start = stampstart();
 
	/* Your code goes here */
 
	stop = stampstop(start);
 
	return 0;
 
}
 
uint32_t
stampstart()
{
	struct timeval  tv;
	struct timezone tz;
	struct tm      *tm;
	uint32_t         start;
 
	gettimeofday(&tv, &tz);
	tm = localtime(&tv.tv_sec);
 
	printf("TIMESTAMP-START\t  %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour,
	       tm->tm_min, tm->tm_sec, tv.tv_usec,
	       tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
	       tm->tm_sec * 1000 + tv.tv_usec / 1000);
 
	start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
		tm->tm_sec * 1000 + tv.tv_usec / 1000;
 
	return (start);
 
}
 
uint32_t
stampstop(uint32_t start)
{
 
	struct timeval  tv;
	struct timezone tz;
	struct tm      *tm;
	uint32_t         stop;
 
	gettimeofday(&tv, &tz);
	tm = localtime(&tv.tv_sec);
 
	stop = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
		tm->tm_sec * 1000 + tv.tv_usec / 1000;
 
	printf("TIMESTAMP-END\t  %d:%02d:%02d:%d (~%d ms) \n", tm->tm_hour,
	       tm->tm_min, tm->tm_sec, tv.tv_usec,
	       tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 +
	       tm->tm_sec * 1000 + tv.tv_usec / 1000);
 
	printf("ELAPSED\t  %d ms\n", stop - start);
 
	return (stop);
 
}

Details


From the manpage for gettimeofday

SYNOPSIS

       #include <sys/time.h>

       int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

DESCRIPTION

       The  gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since the Epoch, and store it in the timeval structure pointed to by tp. The reso-
       lution of the system clock is unspecified.

       struct timeval {
             long    tv_sec;         /* seconds since Jan. 1, 1970 */
             long    tv_usec;        /* and microseconds */
       };

From the manpage for localtime

SYNOPSIS

       #include <time.h>

       struct tm *        localtime(const time_t *clock);

DESCRIPTION

       The localtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as a local time. The  function  corrects  for  the
       timezone and any seasonal time adjustments.

  The structure (of type) struct tm includes the following fields:

           int tm_sec;     /* seconds after the minute [0,61] */
           int tm_min;     /* minutes after the hour [0,59] */
           int tm_hour;    /* hours since midnight [0,23] */
           int tm_mday;    /* day of the month [1,31] */
           int tm_mon;     /* months since January [0,11] */
           int tm_year;    /* years since 1900 */
           int tm_wday;    /* day of week [0,6] (Sunday = 0) */
           int tm_yday;    /* day of year [0,365] (Jan 1 = 0) */
           int tm_isdst;   /* daylight savings flag */
           long tm_gmtoff; /* offset from UTC in seconds */
           char *tm_zone;  /* abbreviation of timezone name */
AD HERE
Labels: , , Wireless Internet Security Coding Network Monitoring


Wireless Internet Security Performance RADIUS server Wireless Internet Security Performance RADIUS server