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) ") ")
22 #define TIME_CHECK(t) (t)
26 * time_check - check if a time is malformed.
27 * @in: the time to check (returned)
28 * @abortstr: the string to print to stderr before aborting (if set).
30 * This can be used to make sure a time isn't negative and doesn't
31 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
32 * that will be printed and abort() is called. Otherwise, if
33 * @abortstr is NULL then the returned timespec will be normalized and
34 * tv_sec set to 0 if it was negative.
36 * Note that if ccan/time is compiled with DEBUG, then it will call this
37 * for all passed and returned times.
40 * printf("Now is %lu seconds since epoch\n",
41 * (long)time_check(time_now(), "time_now() failed?").tv_sec);
43 struct timespec time_check(struct timespec in, const char *abortstr);
46 * time_now - return the current time
49 * printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
51 struct timespec time_now(void);
54 * time_greater - is a after b?
59 * static bool timed_out(const struct timespec *start)
61 * #define TIMEOUT time_from_msec(1000)
62 * return time_greater(time_now(), time_add(*start, TIMEOUT));
65 static inline bool time_greater(struct timespec a, struct timespec b)
67 if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
69 else if (a.tv_sec < b.tv_sec)
72 return a.tv_nsec > b.tv_nsec;
76 * time_less - is a before b?
81 * static bool still_valid(const struct timespec *start)
83 * #define TIMEOUT time_from_msec(1000)
84 * return time_less(time_now(), time_add(*start, TIMEOUT));
87 static inline bool time_less(struct timespec a, struct timespec b)
89 if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
91 else if (a.tv_sec > b.tv_sec)
94 return a.tv_nsec < b.tv_nsec;
98 * time_eq - is a equal to b?
103 * #include <sys/types.h>
104 * #include <sys/wait.h>
106 * // Can we fork in under a nanosecond?
107 * static bool fast_fork(void)
109 * struct timespec start = time_now();
114 * return time_eq(start, time_now());
117 static inline bool time_eq(struct timespec a, struct timespec b)
119 return TIME_CHECK(a).tv_sec == TIME_CHECK(b).tv_sec
120 && a.tv_nsec == b.tv_nsec;
124 * time_sub - subtract two times
125 * @recent: the larger (more recent) time.
126 * @old: the smaller (less recent) time.
128 * This returns a well formed struct timespec.
131 * static bool was_recent(const struct timespec *start)
133 * return time_sub(time_now(), *start).tv_sec < 1;
136 static inline struct timespec time_sub(struct timespec recent,
139 struct timespec diff;
141 diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
142 if (old.tv_nsec > recent.tv_nsec) {
144 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
146 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
148 return TIME_CHECK(diff);
152 * time_add - add two times
156 * The times must not overflow, or the results are undefined.
159 * // We do one every second.
160 * static struct timespec next_time(void)
162 * return time_add(time_now(), time_from_msec(1000));
165 static inline struct timespec time_add(struct timespec a, struct timespec b)
169 sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
170 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
171 if (sum.tv_nsec >= 1000000000) {
173 sum.tv_nsec -= 1000000000;
175 return TIME_CHECK(sum);
179 * time_divide - divide a time by a value.
181 * @div: number to divide it by.
184 * // How long does it take to do a fork?
185 * static struct timespec forking_time(void)
187 * struct timespec start = time_now();
190 * for (i = 0; i < 1000; i++) {
196 * return time_divide(time_sub(time_now(), start), i);
199 struct timespec time_divide(struct timespec t, unsigned long div);
202 * time_multiply - multiply a time by a value.
204 * @mult: number to multiply it by.
208 * printf("Time to do 100000 forks would be %u sec\n",
209 * (unsigned)time_multiply(forking_time(), 1000000).tv_sec);
211 struct timespec time_multiply(struct timespec t, unsigned long mult);
214 * time_to_msec - return number of milliseconds
217 * It's often more convenient to deal with time values as
218 * milliseconds. Note that this will fit into a 32-bit variable if
219 * it's a time difference of less than ~7 weeks.
223 * printf("Forking time is %u msec\n",
224 * (unsigned)time_to_msec(forking_time()));
226 static inline uint64_t time_to_msec(struct timespec t)
230 msec = TIME_CHECK(t).tv_nsec / 1000000 + (uint64_t)t.tv_sec * 1000;
235 * time_to_usec - return number of microseconds
238 * It's often more convenient to deal with time values as
239 * microseconds. Note that this will fit into a 32-bit variable if
240 * it's a time difference of less than ~1 hour.
244 * printf("Forking time is %u usec\n",
245 * (unsigned)time_to_usec(forking_time()));
248 static inline uint64_t time_to_usec(struct timespec t)
252 usec = TIME_CHECK(t).tv_nsec / 1000 + (uint64_t)t.tv_sec * 1000000;
257 * time_to_nsec - return number of nanoseconds
260 * It's sometimes more convenient to deal with time values as
261 * nanoseconds. Note that this will fit into a 32-bit variable if
262 * it's a time difference of less than ~4 seconds.
266 * printf("Forking time is %u nsec\n",
267 * (unsigned)time_to_nsec(forking_time()));
270 static inline uint64_t time_to_nsec(struct timespec t)
274 nsec = TIME_CHECK(t).tv_nsec + (uint64_t)t.tv_sec * 1000000000;
279 * time_from_msec - convert milliseconds to a timespec
280 * @msec: time in milliseconds
283 * // 1/2 second timeout
284 * #define TIMEOUT time_from_msec(500)
286 static inline struct timespec time_from_msec(uint64_t msec)
290 t.tv_nsec = (msec % 1000) * 1000000;
291 t.tv_sec = msec / 1000;
292 return TIME_CHECK(t);
296 * time_from_usec - convert microseconds to a timespec
297 * @usec: time in microseconds
300 * // 1/2 second timeout
301 * #define TIMEOUT time_from_usec(500000)
303 static inline struct timespec time_from_usec(uint64_t usec)
307 t.tv_nsec = (usec % 1000000) * 1000;
308 t.tv_sec = usec / 1000000;
309 return TIME_CHECK(t);
313 * time_from_nsec - convert nanoseconds to a timespec
314 * @nsec: time in nanoseconds
317 * // 1/2 second timeout
318 * #define TIMEOUT time_from_nsec(500000000)
320 static inline struct timespec time_from_nsec(uint64_t nsec)
324 t.tv_nsec = nsec % 1000000000;
325 t.tv_sec = nsec / 1000000000;
326 return TIME_CHECK(t);
330 * timespec_to_timeval - convert a timespec to a timeval.
336 * tv = timespec_to_timeval(time_now());
338 static inline struct timeval timespec_to_timeval(struct timespec ts)
341 tv.tv_sec = ts.tv_sec;
342 tv.tv_usec = ts.tv_nsec / 1000;
347 * timeval_to_timespec - convert a timeval to a timespec.
351 * struct timeval tv = { 0, 500 };
352 * struct timespec ts;
354 * ts = timeval_to_timespec(tv);
356 static inline struct timespec timeval_to_timespec(struct timeval tv)
359 ts.tv_sec = tv.tv_sec;
360 ts.tv_nsec = tv.tv_usec * 1000;
363 #endif /* CCAN_TIME_H */