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) ") ")
25 #define TIMEMONO_CHECK(t) \
26 timemono_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
28 #define TIME_CHECK(t) (t)
29 #define TIMEREL_CHECK(t) (t)
30 #define TIMEABS_CHECK(t) (t)
31 #define TIMEMONO_CHECK(t) (t)
35 * struct timerel - a relative time.
36 * @ts: the actual timespec value.
38 * For example, 1 second: ts.tv_sec = 1, ts.tv_nsec = 0
45 * struct timeabs - an absolue time.
46 * @ts: the actual timespec value.
48 * For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
55 * struct timemono - a monotonic time.
56 * @ts: the actual timespec value.
58 * This comes from the monotonic clock (if available), so it's useful
59 * for measuring intervals as it won't change even if the system clock
60 * is moved for some reason.
67 * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock.
69 * Otherwise time_mono() just refers to time_now(). Your code might
70 * test this if you really need a monotonic clock.
72 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
73 #define TIME_HAVE_MONOTONIC 1
75 #define TIME_HAVE_MONOTONIC 0
78 struct timespec time_check_(struct timespec in, const char *abortstr);
81 * timerel_check - check if a relative time is malformed.
82 * @in: the relative time to check (returned)
83 * @abortstr: the string to print to stderr before aborting (if set).
85 * This can be used to make sure a time isn't negative and doesn't
86 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
87 * that will be printed and abort() is called. Otherwise, if
88 * @abortstr is NULL then the returned timerel will be normalized and
89 * tv_sec set to 0 if it was negative.
91 * Note that if ccan/time is compiled with DEBUG, then it will call this
92 * for all passed and returned times.
95 * printf("Time to calc this was %lu nanoseconds\n",
96 * (long)timerel_check(time_between(time_now(), time_now()),
97 * "time_now() failed?").ts.tv_nsec);
99 struct timerel timerel_check(struct timerel in, const char *abortstr);
102 * timeabs_check - check if an absolute time is malformed.
103 * @in: the absolute time to check (returned)
104 * @abortstr: the string to print to stderr before aborting (if set).
106 * This can be used to make sure a time isn't negative and doesn't
107 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
108 * that will be printed and abort() is called. Otherwise, if
109 * @abortstr is NULL then the returned timeabs will be normalized and
110 * tv_sec set to 0 if it was negative.
112 * Note that if ccan/time is compiled with DEBUG, then it will call this
113 * for all passed and returned times.
116 * printf("Now is %lu seconds since epoch\n",
117 * (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
119 struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
122 * timemono_check - check if a monotonic time is malformed.
123 * @in: the monotonic time to check (returned)
124 * @abortstr: the string to print to stderr before aborting (if set).
126 * This can be used to make sure a time isn't negative and doesn't
127 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
128 * that will be printed and abort() is called. Otherwise, if
129 * @abortstr is NULL then the returned timemono will be normalized and
130 * tv_sec set to 0 if it was negative.
132 * Note that if ccan/time is compiled with DEBUG, then it will call this
133 * for all passed and returned times.
136 * printf("Now is %lu seconds since mono start\n",
137 * (long)timemono_check(time_mono(), "time_mono failed?").ts.tv_sec);
139 struct timemono timemono_check(struct timemono in, const char *abortstr);
142 * time_now - return the current time
145 * printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
147 struct timeabs time_now(void);
150 * time_mono - return the current monotonic time
152 * This value is only really useful for measuring time intervals.
157 struct timemono time_mono(void);
159 static inline bool time_greater_(struct timespec a, struct timespec b)
161 if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
163 else if (a.tv_sec < b.tv_sec)
166 return a.tv_nsec > b.tv_nsec;
170 * time_after - is a after b?
172 * @b: another abstime.
175 * static bool timed_out(const struct timeabs *start)
177 * #define TIMEOUT time_from_msec(1000)
178 * return time_after(time_now(), timeabs_add(*start, TIMEOUT));
181 static inline bool time_after(struct timeabs a, struct timeabs b)
183 return time_greater_(a.ts, b.ts);
187 * time_greater - is a greater than b?
189 * @b: another reltime.
191 static inline bool time_greater(struct timerel a, struct timerel b)
193 return time_greater_(a.ts, b.ts);
196 static inline bool time_less_(struct timespec a, struct timespec b)
198 if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
200 else if (a.tv_sec > b.tv_sec)
203 return a.tv_nsec < b.tv_nsec;
207 * time_before - is a before b?
208 * @a: one absolute time.
209 * @b: another absolute time.
212 * static bool still_valid(const struct timeabs *start)
214 * #define TIMEOUT time_from_msec(1000)
215 * return time_before(time_now(), timeabs_add(*start, TIMEOUT));
218 static inline bool time_before(struct timeabs a, struct timeabs b)
220 return time_less_(a.ts, b.ts);
224 * time_less - is a before b?
225 * @a: one relative time.
226 * @b: another relative time.
228 static inline bool time_less(struct timerel a, struct timerel b)
230 return time_less_(a.ts, b.ts);
234 * timeabs_eq - is a equal to b?
235 * @a: one absolute time.
236 * @b: another absolute time.
239 * #include <sys/types.h>
240 * #include <sys/wait.h>
242 * // Can we fork in under a nanosecond?
243 * static bool fast_fork(void)
245 * struct timeabs start = time_now();
250 * return timeabs_eq(start, time_now());
253 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
255 return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
256 && a.ts.tv_nsec == b.ts.tv_nsec;
260 * timemono_eq - is a equal to b?
261 * @a: one monotonic time.
262 * @b: another monotonic time.
265 * #include <sys/types.h>
266 * #include <sys/wait.h>
268 * // Can we fork in under a nanosecond?
269 * static bool fast_fork(void)
271 * struct timemono start = time_mono();
276 * return timemono_eq(start, time_mono());
279 static inline bool timemono_eq(struct timemono a, struct timemono b)
281 return TIMEMONO_CHECK(a).ts.tv_sec == TIMEMONO_CHECK(b).ts.tv_sec
282 && a.ts.tv_nsec == b.ts.tv_nsec;
286 * timerel_eq - is a equal to b?
287 * @a: one relative time.
288 * @b: another relative time.
291 * #include <sys/types.h>
292 * #include <sys/wait.h>
294 * // Can we fork in under a nanosecond?
295 * static bool fast_fork(void)
297 * struct timeabs start = time_now();
298 * struct timerel diff, zero = { .ts = { 0, 0 } };
303 * diff = time_between(time_now(), start);
304 * return timerel_eq(diff, zero);
307 static inline bool timerel_eq(struct timerel a, struct timerel b)
309 return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
310 && a.ts.tv_nsec == b.ts.tv_nsec;
313 static inline struct timespec time_sub_(struct timespec recent,
316 struct timespec diff;
318 diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
319 if (old.tv_nsec > recent.tv_nsec) {
321 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
323 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
325 return TIME_CHECK(diff);
329 * time_sub - subtract two relative times
330 * @a: the larger time.
331 * @b: the smaller time.
333 * This returns a well formed struct timerel of @a - @b.
335 static inline struct timerel time_sub(struct timerel a, struct timerel b)
339 t.ts = time_sub_(a.ts, b.ts);
344 * time_between - time between two absolute times
345 * @recent: the larger time.
346 * @old: the smaller time.
348 * This returns a well formed struct timerel of @a - @b.
350 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
354 t.ts = time_sub_(recent.ts, old.ts);
359 * timemono_between - time between two monotonic times
360 * @recent: the larger time.
361 * @old: the smaller time.
363 * This returns a well formed struct timerel of @recent - @old.
365 static inline struct timerel timemono_between(struct timemono recent,
370 t.ts = time_sub_(recent.ts, old.ts);
375 * timemono_since - elapsed monotonic time since @old
376 * @old: a monotonic time from the past.
378 static inline struct timerel timemono_since(struct timemono old)
380 struct timemono now = time_mono();
382 return timemono_between(now, TIMEMONO_CHECK(old));
386 * timeabs_sub - subtract a relative time from an absolute time
387 * @abs: the absolute time.
388 * @rel: the relative time.
390 * This returns a well formed struct timeabs of @a - @b.
393 * // We do one every second.
394 * static struct timeabs previous_time(void)
396 * return timeabs_sub(time_now(), time_from_msec(1000));
399 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
403 t.ts = time_sub_(abs.ts, rel.ts);
407 static inline struct timespec time_add_(struct timespec a, struct timespec b)
411 sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
412 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
413 if (sum.tv_nsec >= 1000000000) {
415 sum.tv_nsec -= 1000000000;
417 return TIME_CHECK(sum);
421 * timeabs_add - add a relative to an absolute time
422 * @a: the absolute time.
423 * @b: a relative time.
425 * The times must not overflow, or the results are undefined.
428 * // We do one every second.
429 * static struct timeabs next_time(void)
431 * return timeabs_add(time_now(), time_from_msec(1000));
434 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
438 t.ts = time_add_(a.ts, b.ts);
443 * timemono_add - add a relative to a monotonic time
444 * @a: the monotonic time.
445 * @b: a relative time.
447 * The times must not overflow, or the results are undefined.
450 * // We do one every second.
451 * static struct timemono next_timem(void)
453 * return timemono_add(time_mono(), time_from_msec(1000));
456 static inline struct timemono timemono_add(struct timemono a, struct timerel b)
460 t.ts = time_add_(a.ts, b.ts);
465 * timerel_add - add two relative times
466 * @a: one relative time.
467 * @b: another relative time.
469 * The times must not overflow, or the results are undefined.
472 * static struct timerel double_time(struct timerel a)
474 * return timerel_add(a, a);
477 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
481 t.ts = time_add_(a.ts, b.ts);
486 * time_divide - divide a time by a value.
488 * @div: number to divide it by.
491 * // How long does it take to do a fork?
492 * static struct timerel forking_time(void)
494 * struct timeabs start = time_now();
497 * for (i = 0; i < 1000; i++) {
503 * return time_divide(time_between(time_now(), start), i);
506 struct timerel time_divide(struct timerel t, unsigned long div);
509 * time_multiply - multiply a time by a value.
510 * @t: a relative time.
511 * @mult: number to multiply it by.
515 * printf("Time to do 100000 forks would be %u sec\n",
516 * (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
518 struct timerel time_multiply(struct timerel t, unsigned long mult);
521 * time_to_sec - return number of seconds
524 * It's often more convenient to deal with time values as seconds.
525 * Note that this will fit into an unsigned 32-bit variable if it's a
526 * time of less than about 136 years.
530 * printf("Forking time is %u sec\n",
531 * (unsigned)time_to_sec(forking_time()));
533 static inline uint64_t time_to_sec(struct timerel t)
539 * time_to_msec - return number of milliseconds
540 * @t: a relative time
542 * It's often more convenient to deal with time values as
543 * milliseconds. Note that this will fit into a 32-bit variable if
544 * it's a time difference of less than ~7 weeks.
548 * printf("Forking time is %u msec\n",
549 * (unsigned)time_to_msec(forking_time()));
551 static inline uint64_t time_to_msec(struct timerel t)
555 msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
560 * time_to_usec - return number of microseconds
561 * @t: a relative time
563 * It's often more convenient to deal with time values as
564 * microseconds. Note that this will fit into a 32-bit variable if
565 * it's a time difference of less than ~1 hour.
569 * printf("Forking time is %u usec\n",
570 * (unsigned)time_to_usec(forking_time()));
573 static inline uint64_t time_to_usec(struct timerel t)
577 usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
582 * time_to_nsec - return number of nanoseconds
583 * @t: a relative time
585 * It's sometimes more convenient to deal with time values as
586 * nanoseconds. Note that this will fit into a 32-bit variable if
587 * it's a time difference of less than ~4 seconds.
591 * printf("Forking time is %u nsec\n",
592 * (unsigned)time_to_nsec(forking_time()));
595 static inline uint64_t time_to_nsec(struct timerel t)
599 nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
604 * time_from_sec - convert seconds to a relative time
605 * @msec: time in seconds
608 * // 1 minute timeout
609 * #define TIMEOUT time_from_sec(60)
611 static inline struct timerel time_from_sec(uint64_t sec)
617 return TIMEREL_CHECK(t);
621 * time_from_msec - convert milliseconds to a relative time
622 * @msec: time in milliseconds
625 * // 1/2 second timeout
626 * #define TIMEOUT time_from_msec(500)
628 static inline struct timerel time_from_msec(uint64_t msec)
632 t.ts.tv_nsec = (msec % 1000) * 1000000;
633 t.ts.tv_sec = msec / 1000;
634 return TIMEREL_CHECK(t);
638 * time_from_usec - convert microseconds to a relative time
639 * @usec: time in microseconds
642 * // 1/2 second timeout
643 * #define TIMEOUT time_from_usec(500000)
645 static inline struct timerel time_from_usec(uint64_t usec)
649 t.ts.tv_nsec = (usec % 1000000) * 1000;
650 t.ts.tv_sec = usec / 1000000;
651 return TIMEREL_CHECK(t);
655 * time_from_nsec - convert nanoseconds to a relative time
656 * @nsec: time in nanoseconds
659 * // 1/2 second timeout
660 * #define TIMEOUT time_from_nsec(500000000)
662 static inline struct timerel time_from_nsec(uint64_t nsec)
666 t.ts.tv_nsec = nsec % 1000000000;
667 t.ts.tv_sec = nsec / 1000000000;
668 return TIMEREL_CHECK(t);
671 static inline struct timeval timespec_to_timeval(struct timespec ts)
674 tv.tv_sec = ts.tv_sec;
675 tv.tv_usec = ts.tv_nsec / 1000;
680 * timerel_to_timeval - convert a relative time to a timeval.
681 * @t: a relative time.
684 * struct timerel t = { { 100, 0 } }; // 100 seconds
687 * tv = timerel_to_timeval(t);
688 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
690 static inline struct timeval timerel_to_timeval(struct timerel t)
692 return timespec_to_timeval(t.ts);
696 * timeabs_to_timeval - convert an absolute time to a timeval.
697 * @t: an absolute time.
702 * tv = timeabs_to_timeval(time_now());
703 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
705 static inline struct timeval timeabs_to_timeval(struct timeabs t)
707 return timespec_to_timeval(t.ts);
710 static inline struct timespec timeval_to_timespec(struct timeval tv)
713 ts.tv_sec = tv.tv_sec;
714 ts.tv_nsec = tv.tv_usec * 1000;
719 * timeval_to_timerel - convert a timeval to a relative time.
723 * struct timeval tv = { 0, 500 };
726 * t = timeval_to_timerel(tv);
727 * printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
729 static inline struct timerel timeval_to_timerel(struct timeval tv)
732 t.ts = timeval_to_timespec(tv);
733 return TIMEREL_CHECK(t);
737 * timeval_to_timeabs - convert a timeval to an absolute time.
741 * struct timeval tv = { 1401762008, 500 };
744 * t = timeval_to_timeabs(tv);
745 * printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
747 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
750 t.ts = timeval_to_timespec(tv);
751 return TIMEABS_CHECK(t);
753 #endif /* CCAN_TIME_H */