1 /* Licensed under BSD-MIT - see LICENSE file for details */
6 #if HAVE_STRUCT_TIMESPEC
10 time_t tv_sec; /* seconds */
11 long tv_nsec; /* nanoseconds */
18 #include <ccan/str/str.h>
19 #define TIME_CHECK(t) \
20 time_check_((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
21 #define TIMEREL_CHECK(t) \
22 timerel_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
23 #define TIMEABS_CHECK(t) \
24 timeabs_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
26 #define TIME_CHECK(t) (t)
27 #define TIMEREL_CHECK(t) (t)
28 #define TIMEABS_CHECK(t) (t)
32 * struct timerel - a relative time.
33 * @ts: the actual timespec value.
35 * For example, 1 second: ts.tv_sec = 1, ts.tv_nsec = 0
42 * struct timeabs - an absolue time.
43 * @ts: the actual timespec value.
45 * For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
51 struct timespec time_check_(struct timespec in, const char *abortstr);
54 * timerel_check - check if a relative time is malformed.
55 * @in: the relative time to check (returned)
56 * @abortstr: the string to print to stderr before aborting (if set).
58 * This can be used to make sure a time isn't negative and doesn't
59 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
60 * that will be printed and abort() is called. Otherwise, if
61 * @abortstr is NULL then the returned timerel will be normalized and
62 * tv_sec set to 0 if it was negative.
64 * Note that if ccan/time is compiled with DEBUG, then it will call this
65 * for all passed and returned times.
68 * printf("Time to calc this was %lu nanoseconds\n",
69 * (long)timerel_check(time_between(time_now(), time_now()),
70 * "time_now() failed?").ts.tv_nsec);
72 struct timerel timerel_check(struct timerel in, const char *abortstr);
75 * timeabs_check - check if an absolute time is malformed.
76 * @in: the relative time to check (returned)
77 * @abortstr: the string to print to stderr before aborting (if set).
79 * This can be used to make sure a time isn't negative and doesn't
80 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
81 * that will be printed and abort() is called. Otherwise, if
82 * @abortstr is NULL then the returned timeabs will be normalized and
83 * tv_sec set to 0 if it was negative.
85 * Note that if ccan/time is compiled with DEBUG, then it will call this
86 * for all passed and returned times.
89 * printf("Now is %lu seconds since epoch\n",
90 * (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
92 struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
95 * time_now - return the current time
98 * printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
100 struct timeabs time_now(void);
102 static inline bool time_greater_(struct timespec a, struct timespec b)
104 if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
106 else if (a.tv_sec < b.tv_sec)
109 return a.tv_nsec > b.tv_nsec;
113 * time_after - is a after b?
115 * @b: another abstime.
118 * static bool timed_out(const struct timeabs *start)
120 * #define TIMEOUT time_from_msec(1000)
121 * return time_after(time_now(), timeabs_add(*start, TIMEOUT));
124 static inline bool time_after(struct timeabs a, struct timeabs b)
126 return time_greater_(a.ts, b.ts);
130 * time_greater - is a greater than b?
132 * @b: another reltime.
134 static inline bool time_greater(struct timerel a, struct timerel b)
136 return time_greater_(a.ts, b.ts);
139 static inline bool time_less_(struct timespec a, struct timespec b)
141 if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
143 else if (a.tv_sec > b.tv_sec)
146 return a.tv_nsec < b.tv_nsec;
150 * time_before - is a before b?
151 * @a: one absolute time.
152 * @b: another absolute time.
155 * static bool still_valid(const struct timeabs *start)
157 * #define TIMEOUT time_from_msec(1000)
158 * return time_before(time_now(), timeabs_add(*start, TIMEOUT));
161 static inline bool time_before(struct timeabs a, struct timeabs b)
163 return time_less_(a.ts, b.ts);
167 * time_less - is a before b?
168 * @a: one relative time.
169 * @b: another relative time.
171 static inline bool time_less(struct timerel a, struct timerel b)
173 return time_less_(a.ts, b.ts);
177 * timeabs_eq - is a equal to b?
178 * @a: one absolute time.
179 * @b: another absolute time.
182 * #include <sys/types.h>
183 * #include <sys/wait.h>
185 * // Can we fork in under a nanosecond?
186 * static bool fast_fork(void)
188 * struct timeabs start = time_now();
193 * return timeabs_eq(start, time_now());
196 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
198 return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
199 && a.ts.tv_nsec == b.ts.tv_nsec;
203 * timerel_eq - is a equal to b?
204 * @a: one relative time.
205 * @b: another relative time.
208 * #include <sys/types.h>
209 * #include <sys/wait.h>
211 * // Can we fork in under a nanosecond?
212 * static bool fast_fork(void)
214 * struct timeabs start = time_now();
215 * struct timerel diff, zero = { .ts = { 0, 0 } };
220 * diff = time_between(start, time_now());
221 * return timerel_eq(diff, zero);
224 static inline bool timerel_eq(struct timerel a, struct timerel b)
226 return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
227 && a.ts.tv_nsec == b.ts.tv_nsec;
230 static inline struct timespec time_sub_(struct timespec recent,
233 struct timespec diff;
235 diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
236 if (old.tv_nsec > recent.tv_nsec) {
238 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
240 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
242 return TIME_CHECK(diff);
246 * time_sub - subtract two relative times
247 * @a: the larger time.
248 * @b: the smaller time.
250 * This returns a well formed struct timerel of @a - @b.
252 static inline struct timerel time_sub(struct timerel a, struct timerel b)
256 t.ts = time_sub_(a.ts, b.ts);
261 * time_between - time between two absolute times
262 * @recent: the larger time.
263 * @old: the smaller time.
265 * This returns a well formed struct timerel of @a - @b.
267 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
271 t.ts = time_sub_(recent.ts, old.ts);
276 * timeabs_sub - subtract a relative time from an absolute time
277 * @abs: the absolute time.
278 * @rel: the relative time.
280 * This returns a well formed struct timeabs of @a - @b.
283 * // We do one every second.
284 * static struct timeabs previous_time(void)
286 * return timeabs_sub(time_now(), time_from_msec(1000));
289 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
293 t.ts = time_sub_(abs.ts, rel.ts);
297 static inline struct timespec time_add_(struct timespec a, struct timespec b)
301 sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
302 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
303 if (sum.tv_nsec >= 1000000000) {
305 sum.tv_nsec -= 1000000000;
307 return TIME_CHECK(sum);
311 * timeabs_add - add a relative to an absolute time
312 * @a: the absolute time.
313 * @b: a relative time.
315 * The times must not overflow, or the results are undefined.
318 * // We do one every second.
319 * static struct timeabs next_time(void)
321 * return timeabs_add(time_now(), time_from_msec(1000));
324 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
328 t.ts = time_add_(a.ts, b.ts);
333 * timerel_add - add two relative times
334 * @a: one relative time.
335 * @b: another relative time.
337 * The times must not overflow, or the results are undefined.
340 * static struct timerel double_time(struct timerel a)
342 * return timerel_add(a, a);
345 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
349 t.ts = time_add_(a.ts, b.ts);
354 * time_divide - divide a time by a value.
356 * @div: number to divide it by.
359 * // How long does it take to do a fork?
360 * static struct timerel forking_time(void)
362 * struct timeabs start = time_now();
365 * for (i = 0; i < 1000; i++) {
371 * return time_divide(time_between(time_now(), start), i);
374 struct timerel time_divide(struct timerel t, unsigned long div);
377 * time_multiply - multiply a time by a value.
378 * @t: a relative time.
379 * @mult: number to multiply it by.
383 * printf("Time to do 100000 forks would be %u sec\n",
384 * (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
386 struct timerel time_multiply(struct timerel t, unsigned long mult);
389 * time_to_sec - return number of seconds
392 * It's often more convenient to deal with time values as seconds.
393 * Note that this will fit into an unsigned 32-bit variable if it's a
394 * time of less than about 136 years.
398 * printf("Forking time is %u sec\n",
399 * (unsigned)time_to_sec(forking_time()));
401 static inline uint64_t time_to_sec(struct timerel t)
407 * time_to_msec - return number of milliseconds
408 * @t: a relative time
410 * It's often more convenient to deal with time values as
411 * milliseconds. Note that this will fit into a 32-bit variable if
412 * it's a time difference of less than ~7 weeks.
416 * printf("Forking time is %u msec\n",
417 * (unsigned)time_to_msec(forking_time()));
419 static inline uint64_t time_to_msec(struct timerel t)
423 msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
428 * time_to_usec - return number of microseconds
429 * @t: a relative time
431 * It's often more convenient to deal with time values as
432 * microseconds. Note that this will fit into a 32-bit variable if
433 * it's a time difference of less than ~1 hour.
437 * printf("Forking time is %u usec\n",
438 * (unsigned)time_to_usec(forking_time()));
441 static inline uint64_t time_to_usec(struct timerel t)
445 usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
450 * time_to_nsec - return number of nanoseconds
451 * @t: a relative time
453 * It's sometimes more convenient to deal with time values as
454 * nanoseconds. Note that this will fit into a 32-bit variable if
455 * it's a time difference of less than ~4 seconds.
459 * printf("Forking time is %u nsec\n",
460 * (unsigned)time_to_nsec(forking_time()));
463 static inline uint64_t time_to_nsec(struct timerel t)
467 nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
472 * time_from_sec - convert seconds to a relative time
473 * @msec: time in seconds
476 * // 1 minute timeout
477 * #define TIMEOUT time_from_sec(60)
479 static inline struct timerel time_from_sec(uint64_t sec)
485 return TIMEREL_CHECK(t);
489 * time_from_msec - convert milliseconds to a relative time
490 * @msec: time in milliseconds
493 * // 1/2 second timeout
494 * #define TIMEOUT time_from_msec(500)
496 static inline struct timerel time_from_msec(uint64_t msec)
500 t.ts.tv_nsec = (msec % 1000) * 1000000;
501 t.ts.tv_sec = msec / 1000;
502 return TIMEREL_CHECK(t);
506 * time_from_usec - convert microseconds to a relative time
507 * @usec: time in microseconds
510 * // 1/2 second timeout
511 * #define TIMEOUT time_from_usec(500000)
513 static inline struct timerel time_from_usec(uint64_t usec)
517 t.ts.tv_nsec = (usec % 1000000) * 1000;
518 t.ts.tv_sec = usec / 1000000;
519 return TIMEREL_CHECK(t);
523 * time_from_nsec - convert nanoseconds to a relative time
524 * @nsec: time in nanoseconds
527 * // 1/2 second timeout
528 * #define TIMEOUT time_from_nsec(500000000)
530 static inline struct timerel time_from_nsec(uint64_t nsec)
534 t.ts.tv_nsec = nsec % 1000000000;
535 t.ts.tv_sec = nsec / 1000000000;
536 return TIMEREL_CHECK(t);
539 static inline struct timeval timespec_to_timeval(struct timespec ts)
542 tv.tv_sec = ts.tv_sec;
543 tv.tv_usec = ts.tv_nsec / 1000;
548 * timerel_to_timeval - convert a relative time to a timeval.
549 * @t: a relative time.
552 * struct timerel t = { { 100, 0 } }; // 100 seconds
555 * tv = timerel_to_timeval(t);
556 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
558 static inline struct timeval timerel_to_timeval(struct timerel t)
560 return timespec_to_timeval(t.ts);
564 * timeabs_to_timeval - convert an absolute time to a timeval.
565 * @t: an absolute time.
570 * tv = timeabs_to_timeval(time_now());
571 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
573 static inline struct timeval timeabs_to_timeval(struct timeabs t)
575 return timespec_to_timeval(t.ts);
578 static inline struct timespec timeval_to_timespec(struct timeval tv)
581 ts.tv_sec = tv.tv_sec;
582 ts.tv_nsec = tv.tv_usec * 1000;
587 * timeval_to_timerel - convert a timeval to a relative time.
591 * struct timeval tv = { 0, 500 };
594 * t = timeval_to_timerel(tv);
595 * printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
597 static inline struct timerel timeval_to_timerel(struct timeval tv)
600 t.ts = timeval_to_timespec(tv);
601 return TIMEREL_CHECK(t);
605 * timeval_to_timeabs - convert a timeval to an absolute time.
609 * struct timeval tv = { 1401762008, 500 };
612 * t = timeval_to_timeabs(tv);
613 * printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
615 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
618 t.ts = timeval_to_timespec(tv);
619 return TIMEABS_CHECK(t);
621 #endif /* CCAN_TIME_H */