xref: /netbsd/sys/sys/time.h (revision bf9ec67e)
1 /*	$NetBSD: time.h,v 1.34 2002/01/31 00:13:08 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)time.h	8.5 (Berkeley) 5/4/95
36  */
37 
38 #ifndef _SYS_TIME_H_
39 #define	_SYS_TIME_H_
40 
41 #include <sys/types.h>
42 
43 /*
44  * Structure returned by gettimeofday(2) system call,
45  * and used in other calls.
46  */
47 struct timeval {
48 	long	tv_sec;		/* seconds */
49 	long	tv_usec;	/* and microseconds */
50 };
51 
52 /*
53  * Structure defined by POSIX.1b to be like a timeval.
54  */
55 struct timespec {
56 	time_t	tv_sec;		/* seconds */
57 	long	tv_nsec;	/* and nanoseconds */
58 };
59 
60 #define	TIMEVAL_TO_TIMESPEC(tv, ts) {					\
61 	(ts)->tv_sec = (tv)->tv_sec;					\
62 	(ts)->tv_nsec = (tv)->tv_usec * 1000;				\
63 }
64 #define	TIMESPEC_TO_TIMEVAL(tv, ts) {					\
65 	(tv)->tv_sec = (ts)->tv_sec;					\
66 	(tv)->tv_usec = (ts)->tv_nsec / 1000;				\
67 }
68 
69 /*
70  * Note: timezone is obsolete. All timezone handling is now in
71  * userland. Its just here for back compatibility.
72  */
73 struct timezone {
74 	int	tz_minuteswest;	/* minutes west of Greenwich */
75 	int	tz_dsttime;	/* type of dst correction */
76 };
77 
78 /* Operations on timevals. */
79 #define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
80 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
81 #define	timercmp(tvp, uvp, cmp)						\
82 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
83 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
84 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
85 #define	timeradd(tvp, uvp, vvp)						\
86 	do {								\
87 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
88 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
89 		if ((vvp)->tv_usec >= 1000000) {			\
90 			(vvp)->tv_sec++;				\
91 			(vvp)->tv_usec -= 1000000;			\
92 		}							\
93 	} while (/* CONSTCOND */ 0)
94 #define	timersub(tvp, uvp, vvp)						\
95 	do {								\
96 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
97 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
98 		if ((vvp)->tv_usec < 0) {				\
99 			(vvp)->tv_sec--;				\
100 			(vvp)->tv_usec += 1000000;			\
101 		}							\
102 	} while (/* CONSTCOND */ 0)
103 
104 /* Operations on timespecs. */
105 #define	timespecclear(tsp)		(tsp)->tv_sec = (tsp)->tv_nsec = 0
106 #define	timespecisset(tsp)		((tsp)->tv_sec || (tsp)->tv_nsec)
107 #define	timespeccmp(tsp, usp, cmp)					\
108 	(((tsp)->tv_sec == (usp)->tv_sec) ?				\
109 	    ((tsp)->tv_nsec cmp (usp)->tv_nsec) :			\
110 	    ((tsp)->tv_sec cmp (usp)->tv_sec))
111 #define	timespecadd(tsp, usp, vsp)					\
112 	do {								\
113 		(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;		\
114 		(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;	\
115 		if ((vsp)->tv_nsec >= 1000000000L) {			\
116 			(vsp)->tv_sec++;				\
117 			(vsp)->tv_nsec -= 1000000000L;			\
118 		}							\
119 	} while (/* CONSTCOND */ 0)
120 #define	timespecsub(tsp, usp, vsp)					\
121 	do {								\
122 		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
123 		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
124 		if ((vsp)->tv_nsec < 0) {				\
125 			(vsp)->tv_sec--;				\
126 			(vsp)->tv_nsec += 1000000000L;			\
127 		}							\
128 	} while (/* CONSTCOND */ 0)
129 
130 /*
131  * Names of the interval timers, and structure
132  * defining a timer setting.
133  */
134 #define	ITIMER_REAL	0
135 #define	ITIMER_VIRTUAL	1
136 #define	ITIMER_PROF	2
137 
138 struct	itimerval {
139 	struct	timeval it_interval;	/* timer interval */
140 	struct	timeval it_value;	/* current value */
141 };
142 
143 /*
144  * Getkerninfo clock information structure
145  */
146 struct clockinfo {
147 	int	hz;		/* clock frequency */
148 	int	tick;		/* micro-seconds per hz tick */
149 	int	tickadj;	/* clock skew rate for adjtime() */
150 	int	stathz;		/* statistics clock frequency */
151 	int	profhz;		/* profiling clock frequency */
152 };
153 
154 #define	CLOCK_REALTIME	0
155 #define	CLOCK_VIRTUAL	1
156 #define	CLOCK_PROF	2
157 #define	CLOCK_MONOTONIC	3
158 
159 #define	TIMER_RELTIME	0x0	/* relative timer */
160 #define	TIMER_ABSTIME	0x1	/* absolute timer */
161 
162 #ifdef _KERNEL
163 int	itimerfix __P((struct timeval *tv));
164 int	itimerdecr __P((struct itimerval *itp, int usec));
165 void	microtime __P((struct timeval *tv));
166 int	settime __P((struct timeval *));
167 int	ratecheck __P((struct timeval *, const struct timeval *));
168 int	ppsratecheck __P((struct timeval *, int *, int));
169 int	settimeofday1 __P((const struct timeval *, const struct timezone *,
170 	    struct proc *));
171 int	adjtime1 __P((const struct timeval *, struct timeval *, struct proc *));
172 int	clock_settime1 __P((clockid_t, const struct timespec *));
173 #else /* !_KERNEL */
174 
175 #ifndef _STANDALONE
176 #include <time.h>
177 
178 #ifndef _POSIX_SOURCE
179 #include <sys/cdefs.h>
180 
181 __BEGIN_DECLS
182 int	adjtime __P((const struct timeval *, struct timeval *));
183 int	futimes __P((int, const struct timeval *));
184 int	getitimer __P((int, struct itimerval *));
185 int	gettimeofday __P((struct timeval *, struct timezone *));
186 int	lutimes __P((const char *, const struct timeval *));
187 int	setitimer __P((int, const struct itimerval *, struct itimerval *));
188 int	settimeofday __P((const struct timeval *, const struct timezone *));
189 int	utimes __P((const char *, const struct timeval *));
190 __END_DECLS
191 #endif /* !POSIX */
192 
193 #endif	/* !_STANDALONE */
194 
195 #endif /* !_KERNEL */
196 
197 #endif /* !_SYS_TIME_H_ */
198