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