time.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef _LINUX_TIME_H
  2. #define _LINUX_TIME_H
  3. # include <linux/cache.h>
  4. # include <linux/seqlock.h>
  5. # include <linux/math64.h>
  6. # include <linux/time64.h>
  7. extern struct timezone sys_tz;
  8. #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
  9. static inline int timespec_equal(const struct timespec *a,
  10. const struct timespec *b)
  11. {
  12. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  13. }
  14. /*
  15. * lhs < rhs: return <0
  16. * lhs == rhs: return 0
  17. * lhs > rhs: return >0
  18. */
  19. static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
  20. {
  21. if (lhs->tv_sec < rhs->tv_sec)
  22. return -1;
  23. if (lhs->tv_sec > rhs->tv_sec)
  24. return 1;
  25. return lhs->tv_nsec - rhs->tv_nsec;
  26. }
  27. static inline int timeval_compare(const struct timeval *lhs, const struct timeval *rhs)
  28. {
  29. if (lhs->tv_sec < rhs->tv_sec)
  30. return -1;
  31. if (lhs->tv_sec > rhs->tv_sec)
  32. return 1;
  33. return lhs->tv_usec - rhs->tv_usec;
  34. }
  35. extern unsigned long mktime(const unsigned int year, const unsigned int mon,
  36. const unsigned int day, const unsigned int hour,
  37. const unsigned int min, const unsigned int sec);
  38. extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
  39. /*
  40. * timespec_add_safe assumes both values are positive and checks
  41. * for overflow. It will return TIME_T_MAX if the reutrn would be
  42. * smaller then either of the arguments.
  43. */
  44. extern struct timespec timespec_add_safe(const struct timespec lhs,
  45. const struct timespec rhs);
  46. static inline struct timespec timespec_add(struct timespec lhs,
  47. struct timespec rhs)
  48. {
  49. struct timespec ts_delta;
  50. set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
  51. lhs.tv_nsec + rhs.tv_nsec);
  52. return ts_delta;
  53. }
  54. /*
  55. * sub = lhs - rhs, in normalized form
  56. */
  57. static inline struct timespec timespec_sub(struct timespec lhs,
  58. struct timespec rhs)
  59. {
  60. struct timespec ts_delta;
  61. set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
  62. lhs.tv_nsec - rhs.tv_nsec);
  63. return ts_delta;
  64. }
  65. /*
  66. * Returns true if the timespec is norm, false if denorm:
  67. */
  68. static inline bool timespec_valid(const struct timespec *ts)
  69. {
  70. /* Dates before 1970 are bogus */
  71. if (ts->tv_sec < 0)
  72. return false;
  73. /* Can't have more nanoseconds then a second */
  74. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  75. return false;
  76. return true;
  77. }
  78. static inline bool timespec_valid_strict(const struct timespec *ts)
  79. {
  80. if (!timespec_valid(ts))
  81. return false;
  82. /* Disallow values that could overflow ktime_t */
  83. if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
  84. return false;
  85. return true;
  86. }
  87. static inline bool timeval_valid(const struct timeval *tv)
  88. {
  89. /* Dates before 1970 are bogus */
  90. if (tv->tv_sec < 0)
  91. return false;
  92. /* Can't have more microseconds then a second */
  93. if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
  94. return false;
  95. return true;
  96. }
  97. extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
  98. #define CURRENT_TIME (current_kernel_time())
  99. #define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
  100. /* Some architectures do not supply their own clocksource.
  101. * This is mainly the case in architectures that get their
  102. * inter-tick times by reading the counter on their interval
  103. * timer. Since these timers wrap every tick, they're not really
  104. * useful as clocksources. Wrapping them to act like one is possible
  105. * but not very efficient. So we provide a callout these arches
  106. * can implement for use with the jiffies clocksource to provide
  107. * finer then tick granular time.
  108. */
  109. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  110. extern u32 (*arch_gettimeoffset)(void);
  111. #endif
  112. struct itimerval;
  113. extern int do_setitimer(int which, struct itimerval *value,
  114. struct itimerval *ovalue);
  115. extern int do_getitimer(int which, struct itimerval *value);
  116. extern unsigned int alarm_setitimer(unsigned int seconds);
  117. extern long do_utimes(int dfd, const char __user *filename, struct timespec *times, int flags);
  118. struct tms;
  119. extern void do_sys_times(struct tms *);
  120. /*
  121. * Similar to the struct tm in userspace <time.h>, but it needs to be here so
  122. * that the kernel source is self contained.
  123. */
  124. struct tm {
  125. /*
  126. * the number of seconds after the minute, normally in the range
  127. * 0 to 59, but can be up to 60 to allow for leap seconds
  128. */
  129. int tm_sec;
  130. /* the number of minutes after the hour, in the range 0 to 59*/
  131. int tm_min;
  132. /* the number of hours past midnight, in the range 0 to 23 */
  133. int tm_hour;
  134. /* the day of the month, in the range 1 to 31 */
  135. int tm_mday;
  136. /* the number of months since January, in the range 0 to 11 */
  137. int tm_mon;
  138. /* the number of years since 1900 */
  139. long tm_year;
  140. /* the number of days since Sunday, in the range 0 to 6 */
  141. int tm_wday;
  142. /* the number of days since January 1, in the range 0 to 365 */
  143. int tm_yday;
  144. };
  145. void time_to_tm(time_t totalsecs, int offset, struct tm *result);
  146. /**
  147. * timespec_to_ns - Convert timespec to nanoseconds
  148. * @ts: pointer to the timespec variable to be converted
  149. *
  150. * Returns the scalar nanosecond representation of the timespec
  151. * parameter.
  152. */
  153. static inline s64 timespec_to_ns(const struct timespec *ts)
  154. {
  155. return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  156. }
  157. /**
  158. * timeval_to_ns - Convert timeval to nanoseconds
  159. * @ts: pointer to the timeval variable to be converted
  160. *
  161. * Returns the scalar nanosecond representation of the timeval
  162. * parameter.
  163. */
  164. static inline s64 timeval_to_ns(const struct timeval *tv)
  165. {
  166. return ((s64) tv->tv_sec * NSEC_PER_SEC) +
  167. tv->tv_usec * NSEC_PER_USEC;
  168. }
  169. /**
  170. * ns_to_timespec - Convert nanoseconds to timespec
  171. * @nsec: the nanoseconds value to be converted
  172. *
  173. * Returns the timespec representation of the nsec parameter.
  174. */
  175. extern struct timespec ns_to_timespec(const s64 nsec);
  176. /**
  177. * ns_to_timeval - Convert nanoseconds to timeval
  178. * @nsec: the nanoseconds value to be converted
  179. *
  180. * Returns the timeval representation of the nsec parameter.
  181. */
  182. extern struct timeval ns_to_timeval(const s64 nsec);
  183. /**
  184. * timespec_add_ns - Adds nanoseconds to a timespec
  185. * @a: pointer to timespec to be incremented
  186. * @ns: unsigned nanoseconds value to be added
  187. *
  188. * This must always be inlined because its used from the x86-64 vdso,
  189. * which cannot call other kernel functions.
  190. */
  191. static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
  192. {
  193. a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
  194. a->tv_nsec = ns;
  195. }
  196. #endif