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