Commit | Line | Data |
---|---|---|
5dbf2012 AB |
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 | ||
abc8f96e AB |
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 | { | |
21 | return ts64; | |
22 | } | |
23 | ||
24 | static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) | |
25 | { | |
26 | return ts; | |
27 | } | |
28 | ||
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 | |
39 | ||
40 | #else | |
41 | static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) | |
42 | { | |
43 | struct timespec ret; | |
44 | ||
45 | ret.tv_sec = (time_t)ts64.tv_sec; | |
46 | ret.tv_nsec = ts64.tv_nsec; | |
47 | return ret; | |
48 | } | |
49 | ||
50 | static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) | |
51 | { | |
52 | struct timespec64 ret; | |
53 | ||
54 | ret.tv_sec = ts.tv_sec; | |
55 | ret.tv_nsec = ts.tv_nsec; | |
56 | return ret; | |
57 | } | |
58 | ||
5dbf2012 AB |
59 | static inline int timespec_equal(const struct timespec *a, |
60 | const struct timespec *b) | |
61 | { | |
62 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); | |
63 | } | |
64 | ||
65 | /* | |
66 | * lhs < rhs: return <0 | |
67 | * lhs == rhs: return 0 | |
68 | * lhs > rhs: return >0 | |
69 | */ | |
70 | static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs) | |
71 | { | |
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; | |
77 | } | |
78 | ||
79 | extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); | |
80 | ||
81 | static inline struct timespec timespec_add(struct timespec lhs, | |
82 | struct timespec rhs) | |
83 | { | |
84 | struct timespec ts_delta; | |
85 | ||
86 | set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, | |
87 | lhs.tv_nsec + rhs.tv_nsec); | |
88 | return ts_delta; | |
89 | } | |
90 | ||
91 | /* | |
92 | * sub = lhs - rhs, in normalized form | |
93 | */ | |
94 | static inline struct timespec timespec_sub(struct timespec lhs, | |
95 | struct timespec rhs) | |
96 | { | |
97 | struct timespec ts_delta; | |
98 | ||
99 | set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec, | |
100 | lhs.tv_nsec - rhs.tv_nsec); | |
101 | return ts_delta; | |
102 | } | |
103 | ||
104 | /* | |
105 | * Returns true if the timespec is norm, false if denorm: | |
106 | */ | |
107 | static inline bool timespec_valid(const struct timespec *ts) | |
108 | { | |
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; | |
116 | } | |
117 | ||
118 | static inline bool timespec_valid_strict(const struct timespec *ts) | |
119 | { | |
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; | |
126 | } | |
127 | ||
128 | /** | |
129 | * timespec_to_ns - Convert timespec to nanoseconds | |
130 | * @ts: pointer to the timespec variable to be converted | |
131 | * | |
132 | * Returns the scalar nanosecond representation of the timespec | |
133 | * parameter. | |
134 | */ | |
135 | static inline s64 timespec_to_ns(const struct timespec *ts) | |
136 | { | |
137 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; | |
138 | } | |
139 | ||
140 | /** | |
141 | * ns_to_timespec - Convert nanoseconds to timespec | |
142 | * @nsec: the nanoseconds value to be converted | |
143 | * | |
144 | * Returns the timespec representation of the nsec parameter. | |
145 | */ | |
146 | extern struct timespec ns_to_timespec(const s64 nsec); | |
147 | ||
148 | /** | |
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 | |
152 | * | |
153 | * This must always be inlined because its used from the x86-64 vdso, | |
154 | * which cannot call other kernel functions. | |
155 | */ | |
156 | static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) | |
157 | { | |
158 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); | |
159 | a->tv_nsec = ns; | |
160 | } | |
161 | ||
abc8f96e AB |
162 | #endif |
163 | ||
5dbf2012 AB |
164 | /** |
165 | * time_to_tm - converts the calendar time to local broken-down time | |
166 | * | |
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 | |
171 | */ | |
172 | static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result) | |
173 | { | |
174 | time64_to_tm(totalsecs, offset, result); | |
175 | } | |
176 | ||
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) | |
181 | { | |
182 | return mktime64(year, mon, day, hour, min, sec); | |
183 | } | |
184 | ||
185 | static inline bool timeval_valid(const struct timeval *tv) | |
186 | { | |
187 | /* Dates before 1970 are bogus */ | |
188 | if (tv->tv_sec < 0) | |
189 | return false; | |
190 | ||
191 | /* Can't have more microseconds then a second */ | |
192 | if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) | |
193 | return false; | |
194 | ||
195 | return true; | |
196 | } | |
197 | ||
198 | extern struct timespec timespec_trunc(struct timespec t, unsigned int gran); | |
199 | ||
200 | /** | |
201 | * timeval_to_ns - Convert timeval to nanoseconds | |
202 | * @ts: pointer to the timeval variable to be converted | |
203 | * | |
204 | * Returns the scalar nanosecond representation of the timeval | |
205 | * parameter. | |
206 | */ | |
207 | static inline s64 timeval_to_ns(const struct timeval *tv) | |
208 | { | |
209 | return ((s64) tv->tv_sec * NSEC_PER_SEC) + | |
210 | tv->tv_usec * NSEC_PER_USEC; | |
211 | } | |
212 | ||
213 | /** | |
214 | * ns_to_timeval - Convert nanoseconds to timeval | |
215 | * @nsec: the nanoseconds value to be converted | |
216 | * | |
217 | * Returns the timeval representation of the nsec parameter. | |
218 | */ | |
219 | extern struct timeval ns_to_timeval(const s64 nsec); | |
220 | ||
221 | #endif |