Lely core libraries 2.3.4
time.h
Go to the documentation of this file.
1
22#ifndef LELY_UTIL_TIME_H_
23#define LELY_UTIL_TIME_H_
24
25#include <lely/libc/time.h>
26
27#include <assert.h>
28#include <stdint.h>
29
30#ifndef LELY_UTIL_TIME_INLINE
31#define LELY_UTIL_TIME_INLINE static inline
32#endif
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
39LELY_UTIL_TIME_INLINE void timespec_add(
40 struct timespec *tp, const struct timespec *inc);
41
43LELY_UTIL_TIME_INLINE void timespec_add_sec(
44 struct timespec *tp, uint_least64_t sec);
45
47LELY_UTIL_TIME_INLINE void timespec_add_msec(
48 struct timespec *tp, uint_least64_t msec);
49
51LELY_UTIL_TIME_INLINE void timespec_add_usec(
52 struct timespec *tp, uint_least64_t usec);
53
55LELY_UTIL_TIME_INLINE void timespec_add_nsec(
56 struct timespec *tp, uint_least64_t nsec);
57
59LELY_UTIL_TIME_INLINE void timespec_sub(
60 struct timespec *tp, const struct timespec *dec);
61
63LELY_UTIL_TIME_INLINE void timespec_sub_sec(
64 struct timespec *tp, uint_least64_t sec);
65
67LELY_UTIL_TIME_INLINE void timespec_sub_msec(
68 struct timespec *tp, uint_least64_t msec);
69
71LELY_UTIL_TIME_INLINE void timespec_sub_usec(
72 struct timespec *tp, uint_least64_t usec);
73
75LELY_UTIL_TIME_INLINE void timespec_sub_nsec(
76 struct timespec *tp, uint_least64_t nsec);
77
82LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_sec(
83 const struct timespec *t1, const struct timespec *t2);
84
90LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_msec(
91 const struct timespec *t1, const struct timespec *t2);
92
98LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_usec(
99 const struct timespec *t1, const struct timespec *t2);
100
105LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_nsec(
106 const struct timespec *t1, const struct timespec *t2);
107
115LELY_UTIL_TIME_INLINE int timespec_cmp(const void *p1, const void *p2);
116
117LELY_UTIL_TIME_INLINE void
118timespec_add(struct timespec *tp, const struct timespec *inc)
119{
120 assert(tp);
121 assert(inc);
122
123 tp->tv_sec += inc->tv_sec;
124 tp->tv_nsec += inc->tv_nsec;
125
126 if (tp->tv_nsec >= 1000000000l) {
127 tp->tv_sec++;
128 tp->tv_nsec -= 1000000000l;
129 }
130}
131
132LELY_UTIL_TIME_INLINE void
133timespec_add_sec(struct timespec *tp, uint_least64_t sec)
134{
135 assert(tp);
136
137 tp->tv_sec += sec;
138}
139
140LELY_UTIL_TIME_INLINE void
141timespec_add_msec(struct timespec *tp, uint_least64_t msec)
142{
143 struct timespec inc = { (time_t)(msec / 1000),
144 (long)((msec % 1000) * 1000000l) };
145 timespec_add(tp, &inc);
146}
147
148LELY_UTIL_TIME_INLINE void
149timespec_add_usec(struct timespec *tp, uint_least64_t usec)
150{
151 struct timespec inc = { (time_t)(usec / 1000000l),
152 (long)((usec % 1000000l) * 1000) };
153 timespec_add(tp, &inc);
154}
155
156LELY_UTIL_TIME_INLINE void
157timespec_add_nsec(struct timespec *tp, uint_least64_t nsec)
158{
159 struct timespec inc = { (time_t)(nsec / 1000000000l),
160 (long)(nsec % 1000000000l) };
161 timespec_add(tp, &inc);
162}
163
164LELY_UTIL_TIME_INLINE void
165timespec_sub(struct timespec *tp, const struct timespec *dec)
166{
167 assert(tp);
168 assert(dec);
169
170 tp->tv_sec -= dec->tv_sec;
171 tp->tv_nsec -= dec->tv_nsec;
172
173 if (tp->tv_nsec < 0) {
174 tp->tv_sec--;
175 tp->tv_nsec += 1000000000l;
176 }
177}
178
179LELY_UTIL_TIME_INLINE void
180timespec_sub_sec(struct timespec *tp, uint_least64_t sec)
181{
182 assert(tp);
183
184 tp->tv_sec -= sec;
185}
186
187LELY_UTIL_TIME_INLINE void
188timespec_sub_msec(struct timespec *tp, uint_least64_t msec)
189{
190 struct timespec dec = { (time_t)(msec / 1000),
191 (long)((msec % 1000) * 1000000l) };
192 timespec_sub(tp, &dec);
193}
194
195LELY_UTIL_TIME_INLINE void
196timespec_sub_usec(struct timespec *tp, uint_least64_t usec)
197{
198 struct timespec dec = { (time_t)(usec / 1000000l),
199 (long)((usec % 1000000l) * 1000) };
200 timespec_sub(tp, &dec);
201}
202
203LELY_UTIL_TIME_INLINE void
204timespec_sub_nsec(struct timespec *tp, uint_least64_t nsec)
205{
206 struct timespec dec = { (time_t)(nsec / 1000000000l),
207 (long)(nsec % 1000000000l) };
208 timespec_sub(tp, &dec);
209}
210
211LELY_UTIL_TIME_INLINE int_least64_t
212timespec_diff_sec(const struct timespec *t1, const struct timespec *t2)
213{
214 assert(t1);
215 assert(t2);
216
217 return t1->tv_sec - t2->tv_sec;
218}
219
220LELY_UTIL_TIME_INLINE int_least64_t
221timespec_diff_msec(const struct timespec *t1, const struct timespec *t2)
222{
223 assert(t1);
224 assert(t2);
225
226 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000
227 + (t1->tv_nsec - t2->tv_nsec) / 1000000l;
228}
229
230LELY_UTIL_TIME_INLINE int_least64_t
231timespec_diff_usec(const struct timespec *t1, const struct timespec *t2)
232{
233 assert(t1);
234 assert(t2);
235
236 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000000l
237 + (t1->tv_nsec - t2->tv_nsec) / 1000;
238}
239
240LELY_UTIL_TIME_INLINE int_least64_t
241timespec_diff_nsec(const struct timespec *t1, const struct timespec *t2)
242{
243 assert(t1);
244 assert(t2);
245
246 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000000000l
247 + t1->tv_nsec - t2->tv_nsec;
248}
249
250LELY_UTIL_TIME_INLINE int
251timespec_cmp(const void *p1, const void *p2)
252{
253 if (p1 == p2)
254 return 0;
255
256 if (!p1)
257 return -1;
258 if (!p2)
259 return 1;
260
261 const struct timespec *t1 = (const struct timespec *)p1;
262 const struct timespec *t2 = (const struct timespec *)p2;
263
264 int cmp = (t2->tv_sec < t1->tv_sec) - (t1->tv_sec < t2->tv_sec);
265 if (!cmp)
266 cmp = (t2->tv_nsec < t1->tv_nsec) - (t1->tv_nsec < t2->tv_nsec);
267 return cmp;
268}
269
270#ifdef __cplusplus
271}
272#endif
273
274#endif // !LELY_UTIL_TIME_H_
This header file is part of the C11 and POSIX compatibility library; it includes <time....
This header file is part of the C11 and POSIX compatibility library; it includes <stdint....
A time type with nanosecond resolution.
Definition time.h:88
long tv_nsec
Nanoseconds [0, 999999999].
Definition time.h:92
time_t tv_sec
Whole seconds (>= 0).
Definition time.h:90
int timespec_cmp(const void *p1, const void *p2)
Compares two times.
Definition time.h:251
void timespec_sub_msec(struct timespec *tp, uint_least64_t msec)
Subtracts msec milliseconds from the time at tp.
Definition time.h:188
void timespec_add_sec(struct timespec *tp, uint_least64_t sec)
Adds sec seconds to the time at tp.
Definition time.h:133
void timespec_add(struct timespec *tp, const struct timespec *inc)
Adds the time interval *inc to the time at tp.
Definition time.h:118
void timespec_add_usec(struct timespec *tp, uint_least64_t usec)
Adds usec microseconds to the time at tp.
Definition time.h:149
void timespec_sub(struct timespec *tp, const struct timespec *dec)
Subtracts the time interval *dec from the time at tp.
Definition time.h:165
void timespec_sub_nsec(struct timespec *tp, uint_least64_t nsec)
Subtracts nsec nanoseconds from the time at tp.
Definition time.h:204
void timespec_add_msec(struct timespec *tp, uint_least64_t msec)
Adds msec milliseconds to the time at tp.
Definition time.h:141
int_least64_t timespec_diff_sec(const struct timespec *t1, const struct timespec *t2)
Returns the time difference (in seconds) between *t1 and *t2.
Definition time.h:212
void timespec_sub_usec(struct timespec *tp, uint_least64_t usec)
Subtracts usec microseconds from the time at tp.
Definition time.h:196
int_least64_t timespec_diff_msec(const struct timespec *t1, const struct timespec *t2)
Returns the time difference (in milliseconds) between *t1 and *t2.
Definition time.h:221
void timespec_sub_sec(struct timespec *tp, uint_least64_t sec)
Subtracts sec seconds from the time at tp.
Definition time.h:180
int_least64_t timespec_diff_nsec(const struct timespec *t1, const struct timespec *t2)
Returns the time difference (in nanoseconds) between *t1 and *t2.
Definition time.h:241
void timespec_add_nsec(struct timespec *tp, uint_least64_t nsec)
Adds nsec nanoseconds to the time at tp.
Definition time.h:157
int_least64_t timespec_diff_usec(const struct timespec *t1, const struct timespec *t2)
Returns the time difference (in microseconds) between *t1 and *t2.
Definition time.h:231