hdr	time
lib	clock_settime,gettimeofday,settimeofday,stime,utimes
lib	nanosleep,usleep
lib	utimensat sys/stat.h note{ complete utimensat implementation }end link{
	#include <fcntl.h>
	static struct timespec	ts[2];
	int
	main(void)
	{
		ts[0].tv_nsec = UTIME_NOW;
		ts[1].tv_nsec = UTIME_OMIT;
		return utimensat(AT_FDCWD, ".", ts, AT_SYMLINK_NOFOLLOW) != 0;
	}
}end

if ! mem timeval.tv_sec sys/time.h {
	struct timeval
	{
		time_t	tv_sec;
		time_t	tv_usec;
	};
}
endif

lib	clock_gettime execute{
	#include <time.h>
	int
	main(void)
	{
		struct timespec	tv;
		return clock_gettime(CLOCK_REALTIME, &tv) != 0;
	}
}end

lib	utimets link{
	#include <time.h>
	#include <sys/time.h>
	static struct timespec	tv;
	int
	main(void)
	{
		return utimets(".", &tv) != 0;
	}
}end

tst	prefer_poll note{ is select less precise than 1 ms }end execute{
	#include <sys/types.h>
	#include <sys/time.h>
	#if _sys_select
	# include <sys/select.h>
	#else
	# include <sys/socket.h>
	#endif
	int
	main(void)
	{
		struct timeval tvSleep = { 0, 1 }, tvBefore, tvAfter;
		int i;
		/* Take a few measurements to prevent a fluke. */
		for (i = 0; i < 5; ++i)
		{
			gettimeofday(&tvBefore, NULL);
			select(0, NULL, NULL, NULL, &tvSleep);
			gettimeofday(&tvAfter, NULL);
			if (tvAfter.tv_sec == tvBefore.tv_sec + 1)
			{
				--tvAfter.tv_sec;
				tvAfter.tv_usec += 1000000;
			}
			if ((tvAfter.tv_sec == tvBefore.tv_sec) &&
				tvAfter.tv_usec - tvBefore.tv_usec < 1000)
				return -1;
		}
		return 0;
	}
}end

tst	- -DN=1 - -DN=2 - -DN=3 - -DN=4 output{
	#include <sys/types.h>
	#include <sys/time.h>
	int
	main(void)
	{
		struct timeval	tv;
#if N == 1
		struct timezone	tz;
		if (gettimeofday(&tv, NULL) < 0)
			return 1;
		printf("#define tmgettimeofday(p)	gettimeofday(p,NULL)\n");
#if _lib_settimeofday
		printf("#define tmsettimeofday(p)	settimeofday(p,NULL)\n");
#endif
#endif
#if N == 2
		if (gettimeofday(&tv, NULL) < 0)
			return 1;
		printf("#define tmgettimeofday(p)	gettimeofday(p,NULL)\n");
#if _lib_settimeofday
		printf("#define tmsettimeofday(p)	gettimeofday(p,NULL)\n");
#endif
#endif
#if N == 3
		if (gettimeofday(&tv) < 0)
			return 1;
		printf("#define tmgettimeofday(p)	gettimeofday(p)\n");
#if _lib_settimeofday
		printf("#define tmsettimeofday(p)	settimeofday(p)\n");
#endif
#endif
		return 0;
	}
}end
