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 | |
| 16 | static inline int timespec_equal(const struct timespec *a, |
| 17 | const struct timespec *b) |
| 18 | { |
| 19 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
| 20 | } |
| 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 | |
| 36 | extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); |
| 37 | |
| 38 | static inline struct timespec timespec_add(struct timespec lhs, |
| 39 | struct timespec rhs) |
| 40 | { |
| 41 | struct timespec ts_delta; |
| 42 | |
| 43 | set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, |
| 44 | lhs.tv_nsec + rhs.tv_nsec); |
| 45 | return ts_delta; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * sub = lhs - rhs, in normalized form |
| 50 | */ |
| 51 | static inline struct timespec timespec_sub(struct timespec lhs, |
| 52 | struct timespec rhs) |
| 53 | { |
| 54 | struct timespec ts_delta; |
| 55 | |
| 56 | set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec, |
| 57 | lhs.tv_nsec - rhs.tv_nsec); |
| 58 | return ts_delta; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Returns true if the timespec is norm, false if denorm: |
| 63 | */ |
| 64 | static inline bool timespec_valid(const struct timespec *ts) |
| 65 | { |
| 66 | /* Dates before 1970 are bogus */ |
| 67 | if (ts->tv_sec < 0) |
| 68 | return false; |
| 69 | /* Can't have more nanoseconds then a second */ |
| 70 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 71 | return false; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | static inline bool timespec_valid_strict(const struct timespec *ts) |
| 76 | { |
| 77 | if (!timespec_valid(ts)) |
| 78 | return false; |
| 79 | /* Disallow values that could overflow ktime_t */ |
| 80 | if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) |
| 81 | return false; |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * timespec_to_ns - Convert timespec to nanoseconds |
| 87 | * @ts: pointer to the timespec variable to be converted |
| 88 | * |
| 89 | * Returns the scalar nanosecond representation of the timespec |
| 90 | * parameter. |
| 91 | */ |
| 92 | static inline s64 timespec_to_ns(const struct timespec *ts) |
| 93 | { |
| 94 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * ns_to_timespec - Convert nanoseconds to timespec |
| 99 | * @nsec: the nanoseconds value to be converted |
| 100 | * |
| 101 | * Returns the timespec representation of the nsec parameter. |
| 102 | */ |
| 103 | extern struct timespec ns_to_timespec(const s64 nsec); |
| 104 | |
| 105 | /** |
| 106 | * timespec_add_ns - Adds nanoseconds to a timespec |
| 107 | * @a: pointer to timespec to be incremented |
| 108 | * @ns: unsigned nanoseconds value to be added |
| 109 | * |
| 110 | * This must always be inlined because its used from the x86-64 vdso, |
| 111 | * which cannot call other kernel functions. |
| 112 | */ |
| 113 | static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) |
| 114 | { |
| 115 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); |
| 116 | a->tv_nsec = ns; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * time_to_tm - converts the calendar time to local broken-down time |
| 121 | * |
| 122 | * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970, |
| 123 | * Coordinated Universal Time (UTC). |
| 124 | * @offset offset seconds adding to totalsecs. |
| 125 | * @result pointer to struct tm variable to receive broken-down time |
| 126 | */ |
| 127 | static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result) |
| 128 | { |
| 129 | time64_to_tm(totalsecs, offset, result); |
| 130 | } |
| 131 | |
| 132 | static inline unsigned long mktime(const unsigned int year, |
| 133 | const unsigned int mon, const unsigned int day, |
| 134 | const unsigned int hour, const unsigned int min, |
| 135 | const unsigned int sec) |
| 136 | { |
| 137 | return mktime64(year, mon, day, hour, min, sec); |
| 138 | } |
| 139 | |
| 140 | static inline bool timeval_valid(const struct timeval *tv) |
| 141 | { |
| 142 | /* Dates before 1970 are bogus */ |
| 143 | if (tv->tv_sec < 0) |
| 144 | return false; |
| 145 | |
| 146 | /* Can't have more microseconds then a second */ |
| 147 | if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) |
| 148 | return false; |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | extern struct timespec timespec_trunc(struct timespec t, unsigned int gran); |
| 154 | |
| 155 | /** |
| 156 | * timeval_to_ns - Convert timeval to nanoseconds |
| 157 | * @ts: pointer to the timeval variable to be converted |
| 158 | * |
| 159 | * Returns the scalar nanosecond representation of the timeval |
| 160 | * parameter. |
| 161 | */ |
| 162 | static inline s64 timeval_to_ns(const struct timeval *tv) |
| 163 | { |
| 164 | return ((s64) tv->tv_sec * NSEC_PER_SEC) + |
| 165 | tv->tv_usec * NSEC_PER_USEC; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * ns_to_timeval - Convert nanoseconds to timeval |
| 170 | * @nsec: the nanoseconds value to be converted |
| 171 | * |
| 172 | * Returns the timeval representation of the nsec parameter. |
| 173 | */ |
| 174 | extern struct timeval ns_to_timeval(const s64 nsec); |
| 175 | |
| 176 | #endif |