Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 1 | #ifndef _LINUX_TIME32_H |
| 2 | #define _LINUX_TIME32_H |
| 3 | /* |
| 4 | * These are all interfaces based on the old time_t definition |
| 5 | * that overflows in 2038 on 32-bit architectures. New code |
| 6 | * should use the replacements based on time64_t and timespec64. |
| 7 | * |
| 8 | * Any interfaces in here that become unused as we migrate |
| 9 | * code to time64_t should get removed. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/time64.h> |
| 13 | |
| 14 | #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) |
| 15 | |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 16 | #if __BITS_PER_LONG == 64 |
| 17 | |
| 18 | /* timespec64 is defined as timespec here */ |
| 19 | static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) |
| 20 | { |
Arnd Bergmann | 4f0fad9 | 2018-04-27 15:40:12 +0200 | [diff] [blame] | 21 | return *(const struct timespec *)&ts64; |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) |
| 25 | { |
Arnd Bergmann | 4f0fad9 | 2018-04-27 15:40:12 +0200 | [diff] [blame] | 26 | return *(const struct timespec64 *)&ts; |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 29 | #else |
| 30 | static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) |
| 31 | { |
| 32 | struct timespec ret; |
| 33 | |
| 34 | ret.tv_sec = (time_t)ts64.tv_sec; |
| 35 | ret.tv_nsec = ts64.tv_nsec; |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) |
| 40 | { |
| 41 | struct timespec64 ret; |
| 42 | |
| 43 | ret.tv_sec = ts.tv_sec; |
| 44 | ret.tv_nsec = ts.tv_nsec; |
| 45 | return ret; |
| 46 | } |
Arnd Bergmann | 4f0fad9 | 2018-04-27 15:40:12 +0200 | [diff] [blame] | 47 | #endif |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 48 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 49 | static inline int timespec_equal(const struct timespec *a, |
| 50 | const struct timespec *b) |
| 51 | { |
| 52 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * lhs < rhs: return <0 |
| 57 | * lhs == rhs: return 0 |
| 58 | * lhs > rhs: return >0 |
| 59 | */ |
| 60 | static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs) |
| 61 | { |
| 62 | if (lhs->tv_sec < rhs->tv_sec) |
| 63 | return -1; |
| 64 | if (lhs->tv_sec > rhs->tv_sec) |
| 65 | return 1; |
| 66 | return lhs->tv_nsec - rhs->tv_nsec; |
| 67 | } |
| 68 | |
| 69 | extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); |
| 70 | |
| 71 | static inline struct timespec timespec_add(struct timespec lhs, |
| 72 | struct timespec rhs) |
| 73 | { |
| 74 | struct timespec ts_delta; |
| 75 | |
| 76 | set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, |
| 77 | lhs.tv_nsec + rhs.tv_nsec); |
| 78 | return ts_delta; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * sub = lhs - rhs, in normalized form |
| 83 | */ |
| 84 | static inline struct timespec timespec_sub(struct timespec lhs, |
| 85 | struct timespec rhs) |
| 86 | { |
| 87 | struct timespec ts_delta; |
| 88 | |
| 89 | set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec, |
| 90 | lhs.tv_nsec - rhs.tv_nsec); |
| 91 | return ts_delta; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Returns true if the timespec is norm, false if denorm: |
| 96 | */ |
| 97 | static inline bool timespec_valid(const struct timespec *ts) |
| 98 | { |
| 99 | /* Dates before 1970 are bogus */ |
| 100 | if (ts->tv_sec < 0) |
| 101 | return false; |
| 102 | /* Can't have more nanoseconds then a second */ |
| 103 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 104 | return false; |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | static inline bool timespec_valid_strict(const struct timespec *ts) |
| 109 | { |
| 110 | if (!timespec_valid(ts)) |
| 111 | return false; |
| 112 | /* Disallow values that could overflow ktime_t */ |
| 113 | if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) |
| 114 | return false; |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * timespec_to_ns - Convert timespec to nanoseconds |
| 120 | * @ts: pointer to the timespec variable to be converted |
| 121 | * |
| 122 | * Returns the scalar nanosecond representation of the timespec |
| 123 | * parameter. |
| 124 | */ |
| 125 | static inline s64 timespec_to_ns(const struct timespec *ts) |
| 126 | { |
| 127 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * ns_to_timespec - Convert nanoseconds to timespec |
| 132 | * @nsec: the nanoseconds value to be converted |
| 133 | * |
| 134 | * Returns the timespec representation of the nsec parameter. |
| 135 | */ |
| 136 | extern struct timespec ns_to_timespec(const s64 nsec); |
| 137 | |
| 138 | /** |
| 139 | * timespec_add_ns - Adds nanoseconds to a timespec |
| 140 | * @a: pointer to timespec to be incremented |
| 141 | * @ns: unsigned nanoseconds value to be added |
| 142 | * |
| 143 | * This must always be inlined because its used from the x86-64 vdso, |
| 144 | * which cannot call other kernel functions. |
| 145 | */ |
| 146 | static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) |
| 147 | { |
| 148 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); |
| 149 | a->tv_nsec = ns; |
| 150 | } |
| 151 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 152 | /** |
| 153 | * time_to_tm - converts the calendar time to local broken-down time |
| 154 | * |
| 155 | * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970, |
| 156 | * Coordinated Universal Time (UTC). |
| 157 | * @offset offset seconds adding to totalsecs. |
| 158 | * @result pointer to struct tm variable to receive broken-down time |
| 159 | */ |
| 160 | static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result) |
| 161 | { |
| 162 | time64_to_tm(totalsecs, offset, result); |
| 163 | } |
| 164 | |
| 165 | static inline unsigned long mktime(const unsigned int year, |
| 166 | const unsigned int mon, const unsigned int day, |
| 167 | const unsigned int hour, const unsigned int min, |
| 168 | const unsigned int sec) |
| 169 | { |
| 170 | return mktime64(year, mon, day, hour, min, sec); |
| 171 | } |
| 172 | |
| 173 | static inline bool timeval_valid(const struct timeval *tv) |
| 174 | { |
| 175 | /* Dates before 1970 are bogus */ |
| 176 | if (tv->tv_sec < 0) |
| 177 | return false; |
| 178 | |
| 179 | /* Can't have more microseconds then a second */ |
| 180 | if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) |
| 181 | return false; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | extern struct timespec timespec_trunc(struct timespec t, unsigned int gran); |
| 187 | |
| 188 | /** |
| 189 | * timeval_to_ns - Convert timeval to nanoseconds |
| 190 | * @ts: pointer to the timeval variable to be converted |
| 191 | * |
| 192 | * Returns the scalar nanosecond representation of the timeval |
| 193 | * parameter. |
| 194 | */ |
| 195 | static inline s64 timeval_to_ns(const struct timeval *tv) |
| 196 | { |
| 197 | return ((s64) tv->tv_sec * NSEC_PER_SEC) + |
| 198 | tv->tv_usec * NSEC_PER_USEC; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * ns_to_timeval - Convert nanoseconds to timeval |
| 203 | * @nsec: the nanoseconds value to be converted |
| 204 | * |
| 205 | * Returns the timeval representation of the nsec parameter. |
| 206 | */ |
| 207 | extern struct timeval ns_to_timeval(const s64 nsec); |
Arnd Bergmann | a84d116 | 2018-03-15 17:12:40 +0100 | [diff] [blame] | 208 | extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec); |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 209 | |
| 210 | #endif |