1 #ifndef _LINUX_TIME32_H
2 #define _LINUX_TIME32_H
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.
8 * Any interfaces in here that become unused as we migrate
9 * code to time64_t should get removed.
12 #include <linux/time64.h>
14 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
16 #if __BITS_PER_LONG == 64
18 /* timespec64 is defined as timespec here */
19 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
21 return *(const struct timespec *)&ts64;
24 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
26 return *(const struct timespec64 *)&ts;
30 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
34 ret.tv_sec = (time_t)ts64.tv_sec;
35 ret.tv_nsec = ts64.tv_nsec;
39 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
41 struct timespec64 ret;
43 ret.tv_sec = ts.tv_sec;
44 ret.tv_nsec = ts.tv_nsec;
49 static inline int timespec_equal(const struct timespec *a,
50 const struct timespec *b)
52 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
56 * lhs < rhs: return <0
57 * lhs == rhs: return 0
58 * lhs > rhs: return >0
60 static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
62 if (lhs->tv_sec < rhs->tv_sec)
64 if (lhs->tv_sec > rhs->tv_sec)
66 return lhs->tv_nsec - rhs->tv_nsec;
69 extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
71 static inline struct timespec timespec_add(struct timespec lhs,
74 struct timespec ts_delta;
76 set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
77 lhs.tv_nsec + rhs.tv_nsec);
82 * sub = lhs - rhs, in normalized form
84 static inline struct timespec timespec_sub(struct timespec lhs,
87 struct timespec ts_delta;
89 set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
90 lhs.tv_nsec - rhs.tv_nsec);
95 * Returns true if the timespec is norm, false if denorm:
97 static inline bool timespec_valid(const struct timespec *ts)
99 /* Dates before 1970 are bogus */
102 /* Can't have more nanoseconds then a second */
103 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
108 static inline bool timespec_valid_strict(const struct timespec *ts)
110 if (!timespec_valid(ts))
112 /* Disallow values that could overflow ktime_t */
113 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
119 * timespec_to_ns - Convert timespec to nanoseconds
120 * @ts: pointer to the timespec variable to be converted
122 * Returns the scalar nanosecond representation of the timespec
125 static inline s64 timespec_to_ns(const struct timespec *ts)
127 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
131 * ns_to_timespec - Convert nanoseconds to timespec
132 * @nsec: the nanoseconds value to be converted
134 * Returns the timespec representation of the nsec parameter.
136 extern struct timespec ns_to_timespec(const s64 nsec);
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
143 * This must always be inlined because its used from the x86-64 vdso,
144 * which cannot call other kernel functions.
146 static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
148 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
153 * time_to_tm - converts the calendar time to local broken-down time
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
160 static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result)
162 time64_to_tm(totalsecs, offset, result);
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)
170 return mktime64(year, mon, day, hour, min, sec);
173 static inline bool timeval_valid(const struct timeval *tv)
175 /* Dates before 1970 are bogus */
179 /* Can't have more microseconds then a second */
180 if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
186 extern struct timespec timespec_trunc(struct timespec t, unsigned int gran);
189 * timeval_to_ns - Convert timeval to nanoseconds
190 * @ts: pointer to the timeval variable to be converted
192 * Returns the scalar nanosecond representation of the timeval
195 static inline s64 timeval_to_ns(const struct timeval *tv)
197 return ((s64) tv->tv_sec * NSEC_PER_SEC) +
198 tv->tv_usec * NSEC_PER_USEC;
202 * ns_to_timeval - Convert nanoseconds to timeval
203 * @nsec: the nanoseconds value to be converted
205 * Returns the timeval representation of the nsec parameter.
207 extern struct timeval ns_to_timeval(const s64 nsec);
208 extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec);
211 * New aliases for compat time functions. These will be used to replace
212 * the compat code so it can be shared between 32-bit and 64-bit builds
213 * both of which provide compatibility with old 32-bit tasks.
215 #define old_time32_t compat_time_t
216 #define old_timeval32 compat_timeval
217 #define old_timespec32 compat_timespec
218 #define old_itimerspec32 compat_itimerspec
219 #define ns_to_old_timeval32 ns_to_compat_timeval
220 #define get_old_itimerspec32 get_compat_itimerspec64
221 #define put_old_itimerspec32 put_compat_itimerspec64
222 #define get_old_timespec32 compat_get_timespec64
223 #define put_old_timespec32 compat_put_timespec64