2  * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * The contents of this file constitute Original Code as defined in and 
   7  * are subject to the Apple Public Source License Version 1.1 (the 
   8  * "License").  You may not use this file except in compliance with the 
   9  * License.  Please obtain a copy of the License at 
  10  * http://www.apple.com/publicsource and read it before using this file. 
  12  * This Original Code and all software distributed under the License are 
  13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the 
  17  * License for the specific language governing rights and limitations 
  20  * @APPLE_LICENSE_HEADER_END@ 
  22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ 
  24  * Copyright (c) 1982, 1986, 1993 
  25  *      The Regents of the University of California.  All rights reserved. 
  27  * Redistribution and use in source and binary forms, with or without 
  28  * modification, are permitted provided that the following conditions 
  30  * 1. Redistributions of source code must retain the above copyright 
  31  *    notice, this list of conditions and the following disclaimer. 
  32  * 2. Redistributions in binary form must reproduce the above copyright 
  33  *    notice, this list of conditions and the following disclaimer in the 
  34  *    documentation and/or other materials provided with the distribution. 
  35  * 3. All advertising materials mentioning features or use of this software 
  36  *    must display the following acknowledgement: 
  37  *      This product includes software developed by the University of 
  38  *      California, Berkeley and its contributors. 
  39  * 4. Neither the name of the University nor the names of its contributors 
  40  *    may be used to endorse or promote products derived from this software 
  41  *    without specific prior written permission. 
  43  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  55  *      @(#)time.h      8.2 (Berkeley) 7/10/94 
  61 #include <sys/cdefs.h> 
  62 #include <sys/_types.h> 
  66 typedef __darwin_time_t 
time_t; 
  71 typedef __darwin_suseconds_t    suseconds_t
; 
  76  * Structure returned by gettimeofday(2) system call, 
  77  * and used in other calls. 
  82         time_t          tv_sec
;         /* seconds */ 
  83         suseconds_t     tv_usec
;        /* and microseconds */ 
  88  * Structure used as a parameter by getitimer(2) and setitimer(2) system 
  92         struct  timeval it_interval
;    /* timer interval */ 
  93         struct  timeval it_value
;       /* current value */ 
  97  * Names of the interval timers, and structure 
  98  * defining a timer setting. 
 100 #define ITIMER_REAL     0 
 101 #define ITIMER_VIRTUAL  1 
 102 #define ITIMER_PROF     2 
 106  * [XSI] The fd_set type shall be defined as described in <sys/select.h>. 
 108  * Note:        We use _FD_SET to protect all select related 
 115  * Select uses bit masks of file descriptors in longs.  These macros 
 116  * manipulate such bit fields (the filesystem macros use chars).  The 
 117  * extra protection here is to permit application redefinition above 
 121 #define FD_SETSIZE      1024 
 124 #define __DARWIN_NBBY   8                               /* bits in a byte */ 
 125 #define __DARWIN_NFDBITS        (sizeof(__int32_t) * __DARWIN_NBBY) /* bits per mask */ 
 126 #define __DARWIN_howmany(x, y) (((x) + ((y) - 1)) / (y))        /* # y's == x bits? */ 
 129 typedef struct fd_set 
{ 
 130         __int32_t       fds_bits
[__DARWIN_howmany(FD_SETSIZE
, __DARWIN_NFDBITS
)]; 
 134 #define FD_SET(n, p)    ((p)->fds_bits[(n)/__DARWIN_NFDBITS] |= (1<<((n) % __DARWIN_NFDBITS))) 
 135 #define FD_CLR(n, p)    ((p)->fds_bits[(n)/__DARWIN_NFDBITS] &= ~(1<<((n) % __DARWIN_NFDBITS))) 
 136 #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/__DARWIN_NFDBITS] & (1<<((n) % __DARWIN_NFDBITS))) 
 137 #if __GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3 
 139  * Use the built-in bzero function instead of the library version so that 
 140  * we do not pollute the namespace or introduce prototype warnings. 
 142 #define FD_ZERO(p)      __builtin_bzero(p, sizeof(*(p))) 
 144 #define FD_ZERO(p)      bzero(p, sizeof(*(p))) 
 146 #ifndef _POSIX_C_SOURCE 
 147 #define FD_COPY(f, t)   bcopy(f, t, sizeof(*(f))) 
 148 #endif  /* !_POSIX_C_SOURCE */ 
 150 #endif  /* !_FD_SET */ 
 153 #ifndef _POSIX_C_SOURCE 
 155  * Structure defined by POSIX.4 to be like a timeval. 
 160         time_t  tv_sec
;         /* seconds */ 
 161         long    tv_nsec
;        /* and nanoseconds */ 
 165 // LP64todo - should this move? 
 166 #include <machine/types.h>      /* user_time_t */ 
 168 /* LP64 version of struct timespec.  time_t is a long and must grow when  
 169  * we're dealing with a 64-bit process. 
 170  * WARNING - keep in sync with struct timespec 
 172 struct user_timespec 
{ 
 173         user_time_t     tv_sec
;         /* seconds */ 
 174         int32_t tv_nsec 
__attribute((aligned(8)));      /* and nanoseconds */ 
 185 #include <machine/types.h>      /* user_time_t */ 
 187  * LP64 version of struct timeval.  time_t is a long and must grow when  
 188  * we're dealing with a 64-bit process. 
 189  * WARNING - keep in sync with struct timeval 
 192 struct user_timeval 
{ 
 193         user_time_t     tv_sec
;         /* seconds */ 
 194         suseconds_t     tv_usec 
__attribute((aligned(8)));      /* and microseconds */ 
 197 struct  user_itimerval 
{ 
 198         struct  user_timeval it_interval
;       /* timer interval */ 
 199         struct  user_timeval it_value
;          /* current value */ 
 206 #define TIMEVAL_TO_TIMESPEC(tv, ts) {                                   \ 
 207         (ts)->tv_sec = (tv)->tv_sec;                                    \ 
 208         (ts)->tv_nsec = (tv)->tv_usec * 1000;                           \ 
 210 #define TIMESPEC_TO_TIMEVAL(tv, ts) {                                   \ 
 211         (tv)->tv_sec = (ts)->tv_sec;                                    \ 
 212         (tv)->tv_usec = (ts)->tv_nsec / 1000;                           \ 
 216         int     tz_minuteswest
; /* minutes west of Greenwich */ 
 217         int     tz_dsttime
;     /* type of dst correction */ 
 219 #define DST_NONE        0       /* not on dst */ 
 220 #define DST_USA         1       /* USA style dst */ 
 221 #define DST_AUST        2       /* Australian style dst */ 
 222 #define DST_WET         3       /* Western European dst */ 
 223 #define DST_MET         4       /* Middle European dst */ 
 224 #define DST_EET         5       /* Eastern European dst */ 
 225 #define DST_CAN         6       /* Canada */ 
 227 /* Operations on timevals. */ 
 228 #define timerclear(tvp)         (tvp)->tv_sec = (tvp)->tv_usec = 0 
 229 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec) 
 230 #define timercmp(tvp, uvp, cmp)                                         \ 
 231         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \ 
 232             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \ 
 233             ((tvp)->tv_sec cmp (uvp)->tv_sec)) 
 234 #define timeradd(tvp, uvp, vvp)                                         \ 
 236                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \ 
 237                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \ 
 238                 if ((vvp)->tv_usec >= 1000000) {                        \ 
 240                         (vvp)->tv_usec -= 1000000;                      \ 
 243 #define timersub(tvp, uvp, vvp)                                         \ 
 245                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \ 
 246                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \ 
 247                 if ((vvp)->tv_usec < 0) {                               \ 
 249                         (vvp)->tv_usec += 1000000;                      \ 
 253 #define timevalcmp(l, r, cmp)   timercmp(l, r, cmp) /* freebsd */ 
 256  * Getkerninfo clock information structure 
 259         int     hz
;             /* clock frequency */ 
 260         int     tick
;           /* micro-seconds per hz tick */ 
 261         int     tickadj
;        /* clock skew rate for adjtime() */ 
 262         int     stathz
;         /* statistics clock frequency */ 
 263         int     profhz
;         /* profiling clock frequency */ 
 265 #endif /* ! _POSIX_C_SOURCE */ 
 270 #ifndef _POSIX_C_SOURCE 
 272 void    microtime(struct timeval 
*tv
); 
 273 void    microuptime(struct timeval 
*tv
); 
 274 #define getmicrotime(a)         microtime(a) 
 275 #define getmicrouptime(a)       microuptime(a) 
 276 void    nanotime(struct timespec 
*ts
); 
 277 void    nanouptime(struct timespec 
*ts
); 
 278 #define getnanotime(a)          nanotime(a) 
 279 #define getnanouptime(a)        nanouptime(a) 
 280 void    timevaladd(struct timeval 
*t1
, struct timeval 
*t2
); 
 281 void    timevalsub(struct timeval 
*t1
, struct timeval 
*t2
); 
 282 void    timevalfix(struct timeval 
*t1
); 
 283 #ifdef  BSD_KERNEL_PRIVATE 
 284 time_t  boottime_sec(void); 
 285 void    inittodr(time_t base
); 
 286 int     itimerfix(struct timeval 
*tv
); 
 287 int     itimerdecr(struct itimerval 
*itp
, int usec
); 
 288 #endif /* BSD_KERNEL_PRIVATE */ 
 292 #endif /* ! _POSIX_C_SOURCE */ 
 298 #ifndef _POSIX_C_SOURCE 
 301 int     adjtime(const struct timeval 
*, struct timeval 
*); 
 302 int     futimes(int, const struct timeval 
*); 
 303 int     settimeofday(const struct timeval 
*, const struct timezone 
*); 
 304 #endif /* ! _POSIX_C_SOURCE */ 
 306 int     getitimer(int, struct itimerval 
*); 
 307 int     gettimeofday(struct timeval 
* __restrict
, struct timezone 
* __restrict
); 
 308 int     select(int, fd_set 
* __restrict
, fd_set 
* __restrict
, 
 309                 fd_set 
* __restrict
, struct timeval 
* __restrict
); 
 310 int     setitimer(int, const struct itimerval 
* __restrict
, 
 311                 struct itimerval 
* __restrict
); 
 312 int     utimes(const char *, const struct timeval 
*); 
 318 #endif /* !_SYS_TIME_H_ */