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 absolute 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);
197 * timemono_after - is a after b?
198 * @a: one monotonic time.
199 * @b: another monotonic time.
202 * static bool timed_out(const struct timemono *start)
204 * #define TIMEOUT time_from_msec(1000)
205 * return timemono_after(time_mono(), timemono_add(*start, TIMEOUT));
208 static inline bool timemono_after(struct timemono a, struct timemono b)
210 return time_greater_(a.ts, b.ts);
213 static inline bool time_less_(struct timespec a, struct timespec b)
215 if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
217 else if (a.tv_sec > b.tv_sec)
220 return a.tv_nsec < b.tv_nsec;
224 * time_before - is a before b?
225 * @a: one absolute time.
226 * @b: another absolute time.
229 * static bool still_valid(const struct timeabs *start)
231 * #define TIMEOUT time_from_msec(1000)
232 * return time_before(time_now(), timeabs_add(*start, TIMEOUT));
235 static inline bool time_before(struct timeabs a, struct timeabs b)
237 return time_less_(a.ts, b.ts);
241 * timemono_before - is a before b?
242 * @a: one monotonic time.
243 * @b: another monotonic time.
246 * static bool still_valid(const struct timemono *start)
248 * #define TIMEOUT time_from_msec(1000)
249 * return timemono_before(time_mono(), timemono_add(*start, TIMEOUT));
252 static inline bool timemono_before(struct timemono a, struct timemono b)
254 return time_less_(a.ts, b.ts);
258 * time_less - is a before b?
259 * @a: one relative time.
260 * @b: another relative time.
262 static inline bool time_less(struct timerel a, struct timerel b)
264 return time_less_(a.ts, b.ts);
268 * timeabs_eq - is a equal to b?
269 * @a: one absolute time.
270 * @b: another absolute time.
273 * #include <sys/types.h>
274 * #include <sys/wait.h>
276 * // Can we fork in under a nanosecond?
277 * static bool fast_fork(void)
279 * struct timeabs start = time_now();
284 * return timeabs_eq(start, time_now());
287 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
289 return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
290 && a.ts.tv_nsec == b.ts.tv_nsec;
294 * timemono_eq - is a equal to b?
295 * @a: one monotonic time.
296 * @b: another monotonic time.
299 * #include <sys/types.h>
300 * #include <sys/wait.h>
302 * // Can we fork in under a nanosecond?
303 * static bool fast_fork(void)
305 * struct timemono start = time_mono();
310 * return timemono_eq(start, time_mono());
313 static inline bool timemono_eq(struct timemono a, struct timemono b)
315 return TIMEMONO_CHECK(a).ts.tv_sec == TIMEMONO_CHECK(b).ts.tv_sec
316 && a.ts.tv_nsec == b.ts.tv_nsec;
320 * timerel_eq - is a equal to b?
321 * @a: one relative time.
322 * @b: another relative time.
325 * #include <sys/types.h>
326 * #include <sys/wait.h>
328 * // Can we fork in under a nanosecond?
329 * static bool fast_fork(void)
331 * struct timeabs start = time_now();
332 * struct timerel diff, zero = { .ts = { 0, 0 } };
337 * diff = time_between(time_now(), start);
338 * return timerel_eq(diff, zero);
341 static inline bool timerel_eq(struct timerel a, struct timerel b)
343 return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
344 && a.ts.tv_nsec == b.ts.tv_nsec;
347 static inline struct timespec time_sub_(struct timespec recent,
350 struct timespec diff;
352 diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
353 if (old.tv_nsec > recent.tv_nsec) {
355 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
357 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
359 return TIME_CHECK(diff);
363 * time_sub - subtract two relative times
364 * @a: the larger time.
365 * @b: the smaller time.
367 * This returns a well formed struct timerel of @a - @b.
369 static inline struct timerel time_sub(struct timerel a, struct timerel b)
373 t.ts = time_sub_(a.ts, b.ts);
378 * time_between - time between two absolute times
379 * @recent: the larger time.
380 * @old: the smaller time.
382 * This returns a well formed struct timerel of @a - @b.
384 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
388 t.ts = time_sub_(recent.ts, old.ts);
393 * timemono_between - time between two monotonic times
394 * @recent: the larger time.
395 * @old: the smaller time.
397 * This returns a well formed struct timerel of @recent - @old.
399 static inline struct timerel timemono_between(struct timemono recent,
404 t.ts = time_sub_(recent.ts, old.ts);
409 * timemono_since - elapsed monotonic time since @old
410 * @old: a monotonic time from the past.
412 static inline struct timerel timemono_since(struct timemono old)
414 struct timemono now = time_mono();
416 return timemono_between(now, TIMEMONO_CHECK(old));
420 * timeabs_sub - subtract a relative time from an absolute time
421 * @abs: the absolute time.
422 * @rel: the relative time.
424 * This returns a well formed struct timeabs of @a - @b.
427 * // We do one every second.
428 * static struct timeabs previous_time(void)
430 * return timeabs_sub(time_now(), time_from_msec(1000));
433 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
437 t.ts = time_sub_(abs.ts, rel.ts);
442 * timemono_sub - subtract a relative time from a monotonic time
443 * @mono: the monotonic time.
444 * @rel: the relative time.
446 * This returns a well formed struct timemono of @mono - @rel.
449 * // We do one every second.
450 * static struct timemono previous_time(void)
452 * return timemono_sub(time_mono(), time_from_msec(1000));
455 static inline struct timemono timemono_sub(struct timemono mono, struct timerel rel)
459 t.ts = time_sub_(mono.ts, rel.ts);
464 static inline struct timespec time_add_(struct timespec a, struct timespec b)
468 sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
469 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
470 if (sum.tv_nsec >= 1000000000) {
472 sum.tv_nsec -= 1000000000;
474 return TIME_CHECK(sum);
478 * timeabs_add - add a relative to an absolute time
479 * @a: the absolute time.
480 * @b: a relative time.
482 * The times must not overflow, or the results are undefined.
485 * // We do one every second.
486 * static struct timeabs next_time(void)
488 * return timeabs_add(time_now(), time_from_msec(1000));
491 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
495 t.ts = time_add_(a.ts, b.ts);
500 * timemono_add - add a relative to a monotonic time
501 * @a: the monotonic time.
502 * @b: a relative time.
504 * The times must not overflow, or the results are undefined.
507 * // We do one every second.
508 * static struct timemono next_timem(void)
510 * return timemono_add(time_mono(), time_from_msec(1000));
513 static inline struct timemono timemono_add(struct timemono a, struct timerel b)
517 t.ts = time_add_(a.ts, b.ts);
522 * timerel_add - add two relative times
523 * @a: one relative time.
524 * @b: another relative time.
526 * The times must not overflow, or the results are undefined.
529 * static struct timerel double_time(struct timerel a)
531 * return timerel_add(a, a);
534 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
538 t.ts = time_add_(a.ts, b.ts);
543 * time_divide - divide a time by a value.
545 * @div: number to divide it by.
548 * #include <sys/wait.h>
550 * // How long does it take to do a fork?
551 * static struct timerel forking_time(void)
553 * struct timeabs start = time_now();
556 * for (i = 0; i < 1000; i++) {
562 * return time_divide(time_between(time_now(), start), i);
565 struct timerel time_divide(struct timerel t, unsigned long div);
568 * time_multiply - multiply a time by a value.
569 * @t: a relative time.
570 * @mult: number to multiply it by.
574 * printf("Time to do 100000 forks would be %u sec\n",
575 * (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
577 struct timerel time_multiply(struct timerel t, unsigned long mult);
580 * time_to_sec - return number of seconds
583 * It's often more convenient to deal with time values as seconds.
584 * Note that this will fit into an unsigned 32-bit variable if it's a
585 * time of less than about 136 years.
589 * printf("Forking time is %u sec\n",
590 * (unsigned)time_to_sec(forking_time()));
592 static inline uint64_t time_to_sec(struct timerel t)
598 * time_to_msec - return number of milliseconds
599 * @t: a relative time
601 * It's often more convenient to deal with time values as
602 * milliseconds. Note that this will fit into a 32-bit variable if
603 * it's a time difference of less than ~7 weeks.
607 * printf("Forking time is %u msec\n",
608 * (unsigned)time_to_msec(forking_time()));
610 static inline uint64_t time_to_msec(struct timerel t)
614 msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
619 * time_to_usec - return number of microseconds
620 * @t: a relative time
622 * It's often more convenient to deal with time values as
623 * microseconds. Note that this will fit into a 32-bit variable if
624 * it's a time difference of less than ~1 hour.
628 * printf("Forking time is %u usec\n",
629 * (unsigned)time_to_usec(forking_time()));
632 static inline uint64_t time_to_usec(struct timerel t)
636 usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
641 * time_to_nsec - return number of nanoseconds
642 * @t: a relative time
644 * It's sometimes more convenient to deal with time values as
645 * nanoseconds. Note that this will fit into a 32-bit variable if
646 * it's a time difference of less than ~4 seconds.
650 * printf("Forking time is %u nsec\n",
651 * (unsigned)time_to_nsec(forking_time()));
654 static inline uint64_t time_to_nsec(struct timerel t)
658 nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
663 * time_from_sec - convert seconds to a relative time
664 * @msec: time in seconds
667 * // 1 minute timeout
668 * #define TIMEOUT time_from_sec(60)
670 static inline struct timerel time_from_sec(uint64_t sec)
676 return TIMEREL_CHECK(t);
680 * time_from_msec - convert milliseconds to a relative time
681 * @msec: time in milliseconds
684 * // 1/2 second timeout
685 * #define TIMEOUT time_from_msec(500)
687 static inline struct timerel time_from_msec(uint64_t msec)
691 t.ts.tv_nsec = (msec % 1000) * 1000000;
692 t.ts.tv_sec = msec / 1000;
693 return TIMEREL_CHECK(t);
697 * time_from_usec - convert microseconds to a relative time
698 * @usec: time in microseconds
701 * // 1/2 second timeout
702 * #define TIMEOUT time_from_usec(500000)
704 static inline struct timerel time_from_usec(uint64_t usec)
708 t.ts.tv_nsec = (usec % 1000000) * 1000;
709 t.ts.tv_sec = usec / 1000000;
710 return TIMEREL_CHECK(t);
714 * time_from_nsec - convert nanoseconds to a relative time
715 * @nsec: time in nanoseconds
718 * // 1/2 second timeout
719 * #define TIMEOUT time_from_nsec(500000000)
721 static inline struct timerel time_from_nsec(uint64_t nsec)
725 t.ts.tv_nsec = nsec % 1000000000;
726 t.ts.tv_sec = nsec / 1000000000;
727 return TIMEREL_CHECK(t);
730 static inline struct timeval timespec_to_timeval(struct timespec ts)
733 tv.tv_sec = ts.tv_sec;
734 tv.tv_usec = ts.tv_nsec / 1000;
739 * timerel_to_timeval - convert a relative time to a timeval.
740 * @t: a relative time.
743 * struct timerel t = { { 100, 0 } }; // 100 seconds
746 * tv = timerel_to_timeval(t);
747 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
749 static inline struct timeval timerel_to_timeval(struct timerel t)
751 return timespec_to_timeval(t.ts);
755 * timeabs_to_timeval - convert an absolute time to a timeval.
756 * @t: an absolute time.
761 * tv = timeabs_to_timeval(time_now());
762 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
764 static inline struct timeval timeabs_to_timeval(struct timeabs t)
766 return timespec_to_timeval(t.ts);
769 static inline struct timespec timeval_to_timespec(struct timeval tv)
772 ts.tv_sec = tv.tv_sec;
773 ts.tv_nsec = tv.tv_usec * 1000;
778 * timeval_to_timerel - convert a timeval to a relative time.
782 * struct timeval tv = { 0, 500 };
785 * t = timeval_to_timerel(tv);
786 * printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
788 static inline struct timerel timeval_to_timerel(struct timeval tv)
791 t.ts = timeval_to_timespec(tv);
792 return TIMEREL_CHECK(t);
796 * timeval_to_timeabs - convert a timeval to an absolute time.
800 * struct timeval tv = { 1401762008, 500 };
803 * t = timeval_to_timeabs(tv);
804 * printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
806 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
809 t.ts = timeval_to_timespec(tv);
810 return TIMEABS_CHECK(t);
812 #endif /* CCAN_TIME_H */