home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Include / Linux / time.h < prev    next >
C/C++ Source or Header  |  2002-04-26  |  4KB  |  134 lines

  1. /* $Id: time.h,v 1.2 2002/04/26 23:09:15 smilcke Exp $ */
  2.  
  3. #ifndef _LINUX_TIME_H
  4. #define _LINUX_TIME_H
  5.  
  6. #include <asm/param.h>
  7. #include <linux/types.h>
  8.  
  9. #ifndef _STRUCT_TIMESPEC
  10. #define _STRUCT_TIMESPEC
  11. struct timespec {
  12.     time_t    tv_sec;        /* seconds */
  13.     long    tv_nsec;    /* nanoseconds */
  14. };
  15. #endif /* _STRUCT_TIMESPEC */
  16.  
  17. #ifdef __KERNEL__
  18.  
  19. /*
  20.  * Change timeval to jiffies, trying to avoid the
  21.  * most obvious overflows..
  22.  *
  23.  * And some not so obvious.
  24.  *
  25.  * Note that we don't want to return MAX_LONG, because
  26.  * for various timeout reasons we often end up having
  27.  * to wait "jiffies+1" in order to guarantee that we wait
  28.  * at _least_ "jiffies" - so "jiffies+1" had better still
  29.  * be positive.
  30.  */
  31. #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
  32.  
  33. static __inline__ unsigned long
  34. timespec_to_jiffies(struct timespec *value)
  35. {
  36.     unsigned long sec = value->tv_sec;
  37.     long nsec = value->tv_nsec;
  38.  
  39.     if (sec >= (MAX_JIFFY_OFFSET / HZ))
  40.         return MAX_JIFFY_OFFSET;
  41.     nsec += 1000000000L / HZ - 1;
  42.     nsec /= 1000000000L / HZ;
  43.     return HZ * sec + nsec;
  44. }
  45.  
  46. static __inline__ void
  47. jiffies_to_timespec(unsigned long myjiffies, struct timespec *value)
  48. {
  49.     value->tv_nsec = (myjiffies % HZ) * (1000000000L / HZ);
  50.     value->tv_sec = myjiffies / HZ;
  51. }
  52.  
  53.  
  54. /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  55.  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  56.  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  57.  *
  58.  * [For the Julian calendar (which was used in Russia before 1917,
  59.  * Britain & colonies before 1752, anywhere else before 1582,
  60.  * and is still in use by some communities) leave out the
  61.  * -year/100+year/400 terms, and add 10.]
  62.  *
  63.  * This algorithm was first published by Gauss (I think).
  64.  *
  65.  * WARNING: this function will overflow on 2106-02-07 06:28:16 on
  66.  * machines were long is 32-bit! (However, as time_t is signed, we
  67.  * will already get problems at other places on 2038-01-19 03:14:08)
  68.  */
  69. static __inline__ unsigned long
  70. mktime (unsigned int year, unsigned int mon,
  71.     unsigned int day, unsigned int hour,
  72.     unsigned int min, unsigned int sec)
  73. {
  74.     if (0 >= (int) (mon -= 2)) {    /* 1..12 -> 11,12,1..10 */
  75.         mon += 12;        /* Puts Feb last since it has leap day */
  76.         year -= 1;
  77.     }
  78.  
  79.     return (((
  80.         (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  81.             year*365 - 719499
  82.         )*24 + hour /* now have hours */
  83.       )*60 + min /* now have minutes */
  84.     )*60 + sec; /* finally seconds */
  85. }
  86.  
  87. #endif /* __KERNEL__ */
  88.  
  89.  
  90. struct timeval {
  91.     time_t        tv_sec;        /* seconds */
  92.     suseconds_t    tv_usec;    /* microseconds */
  93. };
  94.  
  95. struct timezone {
  96.     int    tz_minuteswest;    /* minutes west of Greenwich */
  97.     int    tz_dsttime;    /* type of dst correction */
  98. };
  99.  
  100. #define NFDBITS            __NFDBITS
  101.  
  102. #ifdef __KERNEL__
  103. extern void do_gettimeofday(struct timeval *tv);
  104. extern void do_settimeofday(struct timeval *tv);
  105. extern void get_fast_time(struct timeval *tv);
  106. extern void (*do_get_fast_time)(struct timeval *);
  107. #endif
  108.  
  109. #define FD_SETSIZE        __FD_SETSIZE
  110. #define FD_SET(fd,fdsetp)    __FD_SET(fd,fdsetp)
  111. #define FD_CLR(fd,fdsetp)    __FD_CLR(fd,fdsetp)
  112. #define FD_ISSET(fd,fdsetp)    __FD_ISSET(fd,fdsetp)
  113. #define FD_ZERO(fdsetp)        __FD_ZERO(fdsetp)
  114.  
  115. /*
  116.  * Names of the interval timers, and structure
  117.  * defining a timer setting.
  118.  */
  119. #define    ITIMER_REAL    0
  120. #define    ITIMER_VIRTUAL    1
  121. #define    ITIMER_PROF    2
  122.  
  123. struct  itimerspec {
  124.         struct  timespec it_interval;    /* timer period */
  125.         struct  timespec it_value;       /* timer expiration */
  126. };
  127.  
  128. struct    itimerval {
  129.     struct    timeval it_interval;    /* timer interval */
  130.     struct    timeval it_value;    /* current value */
  131. };
  132.  
  133. #endif
  134.