1#ifndef _LINUX_TIME32_H
2#define _LINUX_TIME32_H
3
4
5
6
7
8
9
10
11
12#include <linux/time64.h>
13
14#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
15
16#if __BITS_PER_LONG == 64
17
18
19static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
20{
21 return ts64;
22}
23
24static 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
41static 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
50static 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
59static 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
67
68
69
70static 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
79extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
80
81static 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
93
94static 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
106
107static inline bool timespec_valid(const struct timespec *ts)
108{
109
110 if (ts->tv_sec < 0)
111 return false;
112
113 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
114 return false;
115 return true;
116}
117
118static inline bool timespec_valid_strict(const struct timespec *ts)
119{
120 if (!timespec_valid(ts))
121 return false;
122
123 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
124 return false;
125 return true;
126}
127
128
129
130
131
132
133
134
135static 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
142
143
144
145
146extern struct timespec ns_to_timespec(const s64 nsec);
147
148
149
150
151
152
153
154
155
156static __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
162#endif
163
164
165
166
167
168
169
170
171
172static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result)
173{
174 time64_to_tm(totalsecs, offset, result);
175}
176
177static 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
185static inline bool timeval_valid(const struct timeval *tv)
186{
187
188 if (tv->tv_sec < 0)
189 return false;
190
191
192 if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC)
193 return false;
194
195 return true;
196}
197
198extern struct timespec timespec_trunc(struct timespec t, unsigned int gran);
199
200
201
202
203
204
205
206
207static 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
215
216
217
218
219extern struct timeval ns_to_timeval(const s64 nsec);
220extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec);
221
222#endif
223