/*
 *      based on gettimeofday02.c  from LTP
 * DESCRIPTION
 *      Check if gettimeofday is monotonous
 *
 * ALGORITHM
 *      Call gettimeofday() to get a t1 (fist value)
 *      call it again to get t2, see if t2 < t1, set t2 = t1, repeat 
 */

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

int main(int ac, char **av)
{
	struct timeval tv1, tv2;
	int c = 0, h = 0;

	gettimeofday(&tv1, NULL);
	while (1) {
		gettimeofday(&tv2, NULL);
		if ((tv2.tv_usec < tv1.tv_usec) && (tv2.tv_sec <= tv1.tv_sec)) {
			printf("Run %d - hit %d:\n"
			       "  old: %10i %10i\n"
			       "  new: %10i %10i\n"
			       "  dif: %10i %10i\n", c, h++,
			       (int)tv1.tv_sec, (int)tv1.tv_usec,
			       (int)tv2.tv_sec, (int)tv2.tv_usec,
			       (int)(tv1.tv_sec - tv2.tv_sec),
			       (int)(tv1.tv_usec - tv2.tv_usec));
		}
		c++;
		/*if (c == 1000 * 1000) return 1;*/
		tv1 = tv2;
	}
	return 0;
}

