1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2 /* All Rights Reserved */
6 * Copyright (c) 1982, 1986, 1993 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
12 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
13 * Use is subject to license terms.
19 #include <sys/feature_tests.h>
22 * Structure returned by gettimeofday(2) system call,
23 * and used in other calls.
30 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
31 defined(__EXTENSIONS__)
34 #if !defined(_TIME_T) || __cplusplus >= 199711L
36 typedef long time_t; /* time of day in seconds */
41 typedef long suseconds_t
; /* signed # of microseconds */
42 #endif /* _SUSECONDS_T */
45 time_t tv_sec
; /* seconds */
46 suseconds_t tv_usec
; /* and microseconds */
49 #if defined(_SYSCALL32)
51 #include <sys/types32.h>
53 #define TIMEVAL32_TO_TIMEVAL(tv, tv32) { \
54 (tv)->tv_sec = (time_t)(tv32)->tv_sec; \
55 (tv)->tv_usec = (tv32)->tv_usec; \
58 #define TIMEVAL_TO_TIMEVAL32(tv32, tv) { \
59 (tv32)->tv_sec = (time32_t)(tv)->tv_sec; \
60 (tv32)->tv_usec = (tv)->tv_usec; \
63 #define TIME32_MAX INT32_MAX
64 #define TIME32_MIN INT32_MIN
66 #define TIMEVAL_OVERFLOW(tv) \
67 ((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
69 #endif /* _SYSCALL32 || _KERNEL */
72 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
74 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
77 int tz_minuteswest
; /* minutes west of Greenwich */
78 int tz_dsttime
; /* type of dst correction */
82 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
89 * Needed for longlong_t type. Placement of this due to <sys/types.h>
90 * including <sys/select.h> which relies on the presense of the itimerval
94 #include <sys/types.h>
101 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
103 #define DST_NONE 0 /* not on dst */
104 #define DST_USA 1 /* USA style dst */
105 #define DST_AUST 2 /* Australian style dst */
106 #define DST_WET 3 /* Western European dst */
107 #define DST_MET 4 /* Middle European dst */
108 #define DST_EET 5 /* Eastern European dst */
109 #define DST_CAN 6 /* Canada */
110 #define DST_GB 7 /* Great Britain and Eire */
111 #define DST_RUM 8 /* Rumania */
112 #define DST_TUR 9 /* Turkey */
113 #define DST_AUSTALT 10 /* Australian style with shift in 1986 */
116 * Operations on timevals.
118 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
119 #define timercmp(tvp, uvp, cmp) \
120 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
122 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
124 ((tvp)->tv_sec cmp (uvp)->tv_sec))
126 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
130 * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
131 * lint-safe. If the compiler could know that we always make tv_usec < 1000000
132 * we wouldn't need a special linted version.
134 #define timeradd(tvp, uvp, vvp) \
137 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
138 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
139 if ((vvp)->tv_usec >= 1000000) \
142 (vvp)->tv_usec -= 1000000; \
144 } while ((vvp)->tv_usec >= 1000000)
145 #define timersub(tvp, uvp, vvp) \
148 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
149 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
150 if ((vvp)->tv_usec < 0) \
153 (vvp)->tv_usec += 1000000; \
155 } while ((vvp)->tv_usec >= 1000000)
157 #define timeradd(tvp, uvp, vvp) \
160 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
161 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
162 if ((vvp)->tv_usec >= 1000000) \
165 (vvp)->tv_usec -= 1000000; \
169 #define timersub(tvp, uvp, vvp) \
172 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
173 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
174 if ((vvp)->tv_usec < 0) \
177 (vvp)->tv_usec += 1000000; \
182 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
184 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
186 * Names of the interval timers, and structure
187 * defining a timer setting.
189 #define ITIMER_REAL 0 /* Decrements in real time */
190 #define ITIMER_VIRTUAL 1 /* Decrements in process virtual time */
191 #define ITIMER_PROF 2 /* Decrements both in process virtual */
192 /* time and when system is running on */
193 /* behalf of the process. */
194 #define ITIMER_REALPROF 3 /* Decrements in real time for real- */
195 /* time profiling of multithreaded */
200 struct timeval it_interval
; /* timer interval */
201 struct timeval it_value
; /* current value */
204 #if defined(_SYSCALL32)
207 struct timeval32 it_interval
;
208 struct timeval32 it_value
;
211 #define ITIMERVAL32_TO_ITIMERVAL(itv, itv32) { \
212 TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
213 TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value); \
216 #define ITIMERVAL_TO_ITIMERVAL32(itv32, itv) { \
217 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
218 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value); \
221 #define ITIMERVAL_OVERFLOW(itv) \
222 (TIMEVAL_OVERFLOW(&(itv)->it_interval) || \
223 TIMEVAL_OVERFLOW(&(itv)->it_value))
225 #endif /* _SYSCALL32 */
227 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
230 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
232 * Definitions for commonly used resolutions.
235 #define MILLISEC 1000
236 #define MICROSEC 1000000
237 #define NANOSEC 1000000000
239 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
244 * Time expressed as a 64-bit nanosecond counter.
246 typedef longlong_t hrtime_t
;
250 #include <sys/time_impl.h>
251 #include <sys/mutex.h>
253 extern int tick_per_msec
; /* clock ticks per millisecond (may be zero) */
254 extern int msec_per_tick
; /* milliseconds per clock tick (may be zero) */
255 extern int usec_per_tick
; /* microseconds per clock tick */
256 extern int nsec_per_tick
; /* nanoseconds per clock tick */
259 * Macros to convert from common units of time (sec, msec, usec, nsec,
260 * timeval, timestruc) to clock ticks and vice versa.
262 #define TICK_TO_SEC(tick) ((tick) / hz)
263 #define SEC_TO_TICK(sec) ((sec) * hz)
265 #define TICK_TO_MSEC(tick) \
266 (msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
267 #define MSEC_TO_TICK(msec) \
268 (msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
269 #define MSEC_TO_TICK_ROUNDUP(msec) \
271 ((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
272 (msec) * tick_per_msec)
274 #define TICK_TO_USEC(tick) ((tick) * usec_per_tick)
275 #define USEC_TO_TICK(usec) ((usec) / usec_per_tick)
276 #define USEC_TO_TICK_ROUNDUP(usec) \
277 ((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
279 #define TICK_TO_NSEC(tick) ((hrtime_t)(tick) * nsec_per_tick)
280 #define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
281 #define NSEC_TO_TICK_ROUNDUP(nsec) \
282 ((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
284 #define TICK_TO_TIMEVAL(tick, tvp) { \
285 clock_t __tmptck = (tick); \
286 (tvp)->tv_sec = TICK_TO_SEC(__tmptck); \
287 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
290 #define TICK_TO_TIMEVAL32(tick, tvp) { \
291 clock_t __tmptck = (tick); \
292 time_t __tmptm = TICK_TO_SEC(__tmptck); \
293 (tvp)->tv_sec = (time32_t)__tmptm; \
294 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
297 #define TICK_TO_TIMESTRUC(tick, tsp) { \
298 clock_t __tmptck = (tick); \
299 (tsp)->tv_sec = TICK_TO_SEC(__tmptck); \
300 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
303 #define TICK_TO_TIMESTRUC32(tick, tsp) { \
304 clock_t __tmptck = (tick); \
305 time_t __tmptm = TICK_TO_SEC(__tmptck); \
306 (tsp)->tv_sec = (time32_t)__tmptm; \
307 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm)); \
310 #define TIMEVAL_TO_TICK(tvp) \
311 (SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
313 #define TIMESTRUC_TO_TICK(tsp) \
314 (SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
316 typedef struct todinfo
{
317 int tod_sec
; /* seconds 0-59 */
318 int tod_min
; /* minutes 0-59 */
319 int tod_hour
; /* hours 0-23 */
320 int tod_dow
; /* day of week 1-7 */
321 int tod_day
; /* day of month 1-31 */
322 int tod_month
; /* month 1-12 */
323 int tod_year
; /* year 70+ */
326 extern int64_t timedelta
;
327 extern int timechanged
;
328 extern int tod_needsync
;
329 extern kmutex_t tod_lock
;
330 extern volatile timestruc_t hrestime
;
331 extern hrtime_t hres_last_tick
;
332 extern int64_t hrestime_adj
;
333 extern uint_t adj_shift
;
335 extern timestruc_t
tod_get(void);
336 extern void tod_set(timestruc_t
);
337 extern void set_hrestime(timestruc_t
*);
338 extern todinfo_t
utc_to_tod(time_t);
339 extern time_t tod_to_utc(todinfo_t
);
340 extern int hr_clock_lock(void);
341 extern void hr_clock_unlock(int);
342 extern hrtime_t
gethrtime(void);
343 extern hrtime_t
gethrtime_unscaled(void);
344 extern hrtime_t
gethrtime_max(void);
345 extern hrtime_t
gethrtime_waitfree(void);
346 extern void scalehrtime(hrtime_t
*);
347 extern void gethrestime(timespec_t
*);
348 extern time_t gethrestime_sec(void);
349 extern void gethrestime_lasttick(timespec_t
*);
350 extern void hrt2ts(hrtime_t
, timestruc_t
*);
351 extern hrtime_t
ts2hrt(const timestruc_t
*);
352 extern void hrt2tv(hrtime_t
, struct timeval
*);
353 extern hrtime_t
tv2hrt(struct timeval
*);
354 extern int itimerfix(struct timeval
*, int);
355 extern int itimerdecr(struct itimerval
*, int);
356 extern void timevaladd(struct timeval
*, struct timeval
*);
357 extern void timevalsub(struct timeval
*, struct timeval
*);
358 extern void timevalfix(struct timeval
*);
359 extern void dtrace_hres_tick(void);
361 #if defined(_SYSCALL32)
362 extern void hrt2ts32(hrtime_t
, timestruc32_t
*);
367 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
368 #if defined(__STDC__)
369 int adjtime(struct timeval
*, struct timeval
*);
373 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
375 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
376 defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
377 #if defined(__STDC__)
378 int futimesat(int, const char *, const struct timeval
*);
381 #endif /* defined(__STDC__) */
382 #endif /* defined(__ATFILE_SOURCE) */
384 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
385 defined(__EXTENSIONS__)
387 #if defined(__STDC__)
389 int getitimer(int, struct itimerval
*);
390 int utimes(const char *, const struct timeval
*);
392 int setitimer(int, const struct itimerval
*_RESTRICT_KYWD
,
393 struct itimerval
*_RESTRICT_KYWD
);
395 int setitimer(int, struct itimerval
*_RESTRICT_KYWD
,
396 struct itimerval
*_RESTRICT_KYWD
);
397 #endif /* defined(_XPG2_2) */
404 #endif /* __STDC__ */
405 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
408 * gettimeofday() and settimeofday() were included in SVr4 due to their
409 * common use in BSD based applications. They were to be included exactly
410 * as in BSD, with two parameters. However, AT&T/USL noted that the second
411 * parameter was unused and deleted it, thereby making a routine included
412 * for compatibility, incompatible.
414 * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
417 * This has caused general disagreement in the application community as to
418 * the syntax of these routines. Solaris defaults to the XSH4.2 definition.
419 * The flag _SVID_GETTOD may be used to force the SVID version.
421 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
423 #if defined(__STDC__)
424 #if defined(_SVID_GETTOD)
425 int settimeofday(struct timeval
*);
427 int settimeofday(struct timeval
*, void *);
429 hrtime_t
gethrtime(void);
430 hrtime_t
gethrvtime(void);
433 hrtime_t
gethrtime();
434 hrtime_t
gethrvtime();
435 #endif /* __STDC__ */
437 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
439 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
440 defined(__EXTENSIONS__)
442 #if defined(__STDC__)
443 #if defined(_SVID_GETTOD)
444 int gettimeofday(struct timeval
*);
446 int gettimeofday(struct timeval
*_RESTRICT_KYWD
, void *_RESTRICT_KYWD
);
450 #endif /* __STDC__ */
452 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
455 * The inclusion of <time.h> is historical and was added for
456 * backward compatibility in delta 1.2 when a number of definitions
457 * were moved out of <sys/time.h>. More recently, the timespec and
458 * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
459 * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
460 * which is now included by <time.h>. This change was due to POSIX
461 * 1003.1b-1993 and X/Open UNIX 98 requirements. For non-POSIX and
462 * non-X/Open applications, including this header will still make
463 * visible these definitions.
465 #if !defined(_BOOT) && !defined(_KERNEL) && \
466 !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
471 * The inclusion of <sys/select.h> is needed for the FD_CLR,
472 * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
473 * select() prototype defined in the XOpen specifications
474 * beginning with XSH4v2. Placement required after definition
477 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
478 defined(__EXTENSIONS__)
479 #include <sys/select.h>
488 #endif /* _SYS_TIME_H */