Lely core libraries 1.9.2
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 <stdint.h>
28
29#ifndef LELY_UTIL_TIME_INLINE
30#define LELY_UTIL_TIME_INLINE static inline
31#endif
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
38LELY_UTIL_TIME_INLINE void timespec_add(
39 struct timespec *tp, const struct timespec *inc);
40
42LELY_UTIL_TIME_INLINE void timespec_add_sec(
43 struct timespec *tp, uint_least64_t sec);
44
46LELY_UTIL_TIME_INLINE void timespec_add_msec(
47 struct timespec *tp, uint_least64_t msec);
48
50LELY_UTIL_TIME_INLINE void timespec_add_usec(
51 struct timespec *tp, uint_least64_t usec);
52
54LELY_UTIL_TIME_INLINE void timespec_add_nsec(
55 struct timespec *tp, uint_least64_t nsec);
56
58LELY_UTIL_TIME_INLINE void timespec_sub(
59 struct timespec *tp, const struct timespec *dec);
60
62LELY_UTIL_TIME_INLINE void timespec_sub_sec(
63 struct timespec *tp, uint_least64_t sec);
64
66LELY_UTIL_TIME_INLINE void timespec_sub_msec(
67 struct timespec *tp, uint_least64_t msec);
68
70LELY_UTIL_TIME_INLINE void timespec_sub_usec(
71 struct timespec *tp, uint_least64_t usec);
72
74LELY_UTIL_TIME_INLINE void timespec_sub_nsec(
75 struct timespec *tp, uint_least64_t nsec);
76
78LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_sec(
79 const struct timespec *t1, const struct timespec *t2);
80
85LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_msec(
86 const struct timespec *t1, const struct timespec *t2);
87
92LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_usec(
93 const struct timespec *t1, const struct timespec *t2);
94
99LELY_UTIL_TIME_INLINE int_least64_t timespec_diff_nsec(
100 const struct timespec *t1, const struct timespec *t2);
101
109LELY_UTIL_TIME_INLINE int timespec_cmp(const void *p1, const void *p2);
110
111inline void
112timespec_add(struct timespec *tp, const struct timespec *inc)
113{
114 tp->tv_sec += inc->tv_sec;
115 tp->tv_nsec += inc->tv_nsec;
116
117 if (tp->tv_nsec >= 1000000000l) {
118 tp->tv_sec++;
119 tp->tv_nsec -= 1000000000l;
120 }
121}
122
123inline void
124timespec_add_sec(struct timespec *tp, uint_least64_t sec)
125{
126 tp->tv_sec += sec;
127}
128
129inline void
130timespec_add_msec(struct timespec *tp, uint_least64_t msec)
131{
132 struct timespec inc = { (time_t)(msec / 1000),
133 (long)((msec % 1000) * 1000000l) };
134 timespec_add(tp, &inc);
135}
136
137inline void
138timespec_add_usec(struct timespec *tp, uint_least64_t usec)
139{
140 struct timespec inc = { (time_t)(usec / 1000000l),
141 (long)((usec % 1000000l) * 1000) };
142 timespec_add(tp, &inc);
143}
144
145inline void
146timespec_add_nsec(struct timespec *tp, uint_least64_t nsec)
147{
148 struct timespec inc = { (time_t)(nsec / 1000000000l),
149 (long)(nsec % 1000000000l) };
150 timespec_add(tp, &inc);
151}
152
153inline void
154timespec_sub(struct timespec *tp, const struct timespec *dec)
155{
156 tp->tv_sec -= dec->tv_sec;
157 tp->tv_nsec -= dec->tv_nsec;
158
159 if (tp->tv_nsec < 0) {
160 tp->tv_sec--;
161 tp->tv_nsec += 1000000000l;
162 }
163}
164
165inline void
166timespec_sub_sec(struct timespec *tp, uint_least64_t sec)
167{
168 tp->tv_sec += sec;
169}
170
171inline void
172timespec_sub_msec(struct timespec *tp, uint_least64_t msec)
173{
174 struct timespec dec = { (time_t)(msec / 1000),
175 (long)((msec % 1000) * 1000000l) };
176 timespec_sub(tp, &dec);
177}
178
179inline void
180timespec_sub_usec(struct timespec *tp, uint_least64_t usec)
181{
182 struct timespec dec = { (time_t)(usec / 1000000l),
183 (long)((usec % 1000000l) * 1000) };
184 timespec_sub(tp, &dec);
185}
186
187inline void
188timespec_sub_nsec(struct timespec *tp, uint_least64_t nsec)
189{
190 struct timespec dec = { (time_t)(nsec / 1000000000l),
191 (long)(nsec % 1000000000l) };
192 timespec_sub(tp, &dec);
193}
194
195inline int_least64_t
196timespec_diff_sec(const struct timespec *t1, const struct timespec *t2)
197{
198 return (t1->tv_sec - t2->tv_sec)
199 + (t1->tv_nsec - t2->tv_nsec) / 1000000000l;
200}
201
202inline int_least64_t
203timespec_diff_msec(const struct timespec *t1, const struct timespec *t2)
204{
205 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000
206 + (t1->tv_nsec - t2->tv_nsec) / 1000000l;
207}
208
209inline int_least64_t
210timespec_diff_usec(const struct timespec *t1, const struct timespec *t2)
211{
212 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000000l
213 + (t1->tv_nsec - t2->tv_nsec) / 1000;
214}
215
216inline int_least64_t
217timespec_diff_nsec(const struct timespec *t1, const struct timespec *t2)
218{
219 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000000000l
220 + t1->tv_nsec - t2->tv_nsec;
221}
222
223inline int
224timespec_cmp(const void *p1, const void *p2)
225{
226 if (p1 == p2)
227 return 0;
228
229 if (!p1)
230 return -1;
231 if (!p2)
232 return 1;
233
234 const struct timespec *t1 = (const struct timespec *)p1;
235 const struct timespec *t2 = (const struct timespec *)p2;
236
237 int cmp = (t2->tv_sec < t1->tv_sec) - (t1->tv_sec < t2->tv_sec);
238 if (!cmp)
239 cmp = (t2->tv_nsec < t1->tv_nsec) - (t1->tv_nsec < t2->tv_nsec);
240 return cmp;
241}
242
243#ifdef __cplusplus
244}
245#endif
246
247#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:83
long tv_nsec
Nanoseconds [0, 999999999].
Definition: time.h:87
time_t tv_sec
Whole seconds (>= 0).
Definition: time.h:85
int timespec_cmp(const void *p1, const void *p2)
Compares two times.
Definition: time.h:224
void timespec_sub_msec(struct timespec *tp, uint_least64_t msec)
Subtracts msec milliseconds to the time at tp.
Definition: time.h:172
void timespec_add_sec(struct timespec *tp, uint_least64_t sec)
Adds sec seconds to the time at tp.
Definition: time.h:124
void timespec_add(struct timespec *tp, const struct timespec *inc)
Adds the time interval *inc to the time at tp.
Definition: time.h:112
void timespec_add_usec(struct timespec *tp, uint_least64_t usec)
Adds usec microseconds to the time at tp.
Definition: time.h:138
void timespec_sub(struct timespec *tp, const struct timespec *dec)
Subtracts the time interval *inc to the time at tp.
Definition: time.h:154
void timespec_sub_nsec(struct timespec *tp, uint_least64_t nsec)
Subtracts nsec nanoseconds to the time at tp.
Definition: time.h:188
void timespec_add_msec(struct timespec *tp, uint_least64_t msec)
Adds msec milliseconds to the time at tp.
Definition: time.h:130
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:196
void timespec_sub_usec(struct timespec *tp, uint_least64_t usec)
Subtracts usec microseconds to the time at tp.
Definition: time.h:180
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:203
void timespec_sub_sec(struct timespec *tp, uint_least64_t sec)
Subtracts sec seconds to the time at tp.
Definition: time.h:166
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:217
void timespec_add_nsec(struct timespec *tp, uint_least64_t nsec)
Adds nsec nanoseconds to the time at tp.
Definition: time.h:146
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:210