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
52 * struct timemono - a monotonic time.
53 * @ts: the actual timespec value.
55 * This comes from the monotonic clock (if available), so it's useful
56 * for measuring intervals as it won't change even if the system clock
57 * is moved for some reason.
64 * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock.
66 * Otherwise time_mono() just refers to time_now(). Your code might
67 * test this if you really need a monotonic clock.
69 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
70 #define TIME_HAVE_MONOTONIC 1
73 struct timespec time_check_(struct timespec in, const char *abortstr);
76 * timerel_check - check if a relative time is malformed.
77 * @in: the relative time to check (returned)
78 * @abortstr: the string to print to stderr before aborting (if set).
80 * This can be used to make sure a time isn't negative and doesn't
81 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
82 * that will be printed and abort() is called. Otherwise, if
83 * @abortstr is NULL then the returned timerel will be normalized and
84 * tv_sec set to 0 if it was negative.
86 * Note that if ccan/time is compiled with DEBUG, then it will call this
87 * for all passed and returned times.
90 * printf("Time to calc this was %lu nanoseconds\n",
91 * (long)timerel_check(time_between(time_now(), time_now()),
92 * "time_now() failed?").ts.tv_nsec);
94 struct timerel timerel_check(struct timerel in, const char *abortstr);
97 * timeabs_check - check if an absolute time is malformed.
98 * @in: the relative time to check (returned)
99 * @abortstr: the string to print to stderr before aborting (if set).
101 * This can be used to make sure a time isn't negative and doesn't
102 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
103 * that will be printed and abort() is called. Otherwise, if
104 * @abortstr is NULL then the returned timeabs will be normalized and
105 * tv_sec set to 0 if it was negative.
107 * Note that if ccan/time is compiled with DEBUG, then it will call this
108 * for all passed and returned times.
111 * printf("Now is %lu seconds since epoch\n",
112 * (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
114 struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
117 * time_now - return the current time
120 * printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
122 struct timeabs time_now(void);
125 * time_mono - return the current monotonic time
127 * This value is only really useful for measuring time intervals.
132 struct timemono time_mono(void);
134 static inline bool time_greater_(struct timespec a, struct timespec b)
136 if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
138 else if (a.tv_sec < b.tv_sec)
141 return a.tv_nsec > b.tv_nsec;
145 * time_after - is a after b?
147 * @b: another abstime.
150 * static bool timed_out(const struct timeabs *start)
152 * #define TIMEOUT time_from_msec(1000)
153 * return time_after(time_now(), timeabs_add(*start, TIMEOUT));
156 static inline bool time_after(struct timeabs a, struct timeabs b)
158 return time_greater_(a.ts, b.ts);
162 * time_greater - is a greater than b?
164 * @b: another reltime.
166 static inline bool time_greater(struct timerel a, struct timerel b)
168 return time_greater_(a.ts, b.ts);
171 static inline bool time_less_(struct timespec a, struct timespec b)
173 if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
175 else if (a.tv_sec > b.tv_sec)
178 return a.tv_nsec < b.tv_nsec;
182 * time_before - is a before b?
183 * @a: one absolute time.
184 * @b: another absolute time.
187 * static bool still_valid(const struct timeabs *start)
189 * #define TIMEOUT time_from_msec(1000)
190 * return time_before(time_now(), timeabs_add(*start, TIMEOUT));
193 static inline bool time_before(struct timeabs a, struct timeabs b)
195 return time_less_(a.ts, b.ts);
199 * time_less - is a before b?
200 * @a: one relative time.
201 * @b: another relative time.
203 static inline bool time_less(struct timerel a, struct timerel b)
205 return time_less_(a.ts, b.ts);
209 * timeabs_eq - is a equal to b?
210 * @a: one absolute time.
211 * @b: another absolute time.
214 * #include <sys/types.h>
215 * #include <sys/wait.h>
217 * // Can we fork in under a nanosecond?
218 * static bool fast_fork(void)
220 * struct timeabs start = time_now();
225 * return timeabs_eq(start, time_now());
228 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
230 return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
231 && a.ts.tv_nsec == b.ts.tv_nsec;
235 * timerel_eq - is a equal to b?
236 * @a: one relative time.
237 * @b: another relative time.
240 * #include <sys/types.h>
241 * #include <sys/wait.h>
243 * // Can we fork in under a nanosecond?
244 * static bool fast_fork(void)
246 * struct timeabs start = time_now();
247 * struct timerel diff, zero = { .ts = { 0, 0 } };
252 * diff = time_between(time_now(), start);
253 * return timerel_eq(diff, zero);
256 static inline bool timerel_eq(struct timerel a, struct timerel b)
258 return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
259 && a.ts.tv_nsec == b.ts.tv_nsec;
262 static inline struct timespec time_sub_(struct timespec recent,
265 struct timespec diff;
267 diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
268 if (old.tv_nsec > recent.tv_nsec) {
270 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
272 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
274 return TIME_CHECK(diff);
278 * time_sub - subtract two relative times
279 * @a: the larger time.
280 * @b: the smaller time.
282 * This returns a well formed struct timerel of @a - @b.
284 static inline struct timerel time_sub(struct timerel a, struct timerel b)
288 t.ts = time_sub_(a.ts, b.ts);
293 * time_between - time between two absolute times
294 * @recent: the larger time.
295 * @old: the smaller time.
297 * This returns a well formed struct timerel of @a - @b.
299 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
303 t.ts = time_sub_(recent.ts, old.ts);
308 * timemono_between - time between two monotonic times
309 * @recent: the larger time.
310 * @old: the smaller time.
312 * This returns a well formed struct timerel of @recent - @old.
314 static inline struct timerel timemono_between(struct timemono recent,
319 t.ts = time_sub_(recent.ts, old.ts);
324 * timeabs_sub - subtract a relative time from an absolute time
325 * @abs: the absolute time.
326 * @rel: the relative time.
328 * This returns a well formed struct timeabs of @a - @b.
331 * // We do one every second.
332 * static struct timeabs previous_time(void)
334 * return timeabs_sub(time_now(), time_from_msec(1000));
337 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
341 t.ts = time_sub_(abs.ts, rel.ts);
345 static inline struct timespec time_add_(struct timespec a, struct timespec b)
349 sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
350 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
351 if (sum.tv_nsec >= 1000000000) {
353 sum.tv_nsec -= 1000000000;
355 return TIME_CHECK(sum);
359 * timeabs_add - add a relative to an absolute time
360 * @a: the absolute time.
361 * @b: a relative time.
363 * The times must not overflow, or the results are undefined.
366 * // We do one every second.
367 * static struct timeabs next_time(void)
369 * return timeabs_add(time_now(), time_from_msec(1000));
372 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
376 t.ts = time_add_(a.ts, b.ts);
381 * timerel_add - add two relative times
382 * @a: one relative time.
383 * @b: another relative time.
385 * The times must not overflow, or the results are undefined.
388 * static struct timerel double_time(struct timerel a)
390 * return timerel_add(a, a);
393 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
397 t.ts = time_add_(a.ts, b.ts);
402 * time_divide - divide a time by a value.
404 * @div: number to divide it by.
407 * // How long does it take to do a fork?
408 * static struct timerel forking_time(void)
410 * struct timeabs start = time_now();
413 * for (i = 0; i < 1000; i++) {
419 * return time_divide(time_between(time_now(), start), i);
422 struct timerel time_divide(struct timerel t, unsigned long div);
425 * time_multiply - multiply a time by a value.
426 * @t: a relative time.
427 * @mult: number to multiply it by.
431 * printf("Time to do 100000 forks would be %u sec\n",
432 * (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
434 struct timerel time_multiply(struct timerel t, unsigned long mult);
437 * time_to_sec - return number of seconds
440 * It's often more convenient to deal with time values as seconds.
441 * Note that this will fit into an unsigned 32-bit variable if it's a
442 * time of less than about 136 years.
446 * printf("Forking time is %u sec\n",
447 * (unsigned)time_to_sec(forking_time()));
449 static inline uint64_t time_to_sec(struct timerel t)
455 * time_to_msec - return number of milliseconds
456 * @t: a relative time
458 * It's often more convenient to deal with time values as
459 * milliseconds. Note that this will fit into a 32-bit variable if
460 * it's a time difference of less than ~7 weeks.
464 * printf("Forking time is %u msec\n",
465 * (unsigned)time_to_msec(forking_time()));
467 static inline uint64_t time_to_msec(struct timerel t)
471 msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
476 * time_to_usec - return number of microseconds
477 * @t: a relative time
479 * It's often more convenient to deal with time values as
480 * microseconds. Note that this will fit into a 32-bit variable if
481 * it's a time difference of less than ~1 hour.
485 * printf("Forking time is %u usec\n",
486 * (unsigned)time_to_usec(forking_time()));
489 static inline uint64_t time_to_usec(struct timerel t)
493 usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
498 * time_to_nsec - return number of nanoseconds
499 * @t: a relative time
501 * It's sometimes more convenient to deal with time values as
502 * nanoseconds. Note that this will fit into a 32-bit variable if
503 * it's a time difference of less than ~4 seconds.
507 * printf("Forking time is %u nsec\n",
508 * (unsigned)time_to_nsec(forking_time()));
511 static inline uint64_t time_to_nsec(struct timerel t)
515 nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
520 * time_from_sec - convert seconds to a relative time
521 * @msec: time in seconds
524 * // 1 minute timeout
525 * #define TIMEOUT time_from_sec(60)
527 static inline struct timerel time_from_sec(uint64_t sec)
533 return TIMEREL_CHECK(t);
537 * time_from_msec - convert milliseconds to a relative time
538 * @msec: time in milliseconds
541 * // 1/2 second timeout
542 * #define TIMEOUT time_from_msec(500)
544 static inline struct timerel time_from_msec(uint64_t msec)
548 t.ts.tv_nsec = (msec % 1000) * 1000000;
549 t.ts.tv_sec = msec / 1000;
550 return TIMEREL_CHECK(t);
554 * time_from_usec - convert microseconds to a relative time
555 * @usec: time in microseconds
558 * // 1/2 second timeout
559 * #define TIMEOUT time_from_usec(500000)
561 static inline struct timerel time_from_usec(uint64_t usec)
565 t.ts.tv_nsec = (usec % 1000000) * 1000;
566 t.ts.tv_sec = usec / 1000000;
567 return TIMEREL_CHECK(t);
571 * time_from_nsec - convert nanoseconds to a relative time
572 * @nsec: time in nanoseconds
575 * // 1/2 second timeout
576 * #define TIMEOUT time_from_nsec(500000000)
578 static inline struct timerel time_from_nsec(uint64_t nsec)
582 t.ts.tv_nsec = nsec % 1000000000;
583 t.ts.tv_sec = nsec / 1000000000;
584 return TIMEREL_CHECK(t);
587 static inline struct timeval timespec_to_timeval(struct timespec ts)
590 tv.tv_sec = ts.tv_sec;
591 tv.tv_usec = ts.tv_nsec / 1000;
596 * timerel_to_timeval - convert a relative time to a timeval.
597 * @t: a relative time.
600 * struct timerel t = { { 100, 0 } }; // 100 seconds
603 * tv = timerel_to_timeval(t);
604 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
606 static inline struct timeval timerel_to_timeval(struct timerel t)
608 return timespec_to_timeval(t.ts);
612 * timeabs_to_timeval - convert an absolute time to a timeval.
613 * @t: an absolute time.
618 * tv = timeabs_to_timeval(time_now());
619 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
621 static inline struct timeval timeabs_to_timeval(struct timeabs t)
623 return timespec_to_timeval(t.ts);
626 static inline struct timespec timeval_to_timespec(struct timeval tv)
629 ts.tv_sec = tv.tv_sec;
630 ts.tv_nsec = tv.tv_usec * 1000;
635 * timeval_to_timerel - convert a timeval to a relative time.
639 * struct timeval tv = { 0, 500 };
642 * t = timeval_to_timerel(tv);
643 * printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
645 static inline struct timerel timeval_to_timerel(struct timeval tv)
648 t.ts = timeval_to_timespec(tv);
649 return TIMEREL_CHECK(t);
653 * timeval_to_timeabs - convert a timeval to an absolute time.
657 * struct timeval tv = { 1401762008, 500 };
660 * t = timeval_to_timeabs(tv);
661 * printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
663 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
666 t.ts = timeval_to_timespec(tv);
667 return TIMEABS_CHECK(t);
669 #endif /* CCAN_TIME_H */