17c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2b4203d7Marcel Telka/*	  All Rights Reserved	*/
37c478bdstevel@tonic-gate
47c478bdstevel@tonic-gate
57c478bdstevel@tonic-gate/*
639de7e4danmcd * Copyright (c) 1982, 1986, 1993 Regents of the University of California.
77c478bdstevel@tonic-gate * All rights reserved.  The Berkeley software License Agreement
87c478bdstevel@tonic-gate * specifies the terms and conditions for redistribution.
97c478bdstevel@tonic-gate */
107c478bdstevel@tonic-gate
117c478bdstevel@tonic-gate/*
12ba3594bGarrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
13ba3594bGarrett D'Amore *
14113b131Eric Saxe * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
157c478bdstevel@tonic-gate * Use is subject to license terms.
16b819ceaGordon Ross *
17b819ceaGordon Ross * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
18b7a7784Andy Fiddaman *
19b7a7784Andy Fiddaman * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate
22a8f6344Eli Rosenthal/*
23c12492cSebastien Roy * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
24a8f6344Eli Rosenthal */
25a8f6344Eli Rosenthal
267c478bdstevel@tonic-gate#ifndef _SYS_TIME_H
277c478bdstevel@tonic-gate#define	_SYS_TIME_H
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include <sys/feature_tests.h>
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/*
327c478bdstevel@tonic-gate * Structure returned by gettimeofday(2) system call,
337c478bdstevel@tonic-gate * and used in other calls.
347c478bdstevel@tonic-gate */
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#ifdef	__cplusplus
377c478bdstevel@tonic-gateextern "C" {
387c478bdstevel@tonic-gate#endif
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
417c478bdstevel@tonic-gate	defined(__EXTENSIONS__)
427c478bdstevel@tonic-gate#ifndef	_ASM
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate#if !defined(_TIME_T) || __cplusplus >= 199711L
457c478bdstevel@tonic-gate#define	_TIME_T
467c478bdstevel@tonic-gatetypedef	long	time_t;		/* time of day in seconds */
477c478bdstevel@tonic-gate#endif	/* _TIME_T */
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#ifndef	_SUSECONDS_T
507c478bdstevel@tonic-gate#define	_SUSECONDS_T
517c478bdstevel@tonic-gatetypedef	long	suseconds_t;	/* signed # of microseconds */
527c478bdstevel@tonic-gate#endif	/* _SUSECONDS_T */
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gatestruct timeval {
557c478bdstevel@tonic-gate	time_t		tv_sec;		/* seconds */
567c478bdstevel@tonic-gate	suseconds_t	tv_usec;	/* and microseconds */
577c478bdstevel@tonic-gate};
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate#if defined(_SYSCALL32)
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate#include <sys/types32.h>
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate#define	TIMEVAL32_TO_TIMEVAL(tv, tv32)	{	\
647c478bdstevel@tonic-gate	(tv)->tv_sec = (time_t)(tv32)->tv_sec;	\
657c478bdstevel@tonic-gate	(tv)->tv_usec = (tv32)->tv_usec;	\
667c478bdstevel@tonic-gate}
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate#define	TIMEVAL_TO_TIMEVAL32(tv32, tv)	{		\
697c478bdstevel@tonic-gate	(tv32)->tv_sec = (time32_t)(tv)->tv_sec;	\
70336069cPatrick Mooney	(tv32)->tv_usec = (int32_t)(tv)->tv_usec;	\
717c478bdstevel@tonic-gate}
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate#define	TIME32_MAX	INT32_MAX
747c478bdstevel@tonic-gate#define	TIME32_MIN	INT32_MIN
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate#define	TIMEVAL_OVERFLOW(tv)	\
777c478bdstevel@tonic-gate	((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
787c478bdstevel@tonic-gate
79b819ceaGordon Ross#endif	/* _SYSCALL32 */
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate#endif	/* _ASM */
827c478bdstevel@tonic-gate#endif	/* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
857c478bdstevel@tonic-gate#ifndef	_ASM
867c478bdstevel@tonic-gatestruct timezone {
877c478bdstevel@tonic-gate	int	tz_minuteswest;	/* minutes west of Greenwich */
887c478bdstevel@tonic-gate	int	tz_dsttime;	/* type of dst correction */
897c478bdstevel@tonic-gate};
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate#endif	/* _ASM */
927c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate#ifdef	__cplusplus
957c478bdstevel@tonic-gate}
967c478bdstevel@tonic-gate#endif
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate/*
997c478bdstevel@tonic-gate * Needed for longlong_t type.  Placement of this due to <sys/types.h>
1007c478bdstevel@tonic-gate * including <sys/select.h> which relies on the presense of the itimerval
1017c478bdstevel@tonic-gate * structure.
1027c478bdstevel@tonic-gate */
1037c478bdstevel@tonic-gate#ifndef	_ASM
1047c478bdstevel@tonic-gate#include <sys/types.h>
1057c478bdstevel@tonic-gate#endif	/* _ASM */
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate#ifdef	__cplusplus
1087c478bdstevel@tonic-gateextern "C" {
1097c478bdstevel@tonic-gate#endif
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate#define	DST_NONE	0	/* not on dst */
1147c478bdstevel@tonic-gate#define	DST_USA		1	/* USA style dst */
1157c478bdstevel@tonic-gate#define	DST_AUST	2	/* Australian style dst */
1167c478bdstevel@tonic-gate#define	DST_WET		3	/* Western European dst */
1177c478bdstevel@tonic-gate#define	DST_MET		4	/* Middle European dst */
1187c478bdstevel@tonic-gate#define	DST_EET		5	/* Eastern European dst */
1197c478bdstevel@tonic-gate#define	DST_CAN		6	/* Canada */
1207c478bdstevel@tonic-gate#define	DST_GB		7	/* Great Britain and Eire */
1217c478bdstevel@tonic-gate#define	DST_RUM		8	/* Rumania */
1227c478bdstevel@tonic-gate#define	DST_TUR		9	/* Turkey */
1237c478bdstevel@tonic-gate#define	DST_AUSTALT	10	/* Australian style with shift in 1986 */
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate/*
1267c478bdstevel@tonic-gate * Operations on timevals.
1277c478bdstevel@tonic-gate */
1287c478bdstevel@tonic-gate#define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
1297c478bdstevel@tonic-gate#define	timercmp(tvp, uvp, cmp) \
1307c478bdstevel@tonic-gate	(((tvp)->tv_sec == (uvp)->tv_sec) ? \
1317c478bdstevel@tonic-gate	    /* CSTYLED */ \
1327c478bdstevel@tonic-gate	    ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1337c478bdstevel@tonic-gate	    /* CSTYLED */ \
1347c478bdstevel@tonic-gate	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate#define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
1377c478bdstevel@tonic-gate
13839de7e4danmcd#ifdef __lint
13939de7e4danmcd/*
14039de7e4danmcd * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
14139de7e4danmcd * lint-safe.  If the compiler could know that we always make tv_usec < 1000000
14239de7e4danmcd * we wouldn't need a special linted version.
14339de7e4danmcd */
14439de7e4danmcd#define	timeradd(tvp, uvp, vvp)					\
14539de7e4danmcd	do								\
14639de7e4danmcd	{								\
14739de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
14839de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
14939de7e4danmcd		if ((vvp)->tv_usec >= 1000000)				\
15039de7e4danmcd		{							\
15139de7e4danmcd			(vvp)->tv_sec++;				\
15239de7e4danmcd			(vvp)->tv_usec -= 1000000;			\
15339de7e4danmcd		}							\
15439de7e4danmcd	} while ((vvp)->tv_usec >= 1000000)
15539de7e4danmcd#define	timersub(tvp, uvp, vvp)					\
15639de7e4danmcd	do								\
15739de7e4danmcd	{								\
15839de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
15939de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
16039de7e4danmcd		if ((vvp)->tv_usec < 0)					\
16139de7e4danmcd		{							\
16239de7e4danmcd			(vvp)->tv_sec--;				\
16339de7e4danmcd			(vvp)->tv_usec += 1000000;			\
16439de7e4danmcd		}							\
16539de7e4danmcd	} while ((vvp)->tv_usec >= 1000000)
16639de7e4danmcd#else
16739de7e4danmcd#define	timeradd(tvp, uvp, vvp)					\
16839de7e4danmcd	do								\
16939de7e4danmcd	{								\
17039de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
17139de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
17239de7e4danmcd		if ((vvp)->tv_usec >= 1000000)				\
17339de7e4danmcd		{							\
17439de7e4danmcd			(vvp)->tv_sec++;				\
17539de7e4danmcd			(vvp)->tv_usec -= 1000000;			\
17639de7e4danmcd		}							\
17739de7e4danmcd	} while (0)
17839de7e4danmcd
17939de7e4danmcd#define	timersub(tvp, uvp, vvp)					\
18039de7e4danmcd	do								\
18139de7e4danmcd	{								\
18239de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
18339de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
18439de7e4danmcd		if ((vvp)->tv_usec < 0)					\
18539de7e4danmcd		{							\
18639de7e4danmcd			(vvp)->tv_sec--;				\
18739de7e4danmcd			(vvp)->tv_usec += 1000000;			\
18839de7e4danmcd		}							\
18939de7e4danmcd	} while (0)
19039de7e4danmcd#endif /* __lint */
19139de7e4danmcd
1927c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
1957c478bdstevel@tonic-gate/*
1967c478bdstevel@tonic-gate * Names of the interval timers, and structure
1977c478bdstevel@tonic-gate * defining a timer setting.
1987c478bdstevel@tonic-gate */
1997c478bdstevel@tonic-gate#define	ITIMER_REAL	0	/* Decrements in real time */
2007c478bdstevel@tonic-gate#define	ITIMER_VIRTUAL	1	/* Decrements in process virtual time */
2017c478bdstevel@tonic-gate#define	ITIMER_PROF	2	/* Decrements both in process virtual */
2027c478bdstevel@tonic-gate				/* time and when system is running on */
2037c478bdstevel@tonic-gate				/* behalf of the process. */
2047c478bdstevel@tonic-gate#define	ITIMER_REALPROF	3	/* Decrements in real time for real- */
2057c478bdstevel@tonic-gate				/* time profiling of multithreaded */
2067c478bdstevel@tonic-gate				/* programs. */
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate#ifndef	_ASM
2097c478bdstevel@tonic-gatestruct	itimerval {
2107c478bdstevel@tonic-gate	struct	timeval it_interval;	/* timer interval */
2117c478bdstevel@tonic-gate	struct	timeval it_value;	/* current value */
2127c478bdstevel@tonic-gate};
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gate#if defined(_SYSCALL32)
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gatestruct itimerval32 {
2177c478bdstevel@tonic-gate	struct	timeval32 it_interval;
2187c478bdstevel@tonic-gate	struct	timeval32 it_value;
2197c478bdstevel@tonic-gate};
2207c478bdstevel@tonic-gate
2217c478bdstevel@tonic-gate#define	ITIMERVAL32_TO_ITIMERVAL(itv, itv32)	{	\
2227c478bdstevel@tonic-gate	TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
2237c478bdstevel@tonic-gate	TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value);	\
2247c478bdstevel@tonic-gate}
2257c478bdstevel@tonic-gate
2267c478bdstevel@tonic-gate#define	ITIMERVAL_TO_ITIMERVAL32(itv32, itv)	{	\
2277c478bdstevel@tonic-gate	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
2287c478bdstevel@tonic-gate	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value);	\
2297c478bdstevel@tonic-gate}
2307c478bdstevel@tonic-gate
2317c478bdstevel@tonic-gate#define	ITIMERVAL_OVERFLOW(itv)				\
2327c478bdstevel@tonic-gate	(TIMEVAL_OVERFLOW(&(itv)->it_interval) ||	\
2337c478bdstevel@tonic-gate	TIMEVAL_OVERFLOW(&(itv)->it_value))
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate#endif	/* _SYSCALL32 */
2367c478bdstevel@tonic-gate#endif	/* _ASM */
2377c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gate
2407c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
2417c478bdstevel@tonic-gate/*
2427c478bdstevel@tonic-gate *	Definitions for commonly used resolutions.
2437c478bdstevel@tonic-gate */
2447c478bdstevel@tonic-gate#define	SEC		1
2457c478bdstevel@tonic-gate#define	MILLISEC	1000
2467c478bdstevel@tonic-gate#define	MICROSEC	1000000
247b59e212Josef 'Jeff' Sipek#define	NANOSEC		1000000000LL
2487c478bdstevel@tonic-gate
2490689f76Adam Leventhal#define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
2500689f76Adam Leventhal#define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
2510689f76Adam Leventhal
252c12492cSebastien Roy#define	USEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MICROSEC))
253c12492cSebastien Roy#define	NSEC2USEC(n)	((n) / (NANOSEC / MICROSEC))
2541271e4bPrakash Surya
255a8f6344Eli Rosenthal#define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
256a8f6344Eli Rosenthal#define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
257a8f6344Eli Rosenthal
2587c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
2597c478bdstevel@tonic-gate
2607c478bdstevel@tonic-gate#ifndef	_ASM
2617c478bdstevel@tonic-gate
2627c478bdstevel@tonic-gate/*
2637c478bdstevel@tonic-gate * Time expressed as a 64-bit nanosecond counter.
2647c478bdstevel@tonic-gate */
2657c478bdstevel@tonic-gatetypedef	longlong_t	hrtime_t;
2667c478bdstevel@tonic-gate
267b819ceaGordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate#include <sys/time_impl.h>
2707c478bdstevel@tonic-gate#include <sys/mutex.h>
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gateextern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
2737c478bdstevel@tonic-gateextern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
2747c478bdstevel@tonic-gateextern int usec_per_tick;	/* microseconds per clock tick */
2757c478bdstevel@tonic-gateextern int nsec_per_tick;	/* nanoseconds per clock tick */
2767c478bdstevel@tonic-gate
2777c478bdstevel@tonic-gate/*
2787c478bdstevel@tonic-gate * Macros to convert from common units of time (sec, msec, usec, nsec,
2797c478bdstevel@tonic-gate * timeval, timestruc) to clock ticks and vice versa.
2807c478bdstevel@tonic-gate */
2817c478bdstevel@tonic-gate#define	TICK_TO_SEC(tick)	((tick) / hz)
2827c478bdstevel@tonic-gate#define	SEC_TO_TICK(sec)	((sec) * hz)
2837c478bdstevel@tonic-gate
2847c478bdstevel@tonic-gate#define	TICK_TO_MSEC(tick)	\
2857c478bdstevel@tonic-gate	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
2867c478bdstevel@tonic-gate#define	MSEC_TO_TICK(msec)	\
2877c478bdstevel@tonic-gate	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
2887c478bdstevel@tonic-gate#define	MSEC_TO_TICK_ROUNDUP(msec)	\
2897c478bdstevel@tonic-gate	(msec_per_tick ? \
2907c478bdstevel@tonic-gate	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
2917c478bdstevel@tonic-gate	(msec) * tick_per_msec)
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gate#define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
2947c478bdstevel@tonic-gate#define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
2957c478bdstevel@tonic-gate#define	USEC_TO_TICK_ROUNDUP(usec)	\
2967c478bdstevel@tonic-gate	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
2977c478bdstevel@tonic-gate
29887a18d3Madhavan Venkataraman#define	TICK_TO_NSEC(tick)		((hrtime_t)(tick) * nsec_per_tick)
2997c478bdstevel@tonic-gate#define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
3007c478bdstevel@tonic-gate#define	NSEC_TO_TICK_ROUNDUP(nsec)	\
3017c478bdstevel@tonic-gate	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate#define	TICK_TO_TIMEVAL(tick, tvp) {	\
3047c478bdstevel@tonic-gate	clock_t __tmptck = (tick);	\
3057c478bdstevel@tonic-gate	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
3067c478bdstevel@tonic-gate	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
3077c478bdstevel@tonic-gate}
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gate#define	TICK_TO_TIMEVAL32(tick, tvp) {	\
3107c478bdstevel@tonic-gate	clock_t __tmptck = (tick);	\
3117c478bdstevel@tonic-gate	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
3127c478bdstevel@tonic-gate	(tvp)->tv_sec = (time32_t)__tmptm;	\
3137c478bdstevel@tonic-gate	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
3147c478bdstevel@tonic-gate}
3157c478bdstevel@tonic-gate
3167c478bdstevel@tonic-gate#define	TICK_TO_TIMESTRUC(tick, tsp) {	\
3177c478bdstevel@tonic-gate	clock_t __tmptck = (tick);	\
3187c478bdstevel@tonic-gate	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
3197c478bdstevel@tonic-gate	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
3207c478bdstevel@tonic-gate}
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate#define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
3237c478bdstevel@tonic-gate	clock_t __tmptck = (tick);			\
3247c478bdstevel@tonic-gate	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
3257c478bdstevel@tonic-gate	(tsp)->tv_sec = (time32_t)__tmptm;		\
3267c478bdstevel@tonic-gate	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
3277c478bdstevel@tonic-gate}
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate#define	TIMEVAL_TO_TICK(tvp)	\
3307c478bdstevel@tonic-gate	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
3317c478bdstevel@tonic-gate
3327c478bdstevel@tonic-gate#define	TIMESTRUC_TO_TICK(tsp)	\
3337c478bdstevel@tonic-gate	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
3347c478bdstevel@tonic-gate
3357c478bdstevel@tonic-gatetypedef struct todinfo {
3367c478bdstevel@tonic-gate	int	tod_sec;	/* seconds 0-59 */
3377c478bdstevel@tonic-gate	int	tod_min;	/* minutes 0-59 */
3387c478bdstevel@tonic-gate	int	tod_hour;	/* hours 0-23 */
3397c478bdstevel@tonic-gate	int	tod_dow;	/* day of week 1-7 */
3407c478bdstevel@tonic-gate	int	tod_day;	/* day of month 1-31 */
3417c478bdstevel@tonic-gate	int	tod_month;	/* month 1-12 */
3427c478bdstevel@tonic-gate	int	tod_year;	/* year 70+ */
3437c478bdstevel@tonic-gate} todinfo_t;
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gateextern	int64_t		timedelta;
3463348528dmextern	int		timechanged;
3477c478bdstevel@tonic-gateextern	int		tod_needsync;
3487c478bdstevel@tonic-gateextern	kmutex_t	tod_lock;
349b5b48ccsudheerextern	volatile timestruc_t	hrestime;
3507c478bdstevel@tonic-gateextern	hrtime_t	hres_last_tick;
3517c478bdstevel@tonic-gateextern	int64_t		hrestime_adj;
3527c478bdstevel@tonic-gateextern	uint_t		adj_shift;
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gateextern	timestruc_t	tod_get(void);
3557c478bdstevel@tonic-gateextern	void		tod_set(timestruc_t);
3567c478bdstevel@tonic-gateextern	void		set_hrestime(timestruc_t *);
3577c478bdstevel@tonic-gateextern	todinfo_t	utc_to_tod(time_t);
3587c478bdstevel@tonic-gateextern	time_t		tod_to_utc(todinfo_t);
3597c478bdstevel@tonic-gateextern	int		hr_clock_lock(void);
3607c478bdstevel@tonic-gateextern	void		hr_clock_unlock(int);
361b7a7784Andy Fiddamanextern	hrtime_t	gethrtime(void);
362b7a7784Andy Fiddamanextern	hrtime_t	gethrtime_unscaled(void);
3637c478bdstevel@tonic-gateextern	hrtime_t	gethrtime_max(void);
3647c478bdstevel@tonic-gateextern	hrtime_t	gethrtime_waitfree(void);
3657c478bdstevel@tonic-gateextern	void		scalehrtime(hrtime_t *);
366113b131Eric Saxeextern	uint64_t	unscalehrtime(hrtime_t);
367b7a7784Andy Fiddamanextern	void		gethrestime(timespec_t *);
368b7a7784Andy Fiddamanextern	time_t		gethrestime_sec(void);
3697c478bdstevel@tonic-gateextern	void		gethrestime_lasttick(timespec_t *);
3707c478bdstevel@tonic-gateextern	void		hrt2ts(hrtime_t, timestruc_t *);
3717c478bdstevel@tonic-gateextern	hrtime_t	ts2hrt(const timestruc_t *);
3727c478bdstevel@tonic-gateextern	void		hrt2tv(hrtime_t, struct timeval *);
3737c478bdstevel@tonic-gateextern	hrtime_t	tv2hrt(struct timeval *);
3747c478bdstevel@tonic-gateextern	int		itimerfix(struct timeval *, int);
3757c478bdstevel@tonic-gateextern	int		itimerdecr(struct itimerval *, int);
3767c478bdstevel@tonic-gateextern	void		timevaladd(struct timeval *, struct timeval *);
3777c478bdstevel@tonic-gateextern	void		timevalsub(struct timeval *, struct timeval *);
3787c478bdstevel@tonic-gateextern	void		timevalfix(struct timeval *);
3797c478bdstevel@tonic-gateextern	void		dtrace_hres_tick(void);
3807c478bdstevel@tonic-gate
381d3d5073Rafael Vanoniextern clock_t		ddi_get_lbolt(void);
382d3d5073Rafael Vanoniextern int64_t		ddi_get_lbolt64(void);
383d3d5073Rafael Vanoni
3847c478bdstevel@tonic-gate#if defined(_SYSCALL32)
3857c478bdstevel@tonic-gateextern	void		hrt2ts32(hrtime_t, timestruc32_t *);
3867c478bdstevel@tonic-gate#endif
3877c478bdstevel@tonic-gate
3887c478bdstevel@tonic-gate#endif /* _KERNEL */
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
3917c478bdstevel@tonic-gateint adjtime(struct timeval *, struct timeval *);
3927c478bdstevel@tonic-gate#endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
3937c478bdstevel@tonic-gate
3947c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
3957c478bdstevel@tonic-gate	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
3967c478bdstevel@tonic-gateint futimesat(int, const char *, const struct timeval *);
3977c478bdstevel@tonic-gate#endif /* defined(__ATFILE_SOURCE) */
3987c478bdstevel@tonic-gate
3997c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
4007c478bdstevel@tonic-gate	defined(__EXTENSIONS__)
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gateint getitimer(int, struct itimerval *);
4037c478bdstevel@tonic-gateint utimes(const char *, const struct timeval *);
404b7a7784Andy Fiddaman
4057c478bdstevel@tonic-gate#if defined(_XPG4_2)
4067c478bdstevel@tonic-gateint setitimer(int, const struct itimerval *_RESTRICT_KYWD,
4077c478bdstevel@tonic-gate	struct itimerval *_RESTRICT_KYWD);
4087c478bdstevel@tonic-gate#else
4097c478bdstevel@tonic-gateint setitimer(int, struct itimerval *_RESTRICT_KYWD,
4107c478bdstevel@tonic-gate	struct itimerval *_RESTRICT_KYWD);
4117c478bdstevel@tonic-gate#endif /* defined(_XPG2_2) */
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gate#endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
4147c478bdstevel@tonic-gate
415b7a7784Andy Fiddaman#if !defined(_KERNEL) && !defined(_STRICT_SYMBOLS)
416b7a7784Andy Fiddamanint futimes(int, const struct timeval *);
417b7a7784Andy Fiddamanint lutimes(const char *, const struct timeval *);
418b7a7784Andy Fiddaman
419b7a7784Andy Fiddaman#define	TIMESPEC_TO_TIMEVAL(tv, ts) { \
420b7a7784Andy Fiddaman	(tv)->tv_sec = (ts)->tv_sec; \
421b7a7784Andy Fiddaman	(tv)->tv_usec = (ts)->tv_nsec / 1000; \
422b7a7784Andy Fiddaman}
423b7a7784Andy Fiddaman
424b7a7784Andy Fiddaman#define	TIMEVAL_TO_TIMESPEC(tv, ts) { \
425b7a7784Andy Fiddaman	(ts)->tv_sec = (tv)->tv_sec; \
426b7a7784Andy Fiddaman	(ts)->tv_nsec = (tv)->tv_usec * 1000; \
427b7a7784Andy Fiddaman}
428b7a7784Andy Fiddaman
429b7a7784Andy Fiddaman#endif /* !defined(_KERNEL) && !defined(_STRICT_SYMBOLS) */
430b7a7784Andy Fiddaman
4317c478bdstevel@tonic-gate/*
4327c478bdstevel@tonic-gate * gettimeofday() and settimeofday() were included in SVr4 due to their
4337c478bdstevel@tonic-gate * common use in BSD based applications.  They were to be included exactly
4347c478bdstevel@tonic-gate * as in BSD, with two parameters.  However, AT&T/USL noted that the second
4357c478bdstevel@tonic-gate * parameter was unused and deleted it, thereby making a routine included
4367c478bdstevel@tonic-gate * for compatibility, incompatible.
4377c478bdstevel@tonic-gate *
4387c478bdstevel@tonic-gate * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
4397c478bdstevel@tonic-gate * parameters.
4407c478bdstevel@tonic-gate *
4417c478bdstevel@tonic-gate * This has caused general disagreement in the application community as to
4427c478bdstevel@tonic-gate * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
4437c478bdstevel@tonic-gate * The flag _SVID_GETTOD may be used to force the SVID version.
4447c478bdstevel@tonic-gate */
4457c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gate#if defined(_SVID_GETTOD)
4487c478bdstevel@tonic-gateint settimeofday(struct timeval *);
4497c478bdstevel@tonic-gate#else
4507c478bdstevel@tonic-gateint settimeofday(struct timeval *, void *);
4517c478bdstevel@tonic-gate#endif
4527c478bdstevel@tonic-gatehrtime_t	gethrtime(void);
4537c478bdstevel@tonic-gatehrtime_t	gethrvtime(void);
4547c478bdstevel@tonic-gate
4557c478bdstevel@tonic-gate#endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
4587c478bdstevel@tonic-gate	defined(__EXTENSIONS__)
4597c478bdstevel@tonic-gate
4607c478bdstevel@tonic-gate#if defined(_SVID_GETTOD)
4617c478bdstevel@tonic-gateint gettimeofday(struct timeval *);
4627c478bdstevel@tonic-gate#else
4637c478bdstevel@tonic-gateint gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
4647c478bdstevel@tonic-gate#endif
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate#endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate/*
4697c478bdstevel@tonic-gate * The inclusion of <time.h> is historical and was added for
4707c478bdstevel@tonic-gate * backward compatibility in delta 1.2 when a number of definitions
4717c478bdstevel@tonic-gate * were moved out of <sys/time.h>.  More recently, the timespec and
4727c478bdstevel@tonic-gate * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
4737c478bdstevel@tonic-gate * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
4747c478bdstevel@tonic-gate * which is now included by <time.h>.  This change was due to POSIX
4757c478bdstevel@tonic-gate * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
4767c478bdstevel@tonic-gate * non-X/Open applications, including this header will still make
4777c478bdstevel@tonic-gate * visible these definitions.
4787c478bdstevel@tonic-gate */
479b819ceaGordon Ross#if !defined(_BOOT) && !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
480ae115bcmrj	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
4817c478bdstevel@tonic-gate#include <time.h>
4827c478bdstevel@tonic-gate#endif
4837c478bdstevel@tonic-gate
4847c478bdstevel@tonic-gate/*
4857c478bdstevel@tonic-gate * The inclusion of <sys/select.h> is needed for the FD_CLR,
4867c478bdstevel@tonic-gate * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
4877c478bdstevel@tonic-gate * select() prototype defined in the XOpen specifications
4887c478bdstevel@tonic-gate * beginning with XSH4v2.  Placement required after definition
4897c478bdstevel@tonic-gate * for itimerval.
4907c478bdstevel@tonic-gate */
491b819ceaGordon Ross#if !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
492b819ceaGordon Ross	!defined(__XOPEN_OR_POSIX) || \
493b819ceaGordon Ross	defined(_XPG4_2) || defined(__EXTENSIONS__)
4947c478bdstevel@tonic-gate#include <sys/select.h>
4957c478bdstevel@tonic-gate#endif
4967c478bdstevel@tonic-gate
4977c478bdstevel@tonic-gate#endif	/* _ASM */
4987c478bdstevel@tonic-gate
4997c478bdstevel@tonic-gate#ifdef	__cplusplus
5007c478bdstevel@tonic-gate}
5017c478bdstevel@tonic-gate#endif
5027c478bdstevel@tonic-gate
5037c478bdstevel@tonic-gate#endif	/* _SYS_TIME_H */
504