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 * time_now - return the current time
21 * printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
23 struct timespec time_now(void);
26 * time_greater - is a after b?
31 * static bool timed_out(const struct timespec *start)
33 * #define TIMEOUT time_from_msec(1000)
34 * return time_greater(time_now(), time_add(*start, TIMEOUT));
37 bool time_greater(struct timespec a, struct timespec b);
40 * time_less - is a before b?
45 * static bool still_valid(const struct timespec *start)
47 * #define TIMEOUT time_from_msec(1000)
48 * return time_less(time_now(), time_add(*start, TIMEOUT));
51 bool time_less(struct timespec a, struct timespec b);
54 * time_eq - is a equal to b?
59 * #include <sys/types.h>
60 * #include <sys/wait.h>
62 * // Can we fork in under a microsecond?
63 * static bool fast_fork(void)
65 * struct timespec start = time_now();
70 * return time_eq(start, time_now());
73 bool time_eq(struct timespec a, struct timespec b);
76 * time_sub - subtract two times
77 * @recent: the larger (more recent) time.
78 * @old: the smaller (less recent) time.
80 * This returns a well formed struct timespec.
83 * static bool was_recent(const struct timespec *start)
85 * return time_sub(time_now(), *start).tv_sec < 1;
88 struct timespec time_sub(struct timespec recent, struct timespec old);
91 * time_add - add two times
95 * The times must not overflow, or the results are undefined.
98 * // We do one every second.
99 * static struct timespec next_time(void)
101 * return time_add(time_now(), time_from_msec(1000));
104 struct timespec time_add(struct timespec a, struct timespec b);
107 * time_divide - divide a time by a value.
109 * @div: number to divide it by.
112 * // How long does it take to do a fork?
113 * static struct timespec forking_time(void)
115 * struct timespec start = time_now();
118 * for (i = 0; i < 1000; i++) {
124 * return time_divide(time_sub(time_now(), start), i);
127 struct timespec time_divide(struct timespec t, unsigned long div);
130 * time_multiply - multiply a time by a value.
132 * @mult: number to multiply it by.
136 * printf("Time to do 100000 forks would be %u sec\n",
137 * (unsigned)time_multiply(forking_time(), 1000000).tv_sec);
139 struct timespec time_multiply(struct timespec t, unsigned long mult);
142 * time_to_msec - return number of milliseconds
145 * It's often more convenient to deal with time values as
146 * milliseconds. Note that this will fit into a 32-bit variable if
147 * it's a time difference of less than ~7 weeks.
151 * printf("Forking time is %u msec\n",
152 * (unsigned)time_to_msec(forking_time()));
154 uint64_t time_to_msec(struct timespec t);
157 * time_to_usec - return number of microseconds
160 * It's often more convenient to deal with time values as
161 * microseconds. Note that this will fit into a 32-bit variable if
162 * it's a time difference of less than ~1 hour.
166 * printf("Forking time is %u usec\n",
167 * (unsigned)time_to_usec(forking_time()));
170 uint64_t time_to_usec(struct timespec t);
173 * time_to_nsec - return number of nanoseconds
176 * It's sometimes more convenient to deal with time values as
177 * nanoseconds. Note that this will fit into a 32-bit variable if
178 * it's a time difference of less than ~4 seconds.
182 * printf("Forking time is %u nsec\n",
183 * (unsigned)time_to_nsec(forking_time()));
186 uint64_t time_to_nsec(struct timespec t);
189 * time_from_msec - convert milliseconds to a timespec
190 * @msec: time in milliseconds
193 * // 1/2 second timeout
194 * #define TIMEOUT time_from_msec(500)
196 struct timespec time_from_msec(uint64_t msec);
199 * time_from_usec - convert microseconds to a timespec
200 * @usec: time in microseconds
203 * // 1/2 second timeout
204 * #define TIMEOUT time_from_usec(500000)
206 struct timespec time_from_usec(uint64_t usec);
209 * time_from_nsec - convert nanoseconds to a timespec
210 * @nsec: time in nanoseconds
213 * // 1/2 second timeout
214 * #define TIMEOUT time_from_nsec(500000000)
216 struct timespec time_from_nsec(uint64_t nsec);
219 * timespec_to_timeval - convert a timespec to a timeval.
225 * tv = timespec_to_timeval(time_now());
227 static inline struct timeval timespec_to_timeval(struct timespec ts)
230 tv.tv_sec = ts.tv_sec;
231 tv.tv_usec = ts.tv_nsec / 1000;
236 * timeval_to_timespec - convert a timeval to a timespec.
240 * struct timeval tv = { 0, 500 };
241 * struct timespec ts;
243 * ts = timeval_to_timespec(tv);
245 static inline struct timespec timeval_to_timespec(struct timeval tv)
248 ts.tv_sec = tv.tv_sec;
249 ts.tv_nsec = tv.tv_usec * 1000;
252 #endif /* CCAN_TIME_H */