time.h revision 96052
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
3314487Shsu *	@(#)time.h	8.5 (Berkeley) 5/4/95
3450477Speter * $FreeBSD: head/sys/sys/time.h 96052 2002-05-05 04:33:09Z bde $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#ifndef _SYS_TIME_H_
381541Srgrimes#define _SYS_TIME_H_
391541Srgrimes
4014487Shsu#include <sys/types.h>
4114487Shsu
421541Srgrimes/*
431541Srgrimes * Structure returned by gettimeofday(2) system call,
441541Srgrimes * and used in other calls.
451541Srgrimes */
461541Srgrimesstruct timeval {
471541Srgrimes	long	tv_sec;		/* seconds */
481541Srgrimes	long	tv_usec;	/* and microseconds */
491541Srgrimes};
501541Srgrimes
5125775Speter#ifndef _TIMESPEC_DECLARED
5225775Speter#define _TIMESPEC_DECLARED
531541Srgrimesstruct timespec {
5418397Snate	time_t	tv_sec;		/* seconds */
5518397Snate	long	tv_nsec;	/* and nanoseconds */
561541Srgrimes};
5725775Speter#endif
581541Srgrimes
5941827Sdes#define	TIMEVAL_TO_TIMESPEC(tv, ts)					\
6041827Sdes	do {								\
6141827Sdes		(ts)->tv_sec = (tv)->tv_sec;				\
6241827Sdes		(ts)->tv_nsec = (tv)->tv_usec * 1000;			\
6341827Sdes	} while (0)
6441827Sdes#define	TIMESPEC_TO_TIMEVAL(tv, ts)					\
6541827Sdes	do {								\
6641827Sdes		(tv)->tv_sec = (ts)->tv_sec;				\
6741827Sdes		(tv)->tv_usec = (ts)->tv_nsec / 1000;			\
6841827Sdes	} while (0)
691541Srgrimes
701541Srgrimesstruct timezone {
711541Srgrimes	int	tz_minuteswest;	/* minutes west of Greenwich */
721541Srgrimes	int	tz_dsttime;	/* type of dst correction */
731541Srgrimes};
741541Srgrimes#define	DST_NONE	0	/* not on dst */
751541Srgrimes#define	DST_USA		1	/* USA style dst */
761541Srgrimes#define	DST_AUST	2	/* Australian style dst */
771541Srgrimes#define	DST_WET		3	/* Western European dst */
781541Srgrimes#define	DST_MET		4	/* Middle European dst */
791541Srgrimes#define	DST_EET		5	/* Eastern European dst */
801541Srgrimes#define	DST_CAN		6	/* Canada */
811541Srgrimes
8290362Sphkstruct bintime {
8395817Sphk	time_t	sec;
8495817Sphk	uint64_t frac;
8590362Sphk};
8690362Sphk
8790362Sphkstatic __inline void
8892769Sphkbintime_addx(struct bintime *bt, uint64_t x)
8990362Sphk{
9092769Sphk	uint64_t u;
9190362Sphk
9290362Sphk	u = bt->frac;
9390362Sphk	bt->frac += x;
9490362Sphk	if (u > bt->frac)
9590362Sphk		bt->sec++;
9690362Sphk}
9790362Sphk
9890362Sphkstatic __inline void
9990362Sphkbintime_add(struct bintime *bt, struct bintime *bt2)
10090362Sphk{
10192769Sphk	uint64_t u;
10290362Sphk
10390362Sphk	u = bt->frac;
10490362Sphk	bt->frac += bt2->frac;
10590362Sphk	if (u > bt->frac)
10690362Sphk		bt->sec++;
10790362Sphk	bt->sec += bt2->sec;
10890362Sphk}
10990362Sphk
11090362Sphkstatic __inline void
11190362Sphkbintime_sub(struct bintime *bt, struct bintime *bt2)
11290362Sphk{
11392769Sphk	uint64_t u;
11490362Sphk
11590362Sphk	u = bt->frac;
11690362Sphk	bt->frac -= bt2->frac;
11790362Sphk	if (u < bt->frac)
11890362Sphk		bt->sec--;
11990362Sphk	bt->sec -= bt2->sec;
12090362Sphk}
12190362Sphk
12292769Sphk/*-
12392769Sphk * Background information:
12492769Sphk *
12592769Sphk * When converting between timestamps on parallel timescales of differing
12692769Sphk * resolutions it is historical and scientific practice to round down rather
12792769Sphk * than doing 4/5 rounding.
12892769Sphk *
12992769Sphk *   The date changes at midnight, not at noon.
13092769Sphk *
13192769Sphk *   Even at 15:59:59.999999999 it's not four'o'clock.
13292769Sphk *
13392769Sphk *   time_second ticks after N.999999999 not after N.4999999999
13492769Sphk */
13592769Sphk
13690362Sphkstatic __inline void
13790362Sphkbintime2timespec(struct bintime *bt, struct timespec *ts)
13890362Sphk{
13990362Sphk
14090362Sphk	ts->tv_sec = bt->sec;
14195817Sphk	ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
14290362Sphk}
14390362Sphk
14490362Sphkstatic __inline void
14590362Sphktimespec2bintime(struct timespec *ts, struct bintime *bt)
14690362Sphk{
14790362Sphk
14890362Sphk	bt->sec = ts->tv_sec;
14992769Sphk	/* 18446744073 = int(2^64 / 1000000000) */
15092769Sphk	bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
15190362Sphk}
15290362Sphk
15390362Sphkstatic __inline void
15490362Sphkbintime2timeval(struct bintime *bt, struct timeval *tv)
15590362Sphk{
15690362Sphk
15790362Sphk	tv->tv_sec = bt->sec;
15895817Sphk	tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
15990362Sphk}
16090362Sphk
16190362Sphkstatic __inline void
16290362Sphktimeval2bintime(struct timeval *tv, struct bintime *bt)
16390362Sphk{
16490362Sphk
16590362Sphk	bt->sec = tv->tv_sec;
16692769Sphk	/* 18446744073709 = int(2^64 / 1000000) */
16792769Sphk	bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
16890362Sphk}
16990362Sphk
17055205Speter#ifdef _KERNEL
17135058Sphk
17235029Sphk/* Operations on timespecs */
17335401Seivind#define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
17435029Sphk#define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
17535029Sphk#define	timespeccmp(tvp, uvp, cmp)					\
17635029Sphk	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
17735029Sphk	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
17835029Sphk	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
17935058Sphk#define timespecadd(vvp, uvp)						\
18035029Sphk	do {								\
18135058Sphk		(vvp)->tv_sec += (uvp)->tv_sec;				\
18235058Sphk		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
18335029Sphk		if ((vvp)->tv_nsec >= 1000000000) {			\
18435029Sphk			(vvp)->tv_sec++;				\
18535029Sphk			(vvp)->tv_nsec -= 1000000000;			\
18635029Sphk		}							\
18735029Sphk	} while (0)
18835058Sphk#define timespecsub(vvp, uvp)						\
18935029Sphk	do {								\
19035058Sphk		(vvp)->tv_sec -= (uvp)->tv_sec;				\
19135058Sphk		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
19235029Sphk		if ((vvp)->tv_nsec < 0) {				\
19335029Sphk			(vvp)->tv_sec--;				\
19435029Sphk			(vvp)->tv_nsec += 1000000000;			\
19535029Sphk		}							\
19635029Sphk	} while (0)
19735058Sphk
1981541Srgrimes/* Operations on timevals. */
19935058Sphk
20074574Smarkm#define	timevalclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
20135058Sphk#define	timevalisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
20235029Sphk#define	timevalcmp(tvp, uvp, cmp)					\
20335029Sphk	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
20435029Sphk	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
20535029Sphk	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
20635058Sphk
20735058Sphk/* timevaladd and timevalsub are not inlined */
20835058Sphk
20955205Speter#endif /* _KERNEL */
21035029Sphk
21172093Sasmodai#ifndef _KERNEL			/* NetBSD/OpenBSD compatible interfaces */
21235058Sphk
21374574Smarkm#define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
2141541Srgrimes#define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
21535058Sphk#define	timercmp(tvp, uvp, cmp)					\
2161541Srgrimes	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
2171541Srgrimes	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
2181541Srgrimes	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
21921099Speter#define timeradd(tvp, uvp, vvp)						\
22021099Speter	do {								\
22121099Speter		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
22221099Speter		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
22321099Speter		if ((vvp)->tv_usec >= 1000000) {			\
22421099Speter			(vvp)->tv_sec++;				\
22521099Speter			(vvp)->tv_usec -= 1000000;			\
22621099Speter		}							\
22721099Speter	} while (0)
22821099Speter#define timersub(tvp, uvp, vvp)						\
22921099Speter	do {								\
23021099Speter		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
23121099Speter		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
23221099Speter		if ((vvp)->tv_usec < 0) {				\
23321099Speter			(vvp)->tv_sec--;				\
23421099Speter			(vvp)->tv_usec += 1000000;			\
23521099Speter		}							\
23621099Speter	} while (0)
23721099Speter#endif
2381541Srgrimes
2391541Srgrimes/*
2401541Srgrimes * Names of the interval timers, and structure
2411541Srgrimes * defining a timer setting.
2421541Srgrimes */
2431541Srgrimes#define	ITIMER_REAL	0
2441541Srgrimes#define	ITIMER_VIRTUAL	1
2451541Srgrimes#define	ITIMER_PROF	2
2461541Srgrimes
24783045Sobrienstruct itimerval {
2481541Srgrimes	struct	timeval it_interval;	/* timer interval */
2491541Srgrimes	struct	timeval it_value;	/* current value */
2501541Srgrimes};
2511541Srgrimes
2521541Srgrimes/*
2531541Srgrimes * Getkerninfo clock information structure
2541541Srgrimes */
2551541Srgrimesstruct clockinfo {
2561541Srgrimes	int	hz;		/* clock frequency */
2571541Srgrimes	int	tick;		/* micro-seconds per hz tick */
25896052Sbde	int	spare;
2591541Srgrimes	int	stathz;		/* statistics clock frequency */
2601541Srgrimes	int	profhz;		/* profiling clock frequency */
2611541Srgrimes};
2621541Srgrimes
26334030Sdufault/* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
26434030Sdufault
26534030Sdufault#ifndef CLOCK_REALTIME
26625578Speter#define CLOCK_REALTIME	0
26734030Sdufault#endif
26825578Speter#define CLOCK_VIRTUAL	1
26925578Speter#define CLOCK_PROF	2
27025578Speter
27125578Speter#define TIMER_RELTIME	0x0	/* relative timer */
27234030Sdufault#ifndef TIMER_ABSTIME
27325578Speter#define TIMER_ABSTIME	0x1	/* absolute timer */
27434030Sdufault#endif
27525578Speter
27655205Speter#ifdef _KERNEL
27734961Sphkextern time_t	time_second;
27833690Sphk
27995817Sphk/*
28095491Sphk * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
28195491Sphk *
28295491Sphk * Functions without the "get" prefix returns the best timestamp
28395491Sphk * we can produce in the given format.
28495491Sphk *
28595491Sphk * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
28695491Sphk * "nano"  == struct timespec == seconds + nanoseconds.
28795491Sphk * "micro" == struct timeval  == seconds + microseconds.
28895491Sphk *
28995491Sphk * Functions containing "up" returns time relative to boot and
29095491Sphk * should be used for calculating time intervals.
29195491Sphk *
29295491Sphk * Functions without "up" returns GMT time.
29395491Sphk *
29495491Sphk * Functions with the "get" prefix returns a less precise result
29595491Sphk * much faster than the functions without "get" prefix and should
29695491Sphk * be used where a precision of 10 msec is acceptable or where
29795491Sphk * performance is priority. (NB: "precision", _not_ "resolution" !)
29895491Sphk *
29995491Sphk */
30095491Sphk
30190362Sphkvoid	binuptime(struct bintime *bt);
30295817Sphkvoid	nanouptime(struct timespec *tsp);
30395817Sphkvoid	microuptime(struct timeval *tvp);
30495491Sphk
30590362Sphkvoid	bintime(struct bintime *bt);
30695817Sphkvoid	nanotime(struct timespec *tsp);
30795817Sphkvoid	microtime(struct timeval *tvp);
30895491Sphk
30995491Sphkvoid	getbinuptime(struct bintime *bt);
31095491Sphkvoid	getnanouptime(struct timespec *tsp);
31195817Sphkvoid	getmicrouptime(struct timeval *tvp);
31295491Sphk
31395491Sphkvoid	getbintime(struct bintime *bt);
31495491Sphkvoid	getnanotime(struct timespec *tsp);
31595817Sphkvoid	getmicrotime(struct timeval *tvp);
31695491Sphk
31795817Sphk/* Other functions */
31892719Salfredint	itimerdecr(struct itimerval *itp, int usec);
31992719Salfredint	itimerfix(struct timeval *tv);
32095817Sphkvoid	timevaladd(struct timeval *t1, struct timeval *t2);
32195817Sphkvoid	timevalsub(struct timeval *t1, struct timeval *t2);
32295817Sphkint	tvtohz(struct timeval *tv);
32355205Speter#else /* !_KERNEL */
3241541Srgrimes#include <time.h>
3251541Srgrimes
3261541Srgrimes#include <sys/cdefs.h>
3271541Srgrimes
3281541Srgrimes__BEGIN_DECLS
32992719Salfredint	adjtime(const struct timeval *, struct timeval *);
33092719Salfredint	futimes(int, const struct timeval *);
33192719Salfredint	getitimer(int, struct itimerval *);
33292719Salfredint	gettimeofday(struct timeval *, struct timezone *);
33392719Salfredint	lutimes(const char *, const struct timeval *);
33492719Salfredint	setitimer(int, const struct itimerval *, struct itimerval *);
33592719Salfredint	settimeofday(const struct timeval *, const struct timezone *);
33692719Salfredint	utimes(const char *, const struct timeval *);
3371541Srgrimes__END_DECLS
3381541Srgrimes
33955205Speter#endif /* !_KERNEL */
3401541Srgrimes
3411541Srgrimes#endif /* !_SYS_TIME_H_ */
342