time.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. int get_timespec64(struct timespec64 *ts,
  9. const struct timespec __user *uts);
  10. int put_timespec64(const struct timespec64 *ts,
  11. struct timespec __user *uts);
  12. int get_itimerspec64(struct itimerspec64 *it,
  13. const struct itimerspec __user *uit);
  14. int put_itimerspec64(const struct itimerspec64 *it,
  15. struct itimerspec __user *uit);
  16. #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
  17. static inline int timespec_equal(const struct timespec *a,
  18. const struct timespec *b)
  19. {
  20. return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
  21. }
  22. /*
  23. * lhs < rhs: return <0
  24. * lhs == rhs: return 0
  25. * lhs > rhs: return >0
  26. */
  27. static inline int timespec_compare(const struct timespec *lhs, const struct timespec *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_nsec - rhs->tv_nsec;
  34. }
  35. static inline int timeval_compare(const struct timeval *lhs, const struct timeval *rhs)
  36. {
  37. if (lhs->tv_sec < rhs->tv_sec)
  38. return -1;
  39. if (lhs->tv_sec > rhs->tv_sec)
  40. return 1;
  41. return lhs->tv_usec - rhs->tv_usec;
  42. }
  43. extern time64_t mktime64(const unsigned int year, const unsigned int mon,
  44. const unsigned int day, const unsigned int hour,
  45. const unsigned int min, const unsigned int sec);
  46. /**
  47. * Deprecated. Use mktime64().
  48. */
  49. static inline unsigned long mktime(const unsigned int year,
  50. const unsigned int mon, const unsigned int day,
  51. const unsigned int hour, const unsigned int min,
  52. const unsigned int sec)
  53. {
  54. return mktime64(year, mon, day, hour, min, sec);
  55. }
  56. extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
  57. /*
  58. * timespec_add_safe assumes both values are positive and checks
  59. * for overflow. It will return TIME_T_MAX if the reutrn would be
  60. * smaller then either of the arguments.
  61. */
  62. extern struct timespec timespec_add_safe(const struct timespec lhs,
  63. const struct timespec rhs);
  64. static inline struct timespec timespec_add(struct timespec lhs,
  65. struct timespec rhs)
  66. {
  67. struct timespec ts_delta;
  68. set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
  69. lhs.tv_nsec + rhs.tv_nsec);
  70. return ts_delta;
  71. }
  72. /*
  73. * sub = lhs - rhs, in normalized form
  74. */
  75. static inline struct timespec timespec_sub(struct timespec lhs,
  76. struct timespec rhs)
  77. {
  78. struct timespec ts_delta;
  79. set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
  80. lhs.tv_nsec - rhs.tv_nsec);
  81. return ts_delta;
  82. }
  83. /*
  84. * Returns true if the timespec is norm, false if denorm:
  85. */
  86. static inline bool timespec_valid(const struct timespec *ts)
  87. {
  88. /* Dates before 1970 are bogus */
  89. if (ts->tv_sec < 0)
  90. return false;
  91. /* Can't have more nanoseconds then a second */
  92. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  93. return false;
  94. return true;
  95. }
  96. static inline bool timespec_valid_strict(const struct timespec *ts)
  97. {
  98. if (!timespec_valid(ts))
  99. return false;
  100. /* Disallow values that could overflow ktime_t */
  101. if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
  102. return false;
  103. return true;
  104. }
  105. static inline bool timeval_valid(const struct timeval *tv)
  106. {
  107. /* Dates before 1970 are bogus */
  108. if (tv->tv_sec < 0)
  109. return false;
  110. /* Can't have more microseconds then a second */
  111. if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
  112. return false;
  113. return true;
  114. }
  115. extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
  116. /*
  117. * Validates if a timespec/timeval used to inject a time offset is valid.
  118. * Offsets can be postive or negative. The value of the timeval/timespec
  119. * is the sum of its fields, but *NOTE*: the field tv_usec/tv_nsec must
  120. * always be non-negative.
  121. */
  122. static inline bool timeval_inject_offset_valid(const struct timeval *tv)
  123. {
  124. /* We don't check the tv_sec as it can be positive or negative */
  125. /* Can't have more microseconds then a second */
  126. if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
  127. return false;
  128. return true;
  129. }
  130. static inline bool timespec_inject_offset_valid(const struct timespec *ts)
  131. {
  132. /* We don't check the tv_sec as it can be positive or negative */
  133. /* Can't have more nanoseconds then a second */
  134. if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
  135. return false;
  136. return true;
  137. }
  138. /* Some architectures do not supply their own clocksource.
  139. * This is mainly the case in architectures that get their
  140. * inter-tick times by reading the counter on their interval
  141. * timer. Since these timers wrap every tick, they're not really
  142. * useful as clocksources. Wrapping them to act like one is possible
  143. * but not very efficient. So we provide a callout these arches
  144. * can implement for use with the jiffies clocksource to provide
  145. * finer then tick granular time.
  146. */
  147. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  148. extern u32 (*arch_gettimeoffset)(void);
  149. #endif
  150. struct itimerval;
  151. extern int do_setitimer(int which, struct itimerval *value,
  152. struct itimerval *ovalue);
  153. extern int do_getitimer(int which, struct itimerval *value);
  154. extern long do_utimes(int dfd, const char __user *filename, struct timespec64 *times, int flags);
  155. /*
  156. * Similar to the struct tm in userspace <time.h>, but it needs to be here so
  157. * that the kernel source is self contained.
  158. */
  159. struct tm {
  160. /*
  161. * the number of seconds after the minute, normally in the range
  162. * 0 to 59, but can be up to 60 to allow for leap seconds
  163. */
  164. int tm_sec;
  165. /* the number of minutes after the hour, in the range 0 to 59*/
  166. int tm_min;
  167. /* the number of hours past midnight, in the range 0 to 23 */
  168. int tm_hour;
  169. /* the day of the month, in the range 1 to 31 */
  170. int tm_mday;
  171. /* the number of months since January, in the range 0 to 11 */
  172. int tm_mon;
  173. /* the number of years since 1900 */
  174. long tm_year;
  175. /* the number of days since Sunday, in the range 0 to 6 */
  176. int tm_wday;
  177. /* the number of days since January 1, in the range 0 to 365 */
  178. int tm_yday;
  179. };
  180. void time64_to_tm(time64_t totalsecs, int offset, struct tm *result);
  181. /**
  182. * time_to_tm - converts the calendar time to local broken-down time
  183. *
  184. * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970,
  185. * Coordinated Universal Time (UTC).
  186. * @offset offset seconds adding to totalsecs.
  187. * @result pointer to struct tm variable to receive broken-down time
  188. */
  189. static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result)
  190. {
  191. time64_to_tm(totalsecs, offset, result);
  192. }
  193. /**
  194. * timespec_to_ns - Convert timespec to nanoseconds
  195. * @ts: pointer to the timespec variable to be converted
  196. *
  197. * Returns the scalar nanosecond representation of the timespec
  198. * parameter.
  199. */
  200. static inline s64 timespec_to_ns(const struct timespec *ts)
  201. {
  202. return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
  203. }
  204. /**
  205. * timeval_to_ns - Convert timeval to nanoseconds
  206. * @ts: pointer to the timeval variable to be converted
  207. *
  208. * Returns the scalar nanosecond representation of the timeval
  209. * parameter.
  210. */
  211. static inline s64 timeval_to_ns(const struct timeval *tv)
  212. {
  213. return ((s64) tv->tv_sec * NSEC_PER_SEC) +
  214. tv->tv_usec * NSEC_PER_USEC;
  215. }
  216. /**
  217. * ns_to_timespec - Convert nanoseconds to timespec
  218. * @nsec: the nanoseconds value to be converted
  219. *
  220. * Returns the timespec representation of the nsec parameter.
  221. */
  222. extern struct timespec ns_to_timespec(const s64 nsec);
  223. /**
  224. * ns_to_timeval - Convert nanoseconds to timeval
  225. * @nsec: the nanoseconds value to be converted
  226. *
  227. * Returns the timeval representation of the nsec parameter.
  228. */
  229. extern struct timeval ns_to_timeval(const s64 nsec);
  230. /**
  231. * timespec_add_ns - Adds nanoseconds to a timespec
  232. * @a: pointer to timespec to be incremented
  233. * @ns: unsigned nanoseconds value to be added
  234. *
  235. * This must always be inlined because its used from the x86-64 vdso,
  236. * which cannot call other kernel functions.
  237. */
  238. static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
  239. {
  240. a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
  241. a->tv_nsec = ns;
  242. }
  243. static inline bool itimerspec64_valid(const struct itimerspec64 *its)
  244. {
  245. if (!timespec64_valid(&(its->it_interval)) ||
  246. !timespec64_valid(&(its->it_value)))
  247. return false;
  248. return true;
  249. }
  250. /**
  251. * time_after32 - compare two 32-bit relative times
  252. * @a: the time which may be after @b
  253. * @b: the time which may be before @a
  254. *
  255. * time_after32(a, b) returns true if the time @a is after time @b.
  256. * time_before32(b, a) returns true if the time @b is before time @a.
  257. *
  258. * Similar to time_after(), compare two 32-bit timestamps for relative
  259. * times. This is useful for comparing 32-bit seconds values that can't
  260. * be converted to 64-bit values (e.g. due to disk format or wire protocol
  261. * issues) when it is known that the times are less than 68 years apart.
  262. */
  263. #define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
  264. #define time_before32(b, a) time_after32(a, b)
  265. #endif