1 /* Licensed under BSD-MIT - see LICENSE file for details */
10 * time_now - return the current time
13 * printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
15 struct timeval
time_now(void);
18 * time_greater - is a after b?
23 * static bool timed_out(const struct timeval *start)
25 * #define TIMEOUT time_from_msec(1000)
26 * return time_greater(time_now(), time_add(*start, TIMEOUT));
29 bool time_greater(struct timeval a
, struct timeval b
);
32 * time_less - is a before b?
37 * static bool still_valid(const struct timeval *start)
39 * #define TIMEOUT time_from_msec(1000)
40 * return time_less(time_now(), time_add(*start, TIMEOUT));
43 bool time_less(struct timeval a
, struct timeval b
);
46 * time_eq - is a equal to b?
51 * #include <sys/types.h>
52 * #include <sys/wait.h>
54 * // Can we fork in under a microsecond?
55 * static bool fast_fork(void)
57 * struct timeval start = time_now();
62 * return time_eq(start, time_now());
65 bool time_eq(struct timeval a
, struct timeval b
);
68 * time_sub - subtract two times
69 * @recent: the larger (more recent) time.
70 * @old: the smaller (less recent) time.
72 * This returns a well formed struct timeval.
75 * static bool was_recent(const struct timeval *start)
77 * return time_sub(time_now(), *start).tv_sec < 1;
80 struct timeval
time_sub(struct timeval recent
, struct timeval old
);
83 * time_add - add two times
87 * The times must not overflow, or the results are undefined.
90 * // We do one every second.
91 * static struct timeval next_time(void)
93 * return time_add(time_now(), time_from_msec(1000));
96 struct timeval
time_add(struct timeval a
, struct timeval b
);
99 * time_divide - divide a time by a value.
101 * @div: number to divide it by.
104 * // How long does it take to do a fork?
105 * static struct timeval forking_time(void)
107 * struct timeval start = time_now();
110 * for (i = 0; i < 1000; i++) {
116 * return time_divide(time_sub(time_now(), start), i);
119 struct timeval
time_divide(struct timeval t
, unsigned long div
);
122 * time_multiply - multiply a time by a value.
124 * @mult: number to multiply it by.
128 * printf("Time to do 100000 forks would be %u sec\n",
129 * (unsigned)time_multiply(forking_time(), 1000000).tv_sec);
131 struct timeval
time_multiply(struct timeval t
, unsigned long mult
);
134 * time_to_msec - return number of milliseconds
137 * It's often more convenient to deal with time values as
138 * milliseconds. Note that this will fit into a 32-bit variable if
139 * it's a time difference of less than ~7 weeks.
143 * printf("Forking time is %u msec\n",
144 * (unsigned)time_to_msec(forking_time()));
146 uint64_t time_to_msec(struct timeval t
);
149 * time_to_usec - return number of microseconds
152 * It's often more convenient to deal with time values as
153 * microseconds. Note that this will fit into a 32-bit variable if
154 * it's a time difference of less than ~1 hour.
158 * printf("Forking time is %u usec\n",
159 * (unsigned)time_to_usec(forking_time()));
162 uint64_t time_to_usec(struct timeval t
);
165 * time_from_msec - convert milliseconds to a timeval
166 * @msec: time in milliseconds
169 * // 1/2 second timeout
170 * #define TIMEOUT time_from_msec(500)
172 struct timeval
time_from_msec(uint64_t msec
);
175 * time_from_usec - convert microseconds to a timeval
176 * @usec: time in microseconds
179 * // 1/2 second timeout
180 * #define TIMEOUT time_from_usec(500000)
182 struct timeval
time_from_usec(uint64_t usec
);
184 #endif /* CCAN_TIME_H */