unify {de,}mangle_poll(), get rid of kernel-side POLL...
[cris-mirror.git] / include / linux / time32.h
blob65b1de25198d58e45930c54de13637bedfee4a2c
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.
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 ts64;
24 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
26 return ts;
29 # define timespec_equal timespec64_equal
30 # define timespec_compare timespec64_compare
31 # define set_normalized_timespec set_normalized_timespec64
32 # define timespec_add timespec64_add
33 # define timespec_sub timespec64_sub
34 # define timespec_valid timespec64_valid
35 # define timespec_valid_strict timespec64_valid_strict
36 # define timespec_to_ns timespec64_to_ns
37 # define ns_to_timespec ns_to_timespec64
38 # define timespec_add_ns timespec64_add_ns
40 #else
41 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
43 struct timespec ret;
45 ret.tv_sec = (time_t)ts64.tv_sec;
46 ret.tv_nsec = ts64.tv_nsec;
47 return ret;
50 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
52 struct timespec64 ret;
54 ret.tv_sec = ts.tv_sec;
55 ret.tv_nsec = ts.tv_nsec;
56 return ret;
59 static inline int timespec_equal(const struct timespec *a,
60 const struct timespec *b)
62 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
66 * lhs < rhs: return <0
67 * lhs == rhs: return 0
68 * lhs > rhs: return >0
70 static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
72 if (lhs->tv_sec < rhs->tv_sec)
73 return -1;
74 if (lhs->tv_sec > rhs->tv_sec)
75 return 1;
76 return lhs->tv_nsec - rhs->tv_nsec;
79 extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
81 static inline struct timespec timespec_add(struct timespec lhs,
82 struct timespec rhs)
84 struct timespec ts_delta;
86 set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec,
87 lhs.tv_nsec + rhs.tv_nsec);
88 return ts_delta;
92 * sub = lhs - rhs, in normalized form
94 static inline struct timespec timespec_sub(struct timespec lhs,
95 struct timespec rhs)
97 struct timespec ts_delta;
99 set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec,
100 lhs.tv_nsec - rhs.tv_nsec);
101 return ts_delta;
105 * Returns true if the timespec is norm, false if denorm:
107 static inline bool timespec_valid(const struct timespec *ts)
109 /* Dates before 1970 are bogus */
110 if (ts->tv_sec < 0)
111 return false;
112 /* Can't have more nanoseconds then a second */
113 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
114 return false;
115 return true;
118 static inline bool timespec_valid_strict(const struct timespec *ts)
120 if (!timespec_valid(ts))
121 return false;
122 /* Disallow values that could overflow ktime_t */
123 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
124 return false;
125 return true;
129 * timespec_to_ns - Convert timespec to nanoseconds
130 * @ts: pointer to the timespec variable to be converted
132 * Returns the scalar nanosecond representation of the timespec
133 * parameter.
135 static inline s64 timespec_to_ns(const struct timespec *ts)
137 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
141 * ns_to_timespec - Convert nanoseconds to timespec
142 * @nsec: the nanoseconds value to be converted
144 * Returns the timespec representation of the nsec parameter.
146 extern struct timespec ns_to_timespec(const s64 nsec);
149 * timespec_add_ns - Adds nanoseconds to a timespec
150 * @a: pointer to timespec to be incremented
151 * @ns: unsigned nanoseconds value to be added
153 * This must always be inlined because its used from the x86-64 vdso,
154 * which cannot call other kernel functions.
156 static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
158 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
159 a->tv_nsec = ns;
162 #endif
165 * time_to_tm - converts the calendar time to local broken-down time
167 * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970,
168 * Coordinated Universal Time (UTC).
169 * @offset offset seconds adding to totalsecs.
170 * @result pointer to struct tm variable to receive broken-down time
172 static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result)
174 time64_to_tm(totalsecs, offset, result);
177 static inline unsigned long mktime(const unsigned int year,
178 const unsigned int mon, const unsigned int day,
179 const unsigned int hour, const unsigned int min,
180 const unsigned int sec)
182 return mktime64(year, mon, day, hour, min, sec);
185 static inline bool timeval_valid(const struct timeval *tv)
187 /* Dates before 1970 are bogus */
188 if (tv->tv_sec < 0)
189 return false;
191 /* Can't have more microseconds then a second */
192 if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
193 return false;
195 return true;
198 extern struct timespec timespec_trunc(struct timespec t, unsigned int gran);
201 * timeval_to_ns - Convert timeval to nanoseconds
202 * @ts: pointer to the timeval variable to be converted
204 * Returns the scalar nanosecond representation of the timeval
205 * parameter.
207 static inline s64 timeval_to_ns(const struct timeval *tv)
209 return ((s64) tv->tv_sec * NSEC_PER_SEC) +
210 tv->tv_usec * NSEC_PER_USEC;
214 * ns_to_timeval - Convert nanoseconds to timeval
215 * @nsec: the nanoseconds value to be converted
217 * Returns the timeval representation of the nsec parameter.
219 extern struct timeval ns_to_timeval(const s64 nsec);
221 #endif