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

int main(int argc, char **argv) {
	struct timeval tv, otv;
	float secs;

	if (argc < 2 || 1 != sscanf(argv[1], "%f", &secs)) {
		printf("usage: adjtime_once <seconds> (example: adjtime_once -0.1\n");
		return 3;
	}

	tv.tv_sec = secs;
	secs -= tv.tv_sec;
	tv.tv_usec = secs * 1e6;
	if (tv.tv_usec < 0) {
		tv.tv_sec = -tv.tv_sec;
		tv.tv_sec -= 1;
		tv.tv_usec += 1000000;
	}
	printf("%g seconds %ld s %ld usec\n", secs, tv.tv_sec, tv.tv_usec);

	if (adjtime(&tv, &otv)) {
		printf("fail\n");
		return 1;
	}

	do {
		if (adjtime(NULL, &otv)) {
			printf("fail\n");
			return 1;
		}
		printf("%ld %ld\n", otv.tv_sec, otv.tv_usec);
		sleep(1);
	} while (otv.tv_usec > 0);

	printf("Slewed %g seconds\n", tv.tv_sec + tv.tv_usec / 1e6);

	return 0;
}
