time.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifndef _LINUX_TIME_H
  2. #define _LINUX_TIME_H
  3. #include <linux/types.h>
  4. #ifdef __KERNEL__
  5. #include <linux/seqlock.h>
  6. #endif
  7. #ifndef _STRUCT_TIMESPEC
  8. #define _STRUCT_TIMESPEC
  9. struct timespec {
  10. time_t tv_sec; /* seconds */
  11. long tv_nsec; /* nanoseconds */
  12. };
  13. #endif /* _STRUCT_TIMESPEC */
  14. struct timeval {
  15. time_t tv_sec; /* seconds */
  16. suseconds_t tv_usec; /* microseconds */
  17. };
  18. struct timezone {
  19. int tz_minuteswest; /* minutes west of Greenwich */
  20. int tz_dsttime; /* type of dst correction */
  21. };
  22. #ifdef __KERNEL__
  23. /* Parameters used to convert the timespec values */
  24. #ifndef USEC_PER_SEC
  25. #define USEC_PER_SEC (1000000L)
  26. #endif
  27. #ifndef NSEC_PER_SEC
  28. #define NSEC_PER_SEC (1000000000L)
  29. #endif
  30. #ifndef NSEC_PER_USEC
  31. #define NSEC_PER_USEC (1000L)
  32. #endif
  33. static __inline__ int timespec_equal(struct timespec *a, struct timespec *b)
  34. {
  35. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  36. }
  37. /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  38. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  39. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  40. *
  41. * [For the Julian calendar (which was used in Russia before 1917,
  42. * Britain & colonies before 1752, anywhere else before 1582,
  43. * and is still in use by some communities) leave out the
  44. * -year/100+year/400 terms, and add 10.]
  45. *
  46. * This algorithm was first published by Gauss (I think).
  47. *
  48. * WARNING: this function will overflow on 2106-02-07 06:28:16 on
  49. * machines were long is 32-bit! (However, as time_t is signed, we
  50. * will already get problems at other places on 2038-01-19 03:14:08)
  51. */
  52. static inline unsigned long
  53. mktime (unsigned int year, unsigned int mon,
  54. unsigned int day, unsigned int hour,
  55. unsigned int min, unsigned int sec)
  56. {
  57. if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
  58. mon += 12; /* Puts Feb last since it has leap day */
  59. year -= 1;
  60. }
  61. return (((
  62. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  63. year*365 - 719499
  64. )*24 + hour /* now have hours */
  65. )*60 + min /* now have minutes */
  66. )*60 + sec; /* finally seconds */
  67. }
  68. extern struct timespec xtime;
  69. extern struct timespec wall_to_monotonic;
  70. extern seqlock_t xtime_lock;
  71. static inline unsigned long get_seconds(void)
  72. {
  73. return xtime.tv_sec;
  74. }
  75. struct timespec current_kernel_time(void);
  76. #define CURRENT_TIME (current_kernel_time())
  77. #define CURRENT_TIME_SEC ((struct timespec) { xtime.tv_sec, 0 })
  78. extern void do_gettimeofday(struct timeval *tv);
  79. extern int do_settimeofday(struct timespec *tv);
  80. extern int do_sys_settimeofday(struct timespec *tv, struct timezone *tz);
  81. extern void clock_was_set(void); // call when ever the clock is set
  82. extern int do_posix_clock_monotonic_gettime(struct timespec *tp);
  83. extern long do_nanosleep(struct timespec *t);
  84. extern long do_utimes(char __user * filename, struct timeval * times);
  85. struct itimerval;
  86. extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);
  87. extern int do_getitimer(int which, struct itimerval *value);
  88. extern void getnstimeofday (struct timespec *tv);
  89. extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
  90. static inline void
  91. set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
  92. {
  93. while (nsec > NSEC_PER_SEC) {
  94. nsec -= NSEC_PER_SEC;
  95. ++sec;
  96. }
  97. while (nsec < 0) {
  98. nsec += NSEC_PER_SEC;
  99. --sec;
  100. }
  101. ts->tv_sec = sec;
  102. ts->tv_nsec = nsec;
  103. }
  104. #endif /* __KERNEL__ */
  105. #define NFDBITS __NFDBITS
  106. #define FD_SETSIZE __FD_SETSIZE
  107. #define FD_SET(fd,fdsetp) __FD_SET(fd,fdsetp)
  108. #define FD_CLR(fd,fdsetp) __FD_CLR(fd,fdsetp)
  109. #define FD_ISSET(fd,fdsetp) __FD_ISSET(fd,fdsetp)
  110. #define FD_ZERO(fdsetp) __FD_ZERO(fdsetp)
  111. /*
  112. * Names of the interval timers, and structure
  113. * defining a timer setting.
  114. */
  115. #define ITIMER_REAL 0
  116. #define ITIMER_VIRTUAL 1
  117. #define ITIMER_PROF 2
  118. struct itimerspec {
  119. struct timespec it_interval; /* timer period */
  120. struct timespec it_value; /* timer expiration */
  121. };
  122. struct itimerval {
  123. struct timeval it_interval; /* timer interval */
  124. struct timeval it_value; /* current value */
  125. };
  126. /*
  127. * The IDs of the various system clocks (for POSIX.1b interval timers).
  128. */
  129. #define CLOCK_REALTIME 0
  130. #define CLOCK_MONOTONIC 1
  131. #define CLOCK_PROCESS_CPUTIME_ID 2
  132. #define CLOCK_THREAD_CPUTIME_ID 3
  133. #define CLOCK_REALTIME_HR 4
  134. #define CLOCK_MONOTONIC_HR 5
  135. /*
  136. * The IDs of various hardware clocks
  137. */
  138. #define CLOCK_SGI_CYCLE 10
  139. #define MAX_CLOCKS 16
  140. #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC | \
  141. CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR)
  142. #define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR)
  143. /*
  144. * The various flags for setting POSIX.1b interval timers.
  145. */
  146. #define TIMER_ABSTIME 0x01
  147. #endif