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
35 extern "C" {
36 #endif
37 
39 LELY_UTIL_TIME_INLINE void timespec_add(
40  struct timespec *tp, const struct timespec *inc);
41 
43 LELY_UTIL_TIME_INLINE void timespec_add_sec(
44  struct timespec *tp, uint_least64_t sec);
45 
47 LELY_UTIL_TIME_INLINE void timespec_add_msec(
48  struct timespec *tp, uint_least64_t msec);
49 
51 LELY_UTIL_TIME_INLINE void timespec_add_usec(
52  struct timespec *tp, uint_least64_t usec);
53 
55 LELY_UTIL_TIME_INLINE void timespec_add_nsec(
56  struct timespec *tp, uint_least64_t nsec);
57 
59 LELY_UTIL_TIME_INLINE void timespec_sub(
60  struct timespec *tp, const struct timespec *dec);
61 
63 LELY_UTIL_TIME_INLINE void timespec_sub_sec(
64  struct timespec *tp, uint_least64_t sec);
65 
67 LELY_UTIL_TIME_INLINE void timespec_sub_msec(
68  struct timespec *tp, uint_least64_t msec);
69 
71 LELY_UTIL_TIME_INLINE void timespec_sub_usec(
72  struct timespec *tp, uint_least64_t usec);
73 
75 LELY_UTIL_TIME_INLINE void timespec_sub_nsec(
76  struct timespec *tp, uint_least64_t nsec);
77 
82 LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_sec(
83  const struct timespec *t1, const struct timespec *t2);
84 
90 LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_msec(
91  const struct timespec *t1, const struct timespec *t2);
92 
98 LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_usec(
99  const struct timespec *t1, const struct timespec *t2);
100 
105 LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_nsec(
106  const struct timespec *t1, const struct timespec *t2);
107 
115 LELY_UTIL_TIME_INLINE int timespec_cmp(const void *p1, const void *p2);
116 
117 LELY_UTIL_TIME_INLINE void
118 timespec_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 
132 LELY_UTIL_TIME_INLINE void
133 timespec_add_sec(struct timespec *tp, uint_least64_t sec)
134 {
135  assert(tp);
136 
137  tp->tv_sec += sec;
138 }
139 
140 LELY_UTIL_TIME_INLINE void
141 timespec_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 
148 LELY_UTIL_TIME_INLINE void
149 timespec_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 
156 LELY_UTIL_TIME_INLINE void
157 timespec_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 
164 LELY_UTIL_TIME_INLINE void
165 timespec_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 
179 LELY_UTIL_TIME_INLINE void
180 timespec_sub_sec(struct timespec *tp, uint_least64_t sec)
181 {
182  assert(tp);
183 
184  tp->tv_sec -= sec;
185 }
186 
187 LELY_UTIL_TIME_INLINE void
188 timespec_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 
195 LELY_UTIL_TIME_INLINE void
196 timespec_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 
203 LELY_UTIL_TIME_INLINE void
204 timespec_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 
211 LELY_UTIL_TIME_INLINE int_least64_t
212 timespec_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 
220 LELY_UTIL_TIME_INLINE int_least64_t
221 timespec_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 
230 LELY_UTIL_TIME_INLINE int_least64_t
231 timespec_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 
240 LELY_UTIL_TIME_INLINE int_least64_t
241 timespec_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 
250 LELY_UTIL_TIME_INLINE int
251 timespec_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_
timespec_sub_nsec
void timespec_sub_nsec(struct timespec *tp, uint_least64_t nsec)
Subtracts nsec nanoseconds from the time at tp.
Definition: time.h:204
time.h
timespec_sub
void timespec_sub(struct timespec *tp, const struct timespec *dec)
Subtracts the time interval *dec from the time at tp.
Definition: time.h:165
timespec_cmp
int timespec_cmp(const void *p1, const void *p2)
Compares two times.
Definition: time.h:251
timespec_diff_usec
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
timespec_add_msec
void timespec_add_msec(struct timespec *tp, uint_least64_t msec)
Adds msec milliseconds to the time at tp.
Definition: time.h:141
timespec_diff_sec
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
timespec_diff_nsec
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
stdint.h
timespec_sub_msec
void timespec_sub_msec(struct timespec *tp, uint_least64_t msec)
Subtracts msec milliseconds from the time at tp.
Definition: time.h:188
timespec_sub_sec
void timespec_sub_sec(struct timespec *tp, uint_least64_t sec)
Subtracts sec seconds from the time at tp.
Definition: time.h:180
timespec_sub_usec
void timespec_sub_usec(struct timespec *tp, uint_least64_t usec)
Subtracts usec microseconds from the time at tp.
Definition: time.h:196
timespec_add_sec
void timespec_add_sec(struct timespec *tp, uint_least64_t sec)
Adds sec seconds to the time at tp.
Definition: time.h:133
timespec_diff_msec
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
timespec_add_nsec
void timespec_add_nsec(struct timespec *tp, uint_least64_t nsec)
Adds nsec nanoseconds to the time at tp.
Definition: time.h:157
timespec_add
void timespec_add(struct timespec *tp, const struct timespec *inc)
Adds the time interval *inc to the time at tp.
Definition: time.h:118
timespec_add_usec
void timespec_add_usec(struct timespec *tp, uint_least64_t usec)
Adds usec microseconds to the time at tp.
Definition: time.h:149