time.h revision 108477
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 108477 2002-12-31 04:08:41Z mike $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#ifndef _SYS_TIME_H_ 381541Srgrimes#define _SYS_TIME_H_ 391541Srgrimes 40108477Smike#include <sys/_timeval.h> 4114487Shsu#include <sys/types.h> 4298270Swollman#include <sys/timespec.h> 4314487Shsu 441541Srgrimesstruct timezone { 451541Srgrimes int tz_minuteswest; /* minutes west of Greenwich */ 461541Srgrimes int tz_dsttime; /* type of dst correction */ 471541Srgrimes}; 481541Srgrimes#define DST_NONE 0 /* not on dst */ 491541Srgrimes#define DST_USA 1 /* USA style dst */ 501541Srgrimes#define DST_AUST 2 /* Australian style dst */ 511541Srgrimes#define DST_WET 3 /* Western European dst */ 521541Srgrimes#define DST_MET 4 /* Middle European dst */ 531541Srgrimes#define DST_EET 5 /* Eastern European dst */ 541541Srgrimes#define DST_CAN 6 /* Canada */ 551541Srgrimes 5698270Swollman#if __BSD_VISIBLE 5790362Sphkstruct bintime { 5895817Sphk time_t sec; 5995817Sphk uint64_t frac; 6090362Sphk}; 6190362Sphk 6290362Sphkstatic __inline void 6392769Sphkbintime_addx(struct bintime *bt, uint64_t x) 6490362Sphk{ 6592769Sphk uint64_t u; 6690362Sphk 6790362Sphk u = bt->frac; 6890362Sphk bt->frac += x; 6990362Sphk if (u > bt->frac) 7090362Sphk bt->sec++; 7190362Sphk} 7290362Sphk 7390362Sphkstatic __inline void 7490362Sphkbintime_add(struct bintime *bt, struct bintime *bt2) 7590362Sphk{ 7692769Sphk uint64_t u; 7790362Sphk 7890362Sphk u = bt->frac; 7990362Sphk bt->frac += bt2->frac; 8090362Sphk if (u > bt->frac) 8190362Sphk bt->sec++; 8290362Sphk bt->sec += bt2->sec; 8390362Sphk} 8490362Sphk 8590362Sphkstatic __inline void 8690362Sphkbintime_sub(struct bintime *bt, struct bintime *bt2) 8790362Sphk{ 8892769Sphk uint64_t u; 8990362Sphk 9090362Sphk u = bt->frac; 9190362Sphk bt->frac -= bt2->frac; 9290362Sphk if (u < bt->frac) 9390362Sphk bt->sec--; 9490362Sphk bt->sec -= bt2->sec; 9590362Sphk} 9690362Sphk 9792769Sphk/*- 9892769Sphk * Background information: 9992769Sphk * 10092769Sphk * When converting between timestamps on parallel timescales of differing 10192769Sphk * resolutions it is historical and scientific practice to round down rather 10292769Sphk * than doing 4/5 rounding. 10392769Sphk * 10492769Sphk * The date changes at midnight, not at noon. 10592769Sphk * 10692769Sphk * Even at 15:59:59.999999999 it's not four'o'clock. 10792769Sphk * 10892769Sphk * time_second ticks after N.999999999 not after N.4999999999 10992769Sphk */ 11092769Sphk 11190362Sphkstatic __inline void 11290362Sphkbintime2timespec(struct bintime *bt, struct timespec *ts) 11390362Sphk{ 11490362Sphk 11590362Sphk ts->tv_sec = bt->sec; 11695817Sphk ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32; 11790362Sphk} 11890362Sphk 11990362Sphkstatic __inline void 12090362Sphktimespec2bintime(struct timespec *ts, struct bintime *bt) 12190362Sphk{ 12290362Sphk 12390362Sphk bt->sec = ts->tv_sec; 12492769Sphk /* 18446744073 = int(2^64 / 1000000000) */ 12592769Sphk bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 12690362Sphk} 12790362Sphk 12890362Sphkstatic __inline void 12990362Sphkbintime2timeval(struct bintime *bt, struct timeval *tv) 13090362Sphk{ 13190362Sphk 13290362Sphk tv->tv_sec = bt->sec; 13395817Sphk tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32; 13490362Sphk} 13590362Sphk 13690362Sphkstatic __inline void 13790362Sphktimeval2bintime(struct timeval *tv, struct bintime *bt) 13890362Sphk{ 13990362Sphk 14090362Sphk bt->sec = tv->tv_sec; 14192769Sphk /* 18446744073709 = int(2^64 / 1000000) */ 14292769Sphk bt->frac = tv->tv_usec * (uint64_t)18446744073709LL; 14390362Sphk} 14498270Swollman#endif /* __BSD_VISIBLE */ 14590362Sphk 14655205Speter#ifdef _KERNEL 14735058Sphk 14835029Sphk/* Operations on timespecs */ 14935401Seivind#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) 15035029Sphk#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) 15135029Sphk#define timespeccmp(tvp, uvp, cmp) \ 15235029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 15335029Sphk ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 15435029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 15535058Sphk#define timespecadd(vvp, uvp) \ 15635029Sphk do { \ 15735058Sphk (vvp)->tv_sec += (uvp)->tv_sec; \ 15835058Sphk (vvp)->tv_nsec += (uvp)->tv_nsec; \ 15935029Sphk if ((vvp)->tv_nsec >= 1000000000) { \ 16035029Sphk (vvp)->tv_sec++; \ 16135029Sphk (vvp)->tv_nsec -= 1000000000; \ 16235029Sphk } \ 16335029Sphk } while (0) 16435058Sphk#define timespecsub(vvp, uvp) \ 16535029Sphk do { \ 16635058Sphk (vvp)->tv_sec -= (uvp)->tv_sec; \ 16735058Sphk (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 16835029Sphk if ((vvp)->tv_nsec < 0) { \ 16935029Sphk (vvp)->tv_sec--; \ 17035029Sphk (vvp)->tv_nsec += 1000000000; \ 17135029Sphk } \ 17235029Sphk } while (0) 17335058Sphk 1741541Srgrimes/* Operations on timevals. */ 17535058Sphk 17674574Smarkm#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 17735058Sphk#define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 17835029Sphk#define timevalcmp(tvp, uvp, cmp) \ 17935029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 18035029Sphk ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 18135029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 18235058Sphk 18335058Sphk/* timevaladd and timevalsub are not inlined */ 18435058Sphk 18555205Speter#endif /* _KERNEL */ 18635029Sphk 18772093Sasmodai#ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ 18835058Sphk 18974574Smarkm#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 1901541Srgrimes#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 19135058Sphk#define timercmp(tvp, uvp, cmp) \ 1921541Srgrimes (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 1931541Srgrimes ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 1941541Srgrimes ((tvp)->tv_sec cmp (uvp)->tv_sec)) 19521099Speter#define timeradd(tvp, uvp, vvp) \ 19621099Speter do { \ 19721099Speter (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 19821099Speter (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 19921099Speter if ((vvp)->tv_usec >= 1000000) { \ 20021099Speter (vvp)->tv_sec++; \ 20121099Speter (vvp)->tv_usec -= 1000000; \ 20221099Speter } \ 20321099Speter } while (0) 20421099Speter#define timersub(tvp, uvp, vvp) \ 20521099Speter do { \ 20621099Speter (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 20721099Speter (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 20821099Speter if ((vvp)->tv_usec < 0) { \ 20921099Speter (vvp)->tv_sec--; \ 21021099Speter (vvp)->tv_usec += 1000000; \ 21121099Speter } \ 21221099Speter } while (0) 21321099Speter#endif 2141541Srgrimes 2151541Srgrimes/* 2161541Srgrimes * Names of the interval timers, and structure 2171541Srgrimes * defining a timer setting. 2181541Srgrimes */ 2191541Srgrimes#define ITIMER_REAL 0 2201541Srgrimes#define ITIMER_VIRTUAL 1 2211541Srgrimes#define ITIMER_PROF 2 2221541Srgrimes 22383045Sobrienstruct itimerval { 2241541Srgrimes struct timeval it_interval; /* timer interval */ 2251541Srgrimes struct timeval it_value; /* current value */ 2261541Srgrimes}; 2271541Srgrimes 2281541Srgrimes/* 2291541Srgrimes * Getkerninfo clock information structure 2301541Srgrimes */ 2311541Srgrimesstruct clockinfo { 2321541Srgrimes int hz; /* clock frequency */ 2331541Srgrimes int tick; /* micro-seconds per hz tick */ 23496052Sbde int spare; 2351541Srgrimes int stathz; /* statistics clock frequency */ 2361541Srgrimes int profhz; /* profiling clock frequency */ 2371541Srgrimes}; 2381541Srgrimes 23934030Sdufault/* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */ 24034030Sdufault 24134030Sdufault#ifndef CLOCK_REALTIME 24225578Speter#define CLOCK_REALTIME 0 24334030Sdufault#endif 24425578Speter#define CLOCK_VIRTUAL 1 24525578Speter#define CLOCK_PROF 2 24625578Speter 24725578Speter#define TIMER_RELTIME 0x0 /* relative timer */ 24834030Sdufault#ifndef TIMER_ABSTIME 24925578Speter#define TIMER_ABSTIME 0x1 /* absolute timer */ 25034030Sdufault#endif 25125578Speter 25255205Speter#ifdef _KERNEL 25334961Sphkextern time_t time_second; 254106304Sphkextern time_t time_uptime; 25533690Sphk 25695817Sphk/* 25795491Sphk * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() 25895491Sphk * 25995491Sphk * Functions without the "get" prefix returns the best timestamp 26095491Sphk * we can produce in the given format. 26195491Sphk * 26295491Sphk * "bin" == struct bintime == seconds + 64 bit fraction of seconds. 26395491Sphk * "nano" == struct timespec == seconds + nanoseconds. 26495491Sphk * "micro" == struct timeval == seconds + microseconds. 26595491Sphk * 26695491Sphk * Functions containing "up" returns time relative to boot and 26795491Sphk * should be used for calculating time intervals. 26895491Sphk * 26995491Sphk * Functions without "up" returns GMT time. 27095491Sphk * 27195491Sphk * Functions with the "get" prefix returns a less precise result 27295491Sphk * much faster than the functions without "get" prefix and should 27395491Sphk * be used where a precision of 10 msec is acceptable or where 27495491Sphk * performance is priority. (NB: "precision", _not_ "resolution" !) 27595491Sphk * 27695491Sphk */ 27795491Sphk 27890362Sphkvoid binuptime(struct bintime *bt); 27995817Sphkvoid nanouptime(struct timespec *tsp); 28095817Sphkvoid microuptime(struct timeval *tvp); 28195491Sphk 28290362Sphkvoid bintime(struct bintime *bt); 28395817Sphkvoid nanotime(struct timespec *tsp); 28495817Sphkvoid microtime(struct timeval *tvp); 28595491Sphk 28695491Sphkvoid getbinuptime(struct bintime *bt); 28795491Sphkvoid getnanouptime(struct timespec *tsp); 28895817Sphkvoid getmicrouptime(struct timeval *tvp); 28995491Sphk 29095491Sphkvoid getbintime(struct bintime *bt); 29195491Sphkvoid getnanotime(struct timespec *tsp); 29295817Sphkvoid getmicrotime(struct timeval *tvp); 29395491Sphk 29495817Sphk/* Other functions */ 29592719Salfredint itimerdecr(struct itimerval *itp, int usec); 29692719Salfredint itimerfix(struct timeval *tv); 297108142Ssamint ppsratecheck(struct timeval *, int *, int); 298108142Ssamint ratecheck(struct timeval *, const struct timeval *); 29995817Sphkvoid timevaladd(struct timeval *t1, struct timeval *t2); 30095817Sphkvoid timevalsub(struct timeval *t1, struct timeval *t2); 30195817Sphkint tvtohz(struct timeval *tv); 30255205Speter#else /* !_KERNEL */ 3031541Srgrimes#include <time.h> 3041541Srgrimes 3051541Srgrimes#include <sys/cdefs.h> 3061541Srgrimes 3071541Srgrimes__BEGIN_DECLS 30892719Salfredint adjtime(const struct timeval *, struct timeval *); 30992719Salfredint futimes(int, const struct timeval *); 31092719Salfredint getitimer(int, struct itimerval *); 31192719Salfredint gettimeofday(struct timeval *, struct timezone *); 31292719Salfredint lutimes(const char *, const struct timeval *); 31392719Salfredint setitimer(int, const struct itimerval *, struct itimerval *); 31492719Salfredint settimeofday(const struct timeval *, const struct timezone *); 31592719Salfredint utimes(const char *, const struct timeval *); 3161541Srgrimes__END_DECLS 3171541Srgrimes 31855205Speter#endif /* !_KERNEL */ 3191541Srgrimes 3201541Srgrimes#endif /* !_SYS_TIME_H_ */ 321