22 #ifndef LELY_UTIL_TIME_H_
23 #define LELY_UTIL_TIME_H_
30 #ifndef LELY_UTIL_TIME_INLINE
31 #define LELY_UTIL_TIME_INLINE static inline
40 struct timespec *tp,
const struct timespec *inc);
44 struct timespec *tp, uint_least64_t sec);
48 struct timespec *tp, uint_least64_t msec);
52 struct timespec *tp, uint_least64_t usec);
56 struct timespec *tp, uint_least64_t nsec);
60 struct timespec *tp,
const struct timespec *dec);
64 struct timespec *tp, uint_least64_t sec);
68 struct timespec *tp, uint_least64_t msec);
72 struct timespec *tp, uint_least64_t usec);
76 struct timespec *tp, uint_least64_t nsec);
83 const struct timespec *t1,
const struct timespec *t2);
91 const struct timespec *t1,
const struct timespec *t2);
99 const struct timespec *t1,
const struct timespec *t2);
106 const struct timespec *t1,
const struct timespec *t2);
115 LELY_UTIL_TIME_INLINE
int timespec_cmp(
const void *p1,
const void *p2);
117 LELY_UTIL_TIME_INLINE
void
123 tp->tv_sec += inc->tv_sec;
124 tp->tv_nsec += inc->tv_nsec;
126 if (tp->tv_nsec >= 1000000000l) {
128 tp->tv_nsec -= 1000000000l;
132 LELY_UTIL_TIME_INLINE
void
140 LELY_UTIL_TIME_INLINE
void
143 struct timespec inc = { (time_t)(msec / 1000),
144 (long)((msec % 1000) * 1000000l) };
148 LELY_UTIL_TIME_INLINE
void
151 struct timespec inc = { (time_t)(usec / 1000000l),
152 (long)((usec % 1000000l) * 1000) };
156 LELY_UTIL_TIME_INLINE
void
159 struct timespec inc = { (time_t)(nsec / 1000000000l),
160 (long)(nsec % 1000000000l) };
164 LELY_UTIL_TIME_INLINE
void
170 tp->tv_sec -= dec->tv_sec;
171 tp->tv_nsec -= dec->tv_nsec;
173 if (tp->tv_nsec < 0) {
175 tp->tv_nsec += 1000000000l;
179 LELY_UTIL_TIME_INLINE
void
187 LELY_UTIL_TIME_INLINE
void
190 struct timespec dec = { (time_t)(msec / 1000),
191 (long)((msec % 1000) * 1000000l) };
195 LELY_UTIL_TIME_INLINE
void
198 struct timespec dec = { (time_t)(usec / 1000000l),
199 (long)((usec % 1000000l) * 1000) };
203 LELY_UTIL_TIME_INLINE
void
206 struct timespec dec = { (time_t)(nsec / 1000000000l),
207 (long)(nsec % 1000000000l) };
211 LELY_UTIL_TIME_INLINE int_least64_t
217 return t1->tv_sec - t2->tv_sec;
220 LELY_UTIL_TIME_INLINE int_least64_t
226 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000
227 + (t1->tv_nsec - t2->tv_nsec) / 1000000l;
230 LELY_UTIL_TIME_INLINE int_least64_t
236 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000000l
237 + (t1->tv_nsec - t2->tv_nsec) / 1000;
240 LELY_UTIL_TIME_INLINE int_least64_t
246 return (int_least64_t)(t1->tv_sec - t2->tv_sec) * 1000000000l
247 + t1->tv_nsec - t2->tv_nsec;
250 LELY_UTIL_TIME_INLINE
int
261 const struct timespec *t1 = (
const struct timespec *)p1;
262 const struct timespec *t2 = (
const struct timespec *)p2;
264 int cmp = (t2->tv_sec < t1->tv_sec) - (t1->tv_sec < t2->tv_sec);
266 cmp = (t2->tv_nsec < t1->tv_nsec) - (t1->tv_nsec < t2->tv_nsec);
274 #endif // !LELY_UTIL_TIME_H_