9 * time_now - return the current time
12 * printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
14 struct timeval time_now(void);
17 * time_greater - is a after b?
22 * static bool timed_out(const struct timeval *start)
24 * #define TIMEOUT time_from_msec(1000)
25 * return time_greater(time_now(), time_add(*start, TIMEOUT));
28 bool time_greater(struct timeval a, struct timeval b);
31 * time_less - is a before b?
36 * static bool still_valid(const struct timeval *start)
38 * #define TIMEOUT time_from_msec(1000)
39 * return time_less(time_now(), time_add(*start, TIMEOUT));
42 bool time_less(struct timeval a, struct timeval b);
45 * time_eq - is a equal to b?
50 * #include <sys/types.h>
51 * #include <sys/wait.h>
53 * // Can we fork in under a microsecond?
54 * static bool fast_fork(void)
56 * struct timeval start = time_now();
61 * return time_eq(start, time_now());
64 bool time_eq(struct timeval a, struct timeval b);
67 * time_sub - subtract two times
68 * @recent: the larger (more recent) time.
69 * @old: the smaller (less recent) time.
71 * This returns a well formed struct timeval.
74 * static bool was_recent(const struct timeval *start)
76 * return time_sub(time_now(), *start).tv_sec < 1;
79 struct timeval time_sub(struct timeval recent, struct timeval old);
82 * time_add - add two times
86 * The times must not overflow, or the results are undefined.
89 * // We do one every second.
90 * static struct timeval next_time(void)
92 * return time_add(time_now(), time_from_msec(1000));
95 struct timeval time_add(struct timeval a, struct timeval b);
98 * time_divide - divide a time by a value.
100 * @div: number to divide it by.
103 * // How long does it take to do a fork?
104 * static struct timeval forking_time(void)
106 * struct timeval start = time_now();
109 * for (i = 0; i < 1000; i++) {
115 * return time_divide(time_sub(time_now(), start), i);
118 struct timeval time_divide(struct timeval t, unsigned long div);
121 * time_multiply - multiply a time by a value.
123 * @mult: number to multiply it by.
127 * printf("Time to do 100000 forks would be %u sec\n",
128 * (unsigned)time_multiply(forking_time(), 1000000).tv_sec);
130 struct timeval time_multiply(struct timeval t, unsigned long mult);
133 * time_to_msec - return number of milliseconds
136 * It's often more convenient to deal with time values as
137 * milliseconds. Note that this will fit into a 32-bit variable if
138 * it's a time difference of less than ~7 weeks.
142 * printf("Forking time is %u msec\n",
143 * (unsigned)time_to_msec(forking_time()));
145 uint64_t time_to_msec(struct timeval t);
148 * time_to_usec - return number of microseconds
151 * It's often more convenient to deal with time values as
152 * microseconds. Note that this will fit into a 32-bit variable if
153 * it's a time difference of less than ~1 hour.
157 * printf("Forking time is %u usec\n",
158 * (unsigned)time_to_usec(forking_time()));
161 uint64_t time_to_usec(struct timeval t);
164 * time_from_msec - convert milliseconds to a timeval
165 * @msec: time in milliseconds
168 * // 1/2 second timeout
169 * #define TIMEOUT time_from_msec(500)
171 struct timeval time_from_msec(uint64_t msec);
174 * time_from_usec - convert microseconds to a timeval
175 * @usec: time in microseconds
178 * // 1/2 second timeout
179 * #define TIMEOUT time_from_usec(500000)
181 struct timeval time_from_usec(uint64_t usec);
183 #endif /* CCAN_TIME_H */