time.h revision 72093
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 72093 2001-02-06 12:05:58Z asmodai $ 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 8255205Speter#ifdef _KERNEL 8335058Sphk 8435029Sphk/* Operations on timespecs */ 8535401Seivind#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) 8635029Sphk#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) 8735029Sphk#define timespeccmp(tvp, uvp, cmp) \ 8835029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 8935029Sphk ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 9035029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 9135058Sphk#define timespecadd(vvp, uvp) \ 9235029Sphk do { \ 9335058Sphk (vvp)->tv_sec += (uvp)->tv_sec; \ 9435058Sphk (vvp)->tv_nsec += (uvp)->tv_nsec; \ 9535029Sphk if ((vvp)->tv_nsec >= 1000000000) { \ 9635029Sphk (vvp)->tv_sec++; \ 9735029Sphk (vvp)->tv_nsec -= 1000000000; \ 9835029Sphk } \ 9935029Sphk } while (0) 10035058Sphk#define timespecsub(vvp, uvp) \ 10135029Sphk do { \ 10235058Sphk (vvp)->tv_sec -= (uvp)->tv_sec; \ 10335058Sphk (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 10435029Sphk if ((vvp)->tv_nsec < 0) { \ 10535029Sphk (vvp)->tv_sec--; \ 10635029Sphk (vvp)->tv_nsec += 1000000000; \ 10735029Sphk } \ 10835029Sphk } while (0) 10935058Sphk 1101541Srgrimes/* Operations on timevals. */ 11135058Sphk 11235058Sphk#define timevalclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 11335058Sphk#define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 11435029Sphk#define timevalcmp(tvp, uvp, cmp) \ 11535029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 11635029Sphk ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 11735029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 11835058Sphk 11935058Sphk/* timevaladd and timevalsub are not inlined */ 12035058Sphk 12155205Speter#endif /* _KERNEL */ 12235029Sphk 12372093Sasmodai#ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ 12435058Sphk 12535401Seivind#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 1261541Srgrimes#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 12735058Sphk#define timercmp(tvp, uvp, cmp) \ 1281541Srgrimes (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 1291541Srgrimes ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 1301541Srgrimes ((tvp)->tv_sec cmp (uvp)->tv_sec)) 13121099Speter#define timeradd(tvp, uvp, vvp) \ 13221099Speter do { \ 13321099Speter (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 13421099Speter (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 13521099Speter if ((vvp)->tv_usec >= 1000000) { \ 13621099Speter (vvp)->tv_sec++; \ 13721099Speter (vvp)->tv_usec -= 1000000; \ 13821099Speter } \ 13921099Speter } while (0) 14021099Speter#define timersub(tvp, uvp, vvp) \ 14121099Speter do { \ 14221099Speter (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 14321099Speter (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 14421099Speter if ((vvp)->tv_usec < 0) { \ 14521099Speter (vvp)->tv_sec--; \ 14621099Speter (vvp)->tv_usec += 1000000; \ 14721099Speter } \ 14821099Speter } while (0) 14921099Speter#endif 1501541Srgrimes 1511541Srgrimes/* 1521541Srgrimes * Names of the interval timers, and structure 1531541Srgrimes * defining a timer setting. 1541541Srgrimes */ 1551541Srgrimes#define ITIMER_REAL 0 1561541Srgrimes#define ITIMER_VIRTUAL 1 1571541Srgrimes#define ITIMER_PROF 2 1581541Srgrimes 1591541Srgrimesstruct itimerval { 1601541Srgrimes struct timeval it_interval; /* timer interval */ 1611541Srgrimes struct timeval it_value; /* current value */ 1621541Srgrimes}; 1631541Srgrimes 1641541Srgrimes/* 1651541Srgrimes * Getkerninfo clock information structure 1661541Srgrimes */ 1671541Srgrimesstruct clockinfo { 1681541Srgrimes int hz; /* clock frequency */ 1691541Srgrimes int tick; /* micro-seconds per hz tick */ 17026897Sjhay int tickadj; /* clock skew rate for adjtime() */ 1711541Srgrimes int stathz; /* statistics clock frequency */ 1721541Srgrimes int profhz; /* profiling clock frequency */ 1731541Srgrimes}; 1741541Srgrimes 17534030Sdufault/* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */ 17634030Sdufault 17734030Sdufault#ifndef CLOCK_REALTIME 17825578Speter#define CLOCK_REALTIME 0 17934030Sdufault#endif 18025578Speter#define CLOCK_VIRTUAL 1 18125578Speter#define CLOCK_PROF 2 18225578Speter 18325578Speter#define TIMER_RELTIME 0x0 /* relative timer */ 18434030Sdufault#ifndef TIMER_ABSTIME 18525578Speter#define TIMER_ABSTIME 0x1 /* absolute timer */ 18634030Sdufault#endif 18725578Speter 18855205Speter#ifdef _KERNEL 18934961Sphkextern time_t time_second; 19033690Sphk 19136119Sphkvoid getmicrouptime __P((struct timeval *tv)); 19234901Sphkvoid getmicrotime __P((struct timeval *tv)); 19336119Sphkvoid getnanouptime __P((struct timespec *tv)); 19434901Sphkvoid getnanotime __P((struct timespec *tv)); 19535029Sphkint itimerdecr __P((struct itimerval *itp, int usec)); 19614487Shsuint itimerfix __P((struct timeval *tv)); 19736119Sphkvoid microuptime __P((struct timeval *tv)); 19833690Sphkvoid microtime __P((struct timeval *tv)); 19936119Sphkvoid nanouptime __P((struct timespec *ts)); 20033690Sphkvoid nanotime __P((struct timespec *ts)); 2013484Sphkvoid timevaladd __P((struct timeval *, struct timeval *)); 2023484Sphkvoid timevalsub __P((struct timeval *, struct timeval *)); 20334961Sphkint tvtohz __P((struct timeval *)); 20455205Speter#else /* !_KERNEL */ 2051541Srgrimes#include <time.h> 2061541Srgrimes 2071541Srgrimes#include <sys/cdefs.h> 2081541Srgrimes 2091541Srgrimes__BEGIN_DECLS 2101541Srgrimesint adjtime __P((const struct timeval *, struct timeval *)); 21155045Sbdeint futimes __P((int, const struct timeval *)); 2121541Srgrimesint getitimer __P((int, struct itimerval *)); 2131541Srgrimesint gettimeofday __P((struct timeval *, struct timezone *)); 21455045Sbdeint lutimes __P((const char *, const struct timeval *)); 2151541Srgrimesint setitimer __P((int, const struct itimerval *, struct itimerval *)); 2161541Srgrimesint settimeofday __P((const struct timeval *, const struct timezone *)); 2171541Srgrimesint utimes __P((const char *, const struct timeval *)); 2181541Srgrimes__END_DECLS 2191541Srgrimes 22055205Speter#endif /* !_KERNEL */ 2211541Srgrimes 2221541Srgrimes#endif /* !_SYS_TIME_H_ */ 223