time.h revision 209216
1139825Simp/*- 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 * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 2914487Shsu * @(#)time.h 8.5 (Berkeley) 5/4/95 3050477Speter * $FreeBSD: head/sys/sys/time.h 209216 2010-06-15 19:28:44Z jkim $ 311541Srgrimes */ 321541Srgrimes 331541Srgrimes#ifndef _SYS_TIME_H_ 341541Srgrimes#define _SYS_TIME_H_ 351541Srgrimes 36108477Smike#include <sys/_timeval.h> 3714487Shsu#include <sys/types.h> 3898270Swollman#include <sys/timespec.h> 3914487Shsu 401541Srgrimesstruct timezone { 411541Srgrimes int tz_minuteswest; /* minutes west of Greenwich */ 421541Srgrimes int tz_dsttime; /* type of dst correction */ 431541Srgrimes}; 441541Srgrimes#define DST_NONE 0 /* not on dst */ 451541Srgrimes#define DST_USA 1 /* USA style dst */ 461541Srgrimes#define DST_AUST 2 /* Australian style dst */ 471541Srgrimes#define DST_WET 3 /* Western European dst */ 481541Srgrimes#define DST_MET 4 /* Middle European dst */ 491541Srgrimes#define DST_EET 5 /* Eastern European dst */ 501541Srgrimes#define DST_CAN 6 /* Canada */ 511541Srgrimes 5298270Swollman#if __BSD_VISIBLE 5390362Sphkstruct bintime { 5495817Sphk time_t sec; 5595817Sphk uint64_t frac; 5690362Sphk}; 5790362Sphk 5890362Sphkstatic __inline void 5992769Sphkbintime_addx(struct bintime *bt, uint64_t x) 6090362Sphk{ 6192769Sphk uint64_t u; 6290362Sphk 6390362Sphk u = bt->frac; 6490362Sphk bt->frac += x; 6590362Sphk if (u > bt->frac) 6690362Sphk bt->sec++; 6790362Sphk} 6890362Sphk 6990362Sphkstatic __inline void 70121524Salfredbintime_add(struct bintime *bt, const struct bintime *bt2) 7190362Sphk{ 7292769Sphk uint64_t u; 7390362Sphk 7490362Sphk u = bt->frac; 7590362Sphk bt->frac += bt2->frac; 7690362Sphk if (u > bt->frac) 7790362Sphk bt->sec++; 7890362Sphk bt->sec += bt2->sec; 7990362Sphk} 8090362Sphk 8190362Sphkstatic __inline void 82121524Salfredbintime_sub(struct bintime *bt, const 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 9392769Sphk/*- 9492769Sphk * Background information: 9592769Sphk * 9692769Sphk * When converting between timestamps on parallel timescales of differing 9792769Sphk * resolutions it is historical and scientific practice to round down rather 9892769Sphk * than doing 4/5 rounding. 9992769Sphk * 10092769Sphk * The date changes at midnight, not at noon. 10192769Sphk * 10292769Sphk * Even at 15:59:59.999999999 it's not four'o'clock. 10392769Sphk * 10492769Sphk * time_second ticks after N.999999999 not after N.4999999999 10592769Sphk */ 10692769Sphk 10790362Sphkstatic __inline void 108121524Salfredbintime2timespec(const struct bintime *bt, struct timespec *ts) 10990362Sphk{ 11090362Sphk 11190362Sphk ts->tv_sec = bt->sec; 11295817Sphk ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32; 11390362Sphk} 11490362Sphk 11590362Sphkstatic __inline void 116121524Salfredtimespec2bintime(const struct timespec *ts, struct bintime *bt) 11790362Sphk{ 11890362Sphk 11990362Sphk bt->sec = ts->tv_sec; 12092769Sphk /* 18446744073 = int(2^64 / 1000000000) */ 12192769Sphk bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 12290362Sphk} 12390362Sphk 12490362Sphkstatic __inline void 125121524Salfredbintime2timeval(const struct bintime *bt, struct timeval *tv) 12690362Sphk{ 12790362Sphk 12890362Sphk tv->tv_sec = bt->sec; 12995817Sphk tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32; 13090362Sphk} 13190362Sphk 13290362Sphkstatic __inline void 133121524Salfredtimeval2bintime(const struct timeval *tv, struct bintime *bt) 13490362Sphk{ 13590362Sphk 13690362Sphk bt->sec = tv->tv_sec; 13792769Sphk /* 18446744073709 = int(2^64 / 1000000) */ 13892769Sphk bt->frac = tv->tv_usec * (uint64_t)18446744073709LL; 13990362Sphk} 14098270Swollman#endif /* __BSD_VISIBLE */ 14190362Sphk 14255205Speter#ifdef _KERNEL 14335058Sphk 14435029Sphk/* Operations on timespecs */ 14535401Seivind#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) 14635029Sphk#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) 14735029Sphk#define timespeccmp(tvp, uvp, cmp) \ 14835029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 14935029Sphk ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 15035029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 15135058Sphk#define timespecadd(vvp, uvp) \ 15235029Sphk do { \ 15335058Sphk (vvp)->tv_sec += (uvp)->tv_sec; \ 15435058Sphk (vvp)->tv_nsec += (uvp)->tv_nsec; \ 15535029Sphk if ((vvp)->tv_nsec >= 1000000000) { \ 15635029Sphk (vvp)->tv_sec++; \ 15735029Sphk (vvp)->tv_nsec -= 1000000000; \ 15835029Sphk } \ 15935029Sphk } while (0) 16035058Sphk#define timespecsub(vvp, uvp) \ 16135029Sphk do { \ 16235058Sphk (vvp)->tv_sec -= (uvp)->tv_sec; \ 16335058Sphk (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 16435029Sphk if ((vvp)->tv_nsec < 0) { \ 16535029Sphk (vvp)->tv_sec--; \ 16635029Sphk (vvp)->tv_nsec += 1000000000; \ 16735029Sphk } \ 16835029Sphk } while (0) 16935058Sphk 1701541Srgrimes/* Operations on timevals. */ 17135058Sphk 17274574Smarkm#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 17335058Sphk#define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 17435029Sphk#define timevalcmp(tvp, uvp, cmp) \ 17535029Sphk (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 17635029Sphk ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 17735029Sphk ((tvp)->tv_sec cmp (uvp)->tv_sec)) 17835058Sphk 17935058Sphk/* timevaladd and timevalsub are not inlined */ 18035058Sphk 18155205Speter#endif /* _KERNEL */ 18235029Sphk 18372093Sasmodai#ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ 18435058Sphk 18574574Smarkm#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 1861541Srgrimes#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 18735058Sphk#define timercmp(tvp, uvp, cmp) \ 1881541Srgrimes (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 1891541Srgrimes ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 1901541Srgrimes ((tvp)->tv_sec cmp (uvp)->tv_sec)) 19121099Speter#define timeradd(tvp, uvp, vvp) \ 19221099Speter do { \ 19321099Speter (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 19421099Speter (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 19521099Speter if ((vvp)->tv_usec >= 1000000) { \ 19621099Speter (vvp)->tv_sec++; \ 19721099Speter (vvp)->tv_usec -= 1000000; \ 19821099Speter } \ 19921099Speter } while (0) 20021099Speter#define timersub(tvp, uvp, vvp) \ 20121099Speter do { \ 20221099Speter (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 20321099Speter (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 20421099Speter if ((vvp)->tv_usec < 0) { \ 20521099Speter (vvp)->tv_sec--; \ 20621099Speter (vvp)->tv_usec += 1000000; \ 20721099Speter } \ 20821099Speter } while (0) 20921099Speter#endif 2101541Srgrimes 2111541Srgrimes/* 2121541Srgrimes * Names of the interval timers, and structure 2131541Srgrimes * defining a timer setting. 2141541Srgrimes */ 2151541Srgrimes#define ITIMER_REAL 0 2161541Srgrimes#define ITIMER_VIRTUAL 1 2171541Srgrimes#define ITIMER_PROF 2 2181541Srgrimes 21983045Sobrienstruct itimerval { 2201541Srgrimes struct timeval it_interval; /* timer interval */ 2211541Srgrimes struct timeval it_value; /* current value */ 2221541Srgrimes}; 2231541Srgrimes 2241541Srgrimes/* 2251541Srgrimes * Getkerninfo clock information structure 2261541Srgrimes */ 2271541Srgrimesstruct clockinfo { 2281541Srgrimes int hz; /* clock frequency */ 2291541Srgrimes int tick; /* micro-seconds per hz tick */ 23096052Sbde int spare; 2311541Srgrimes int stathz; /* statistics clock frequency */ 2321541Srgrimes int profhz; /* profiling clock frequency */ 2331541Srgrimes}; 2341541Srgrimes 235144529Sdas/* These macros are also in time.h. */ 23634030Sdufault#ifndef CLOCK_REALTIME 23725578Speter#define CLOCK_REALTIME 0 23825578Speter#define CLOCK_VIRTUAL 1 23925578Speter#define CLOCK_PROF 2 240111300Sphk#define CLOCK_MONOTONIC 4 241152844Srwatson#define CLOCK_UPTIME 5 /* FreeBSD-specific. */ 242152844Srwatson#define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */ 243152844Srwatson#define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */ 244152844Srwatson#define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */ 245152844Srwatson#define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */ 246152844Srwatson#define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */ 247152844Srwatson#define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */ 248152844Srwatson#define CLOCK_SECOND 13 /* FreeBSD-specific. */ 249175429Sdavidxu#define CLOCK_THREAD_CPUTIME_ID 14 250144529Sdas#endif 25125578Speter 252144529Sdas#ifndef TIMER_ABSTIME 25325578Speter#define TIMER_RELTIME 0x0 /* relative timer */ 25425578Speter#define TIMER_ABSTIME 0x1 /* absolute timer */ 25534030Sdufault#endif 25625578Speter 25755205Speter#ifdef _KERNEL 258178429Sphk 259178429Sphk/* 260178429Sphk * Kernel to clock driver interface. 261178429Sphk */ 262178429Sphkvoid inittodr(time_t base); 263178429Sphkvoid resettodr(void); 264178429Sphk 26534961Sphkextern time_t time_second; 266106304Sphkextern time_t time_uptime; 267209216Sjkimextern struct bintime boottimebin; 268126401Sphkextern struct timeval boottime; 26933690Sphk 27095817Sphk/* 27195491Sphk * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() 27295491Sphk * 27395491Sphk * Functions without the "get" prefix returns the best timestamp 27495491Sphk * we can produce in the given format. 27595491Sphk * 27695491Sphk * "bin" == struct bintime == seconds + 64 bit fraction of seconds. 27795491Sphk * "nano" == struct timespec == seconds + nanoseconds. 27895491Sphk * "micro" == struct timeval == seconds + microseconds. 27995491Sphk * 28095491Sphk * Functions containing "up" returns time relative to boot and 28195491Sphk * should be used for calculating time intervals. 28295491Sphk * 28395491Sphk * Functions without "up" returns GMT time. 28495491Sphk * 28595491Sphk * Functions with the "get" prefix returns a less precise result 28695491Sphk * much faster than the functions without "get" prefix and should 287198570Sru * be used where a precision of 1/hz seconds is acceptable or where 28895491Sphk * performance is priority. (NB: "precision", _not_ "resolution" !) 28995491Sphk * 29095491Sphk */ 29195491Sphk 29290362Sphkvoid binuptime(struct bintime *bt); 29395817Sphkvoid nanouptime(struct timespec *tsp); 29495817Sphkvoid microuptime(struct timeval *tvp); 29595491Sphk 29690362Sphkvoid bintime(struct bintime *bt); 29795817Sphkvoid nanotime(struct timespec *tsp); 29895817Sphkvoid microtime(struct timeval *tvp); 29995491Sphk 30095491Sphkvoid getbinuptime(struct bintime *bt); 30195491Sphkvoid getnanouptime(struct timespec *tsp); 30295817Sphkvoid getmicrouptime(struct timeval *tvp); 30395491Sphk 30495491Sphkvoid getbintime(struct bintime *bt); 30595491Sphkvoid getnanotime(struct timespec *tsp); 30695817Sphkvoid getmicrotime(struct timeval *tvp); 30795491Sphk 30895817Sphk/* Other functions */ 30992719Salfredint itimerdecr(struct itimerval *itp, int usec); 31092719Salfredint itimerfix(struct timeval *tv); 311108142Ssamint ppsratecheck(struct timeval *, int *, int); 312108142Ssamint ratecheck(struct timeval *, const struct timeval *); 313121523Salfredvoid timevaladd(struct timeval *t1, const struct timeval *t2); 314121523Salfredvoid timevalsub(struct timeval *t1, const struct timeval *t2); 31595817Sphkint tvtohz(struct timeval *tv); 31655205Speter#else /* !_KERNEL */ 3171541Srgrimes#include <time.h> 3181541Srgrimes 3191541Srgrimes#include <sys/cdefs.h> 320189821Sdas#include <sys/select.h> 3211541Srgrimes 3221541Srgrimes__BEGIN_DECLS 323189821Sdasint setitimer(int, const struct itimerval *, struct itimerval *); 324189821Sdasint utimes(const char *, const struct timeval *); 325189821Sdas 326189821Sdas#if __BSD_VISIBLE 32792719Salfredint adjtime(const struct timeval *, struct timeval *); 32892719Salfredint futimes(int, const struct timeval *); 329189821Sdasint futimesat(int, const char *, const struct timeval [2]); 330189821Sdasint lutimes(const char *, const struct timeval *); 331189821Sdasint settimeofday(const struct timeval *, const struct timezone *); 332189821Sdas#endif 333189821Sdas 334189821Sdas#if __XSI_VISIBLE 33592719Salfredint getitimer(int, struct itimerval *); 33692719Salfredint gettimeofday(struct timeval *, struct timezone *); 337189821Sdas#endif 338189821Sdas 3391541Srgrimes__END_DECLS 3401541Srgrimes 34155205Speter#endif /* !_KERNEL */ 3421541Srgrimes 3431541Srgrimes#endif /* !_SYS_TIME_H_ */ 344