time.h revision 108142
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 108142 2002-12-20 23:54:47Z sam $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#ifndef _SYS_TIME_H_ 381541Srgrimes#define _SYS_TIME_H_ 391541Srgrimes 4014487Shsu#include <sys/types.h> 4198270Swollman#include <sys/timespec.h> 4214487Shsu 431541Srgrimes/* 441541Srgrimes * Structure returned by gettimeofday(2) system call, 451541Srgrimes * and used in other calls. 461541Srgrimes */ 471541Srgrimesstruct timeval { 481541Srgrimes long tv_sec; /* seconds */ 491541Srgrimes long tv_usec; /* and microseconds */ 501541Srgrimes}; 511541Srgrimes 521541Srgrimesstruct timezone { 531541Srgrimes int tz_minuteswest; /* minutes west of Greenwich */ 541541Srgrimes int tz_dsttime; /* type of dst correction */ 551541Srgrimes}; 561541Srgrimes#define DST_NONE 0 /* not on dst */ 571541Srgrimes#define DST_USA 1 /* USA style dst */ 581541Srgrimes#define DST_AUST 2 /* Australian style dst */ 591541Srgrimes#define DST_WET 3 /* Western European dst */ 601541Srgrimes#define DST_MET 4 /* Middle European dst */ 611541Srgrimes#define DST_EET 5 /* Eastern European dst */ 621541Srgrimes#define DST_CAN 6 /* Canada */ 631541Srgrimes 6498270Swollman#if __BSD_VISIBLE 6590362Sphkstruct bintime { 6695817Sphk time_t sec; 6795817Sphk uint64_t frac; 6890362Sphk}; 6990362Sphk 7090362Sphkstatic __inline void 7192769Sphkbintime_addx(struct bintime *bt, uint64_t x) 7290362Sphk{ 7392769Sphk uint64_t u; 7490362Sphk 7590362Sphk u = bt->frac; 7690362Sphk bt->frac += x; 7790362Sphk if (u > bt->frac) 7890362Sphk bt->sec++; 7990362Sphk} 8090362Sphk 8190362Sphkstatic __inline void 8290362Sphkbintime_add(struct bintime *bt, struct bintime *bt2) 8390362Sphk{ 8492769Sphk uint64_t u; 8590362Sphk 8690362Sphk u = bt->frac; 8790362Sphk bt->frac += bt2->frac; 8890362Sphk if (u > bt->frac) 8990362Sphk bt->sec++; 9090362Sphk bt->sec += bt2->sec; 9190362Sphk} 9290362Sphk 9390362Sphkstatic __inline void 9490362Sphkbintime_sub(struct bintime *bt, struct bintime *bt2) 9590362Sphk{ 9692769Sphk uint64_t u; 9790362Sphk 9890362Sphk u = bt->frac; 9990362Sphk bt->frac -= bt2->frac; 10090362Sphk if (u < bt->frac) 10190362Sphk bt->sec--; 10290362Sphk bt->sec -= bt2->sec; 10390362Sphk} 10490362Sphk 10592769Sphk/*- 10692769Sphk * Background information: 10792769Sphk * 10892769Sphk * When converting between timestamps on parallel timescales of differing 10992769Sphk * resolutions it is historical and scientific practice to round down rather 11092769Sphk * than doing 4/5 rounding. 11192769Sphk * 11292769Sphk * The date changes at midnight, not at noon. 11392769Sphk * 11492769Sphk * Even at 15:59:59.999999999 it's not four'o'clock. 11592769Sphk * 11692769Sphk * time_second ticks after N.999999999 not after N.4999999999 11792769Sphk */ 11892769Sphk 11990362Sphkstatic __inline void 12090362Sphkbintime2timespec(struct bintime *bt, struct timespec *ts) 12190362Sphk{ 12290362Sphk 12390362Sphk ts->tv_sec = bt->sec; 12495817Sphk ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32; 12590362Sphk} 12690362Sphk 12790362Sphkstatic __inline void 12890362Sphktimespec2bintime(struct timespec *ts, struct bintime *bt) 12990362Sphk{ 13090362Sphk 13190362Sphk bt->sec = ts->tv_sec; 13292769Sphk /* 18446744073 = int(2^64 / 1000000000) */ 13392769Sphk bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 13490362Sphk} 13590362Sphk 13690362Sphkstatic __inline void 13790362Sphkbintime2timeval(struct bintime *bt, struct timeval *tv) 13890362Sphk{ 13990362Sphk 14090362Sphk tv->tv_sec = bt->sec; 14195817Sphk tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32; 14290362Sphk} 14390362Sphk 14490362Sphkstatic __inline void 14590362Sphktimeval2bintime(struct timeval *tv, struct bintime *bt) 14690362Sphk{ 14790362Sphk 14890362Sphk bt->sec = tv->tv_sec; 14992769Sphk /* 18446744073709 = int(2^64 / 1000000) */ 15092769Sphk bt->frac = tv->tv_usec * (uint64_t)18446744073709LL; 15190362Sphk} 15298270Swollman#endif /* __BSD_VISIBLE */ 15390362Sphk 15455205Speter#ifdef _KERNEL 15535058Sphk 15635029Sphk/* Operations on timespecs */ 15735401Seivind#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) 15835029Sphk#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) 15935029Sphk#define timespeccmp(tvp, uvp, cmp) \ 16035029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 16135029Sphk ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 16235029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 16335058Sphk#define timespecadd(vvp, uvp) \ 16435029Sphk do { \ 16535058Sphk (vvp)->tv_sec += (uvp)->tv_sec; \ 16635058Sphk (vvp)->tv_nsec += (uvp)->tv_nsec; \ 16735029Sphk if ((vvp)->tv_nsec >= 1000000000) { \ 16835029Sphk (vvp)->tv_sec++; \ 16935029Sphk (vvp)->tv_nsec -= 1000000000; \ 17035029Sphk } \ 17135029Sphk } while (0) 17235058Sphk#define timespecsub(vvp, uvp) \ 17335029Sphk do { \ 17435058Sphk (vvp)->tv_sec -= (uvp)->tv_sec; \ 17535058Sphk (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 17635029Sphk if ((vvp)->tv_nsec < 0) { \ 17735029Sphk (vvp)->tv_sec--; \ 17835029Sphk (vvp)->tv_nsec += 1000000000; \ 17935029Sphk } \ 18035029Sphk } while (0) 18135058Sphk 1821541Srgrimes/* Operations on timevals. */ 18335058Sphk 18474574Smarkm#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 18535058Sphk#define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 18635029Sphk#define timevalcmp(tvp, uvp, cmp) \ 18735029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 18835029Sphk ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 18935029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 19035058Sphk 19135058Sphk/* timevaladd and timevalsub are not inlined */ 19235058Sphk 19355205Speter#endif /* _KERNEL */ 19435029Sphk 19572093Sasmodai#ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ 19635058Sphk 19774574Smarkm#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 1981541Srgrimes#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 19935058Sphk#define timercmp(tvp, uvp, cmp) \ 2001541Srgrimes (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 2011541Srgrimes ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 2021541Srgrimes ((tvp)->tv_sec cmp (uvp)->tv_sec)) 20321099Speter#define timeradd(tvp, uvp, vvp) \ 20421099Speter do { \ 20521099Speter (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 20621099Speter (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 20721099Speter if ((vvp)->tv_usec >= 1000000) { \ 20821099Speter (vvp)->tv_sec++; \ 20921099Speter (vvp)->tv_usec -= 1000000; \ 21021099Speter } \ 21121099Speter } while (0) 21221099Speter#define timersub(tvp, uvp, vvp) \ 21321099Speter do { \ 21421099Speter (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 21521099Speter (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 21621099Speter if ((vvp)->tv_usec < 0) { \ 21721099Speter (vvp)->tv_sec--; \ 21821099Speter (vvp)->tv_usec += 1000000; \ 21921099Speter } \ 22021099Speter } while (0) 22121099Speter#endif 2221541Srgrimes 2231541Srgrimes/* 2241541Srgrimes * Names of the interval timers, and structure 2251541Srgrimes * defining a timer setting. 2261541Srgrimes */ 2271541Srgrimes#define ITIMER_REAL 0 2281541Srgrimes#define ITIMER_VIRTUAL 1 2291541Srgrimes#define ITIMER_PROF 2 2301541Srgrimes 23183045Sobrienstruct itimerval { 2321541Srgrimes struct timeval it_interval; /* timer interval */ 2331541Srgrimes struct timeval it_value; /* current value */ 2341541Srgrimes}; 2351541Srgrimes 2361541Srgrimes/* 2371541Srgrimes * Getkerninfo clock information structure 2381541Srgrimes */ 2391541Srgrimesstruct clockinfo { 2401541Srgrimes int hz; /* clock frequency */ 2411541Srgrimes int tick; /* micro-seconds per hz tick */ 24296052Sbde int spare; 2431541Srgrimes int stathz; /* statistics clock frequency */ 2441541Srgrimes int profhz; /* profiling clock frequency */ 2451541Srgrimes}; 2461541Srgrimes 24734030Sdufault/* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */ 24834030Sdufault 24934030Sdufault#ifndef CLOCK_REALTIME 25025578Speter#define CLOCK_REALTIME 0 25134030Sdufault#endif 25225578Speter#define CLOCK_VIRTUAL 1 25325578Speter#define CLOCK_PROF 2 25425578Speter 25525578Speter#define TIMER_RELTIME 0x0 /* relative timer */ 25634030Sdufault#ifndef TIMER_ABSTIME 25725578Speter#define TIMER_ABSTIME 0x1 /* absolute timer */ 25834030Sdufault#endif 25925578Speter 26055205Speter#ifdef _KERNEL 26134961Sphkextern time_t time_second; 262106304Sphkextern time_t time_uptime; 26333690Sphk 26495817Sphk/* 26595491Sphk * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() 26695491Sphk * 26795491Sphk * Functions without the "get" prefix returns the best timestamp 26895491Sphk * we can produce in the given format. 26995491Sphk * 27095491Sphk * "bin" == struct bintime == seconds + 64 bit fraction of seconds. 27195491Sphk * "nano" == struct timespec == seconds + nanoseconds. 27295491Sphk * "micro" == struct timeval == seconds + microseconds. 27395491Sphk * 27495491Sphk * Functions containing "up" returns time relative to boot and 27595491Sphk * should be used for calculating time intervals. 27695491Sphk * 27795491Sphk * Functions without "up" returns GMT time. 27895491Sphk * 27995491Sphk * Functions with the "get" prefix returns a less precise result 28095491Sphk * much faster than the functions without "get" prefix and should 28195491Sphk * be used where a precision of 10 msec is acceptable or where 28295491Sphk * performance is priority. (NB: "precision", _not_ "resolution" !) 28395491Sphk * 28495491Sphk */ 28595491Sphk 28690362Sphkvoid binuptime(struct bintime *bt); 28795817Sphkvoid nanouptime(struct timespec *tsp); 28895817Sphkvoid microuptime(struct timeval *tvp); 28995491Sphk 29090362Sphkvoid bintime(struct bintime *bt); 29195817Sphkvoid nanotime(struct timespec *tsp); 29295817Sphkvoid microtime(struct timeval *tvp); 29395491Sphk 29495491Sphkvoid getbinuptime(struct bintime *bt); 29595491Sphkvoid getnanouptime(struct timespec *tsp); 29695817Sphkvoid getmicrouptime(struct timeval *tvp); 29795491Sphk 29895491Sphkvoid getbintime(struct bintime *bt); 29995491Sphkvoid getnanotime(struct timespec *tsp); 30095817Sphkvoid getmicrotime(struct timeval *tvp); 30195491Sphk 30295817Sphk/* Other functions */ 30392719Salfredint itimerdecr(struct itimerval *itp, int usec); 30492719Salfredint itimerfix(struct timeval *tv); 305108142Ssamint ppsratecheck(struct timeval *, int *, int); 306108142Ssamint ratecheck(struct timeval *, const struct timeval *); 30795817Sphkvoid timevaladd(struct timeval *t1, struct timeval *t2); 30895817Sphkvoid timevalsub(struct timeval *t1, struct timeval *t2); 30995817Sphkint tvtohz(struct timeval *tv); 31055205Speter#else /* !_KERNEL */ 3111541Srgrimes#include <time.h> 3121541Srgrimes 3131541Srgrimes#include <sys/cdefs.h> 3141541Srgrimes 3151541Srgrimes__BEGIN_DECLS 31692719Salfredint adjtime(const struct timeval *, struct timeval *); 31792719Salfredint futimes(int, const struct timeval *); 31892719Salfredint getitimer(int, struct itimerval *); 31992719Salfredint gettimeofday(struct timeval *, struct timezone *); 32092719Salfredint lutimes(const char *, const struct timeval *); 32192719Salfredint setitimer(int, const struct itimerval *, struct itimerval *); 32292719Salfredint settimeofday(const struct timeval *, const struct timezone *); 32392719Salfredint utimes(const char *, const struct timeval *); 3241541Srgrimes__END_DECLS 3251541Srgrimes 32655205Speter#endif /* !_KERNEL */ 3271541Srgrimes 3281541Srgrimes#endif /* !_SYS_TIME_H_ */ 329