]> git.ozlabs.org Git - ccan/blob - ccan/time/time.h
fdpass: fix complilation on FreeBSD.
[ccan] / ccan / time / time.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_TIME_H
3 #define CCAN_TIME_H
4 #include "config.h"
5 #include <sys/time.h>
6 #if HAVE_STRUCT_TIMESPEC
7 #include <time.h>
8 #else
9 struct timespec {
10         time_t   tv_sec;        /* seconds */
11         long     tv_nsec;       /* nanoseconds */
12 };
13 #endif
14 #include <stdint.h>
15 #include <stdbool.h>
16
17 #ifdef DEBUG
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) ") ")
27 #else
28 #define TIME_CHECK(t) (t)
29 #define TIMEREL_CHECK(t) (t)
30 #define TIMEABS_CHECK(t) (t)
31 #define TIMEMONO_CHECK(t) (t)
32 #endif
33
34 /**
35  * struct timerel - a relative time.
36  * @ts: the actual timespec value.
37  *
38  * For example, 1 second: ts.tv_sec = 1, ts.tv_nsec = 0
39  */
40 struct timerel {
41         struct timespec ts;
42 };
43
44 /**
45  * struct timeabs - an absolute time.
46  * @ts: the actual timespec value.
47  *
48  * For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
49  */
50 struct timeabs {
51         struct timespec ts;
52 };
53
54 /**
55  * struct timemono - a monotonic time.
56  * @ts: the actual timespec value.
57  *
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.
61  */
62 struct timemono {
63         struct timespec ts;
64 };
65
66 /**
67  * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock.
68  *
69  * Otherwise time_mono() just refers to time_now().  Your code might
70  * test this if you really need a monotonic clock.
71  */
72 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
73 #define TIME_HAVE_MONOTONIC 1
74 #else
75 #define TIME_HAVE_MONOTONIC 0
76 #endif
77
78 struct timespec time_check_(struct timespec in, const char *abortstr);
79
80 /**
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).
84  *
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.
90  *
91  * Note that if ccan/time is compiled with DEBUG, then it will call this
92  * for all passed and returned times.
93  *
94  * Example:
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);
98  */
99 struct timerel timerel_check(struct timerel in, const char *abortstr);
100
101 /**
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).
105  *
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.
111  *
112  * Note that if ccan/time is compiled with DEBUG, then it will call this
113  * for all passed and returned times.
114  *
115  * Example:
116  *      printf("Now is %lu seconds since epoch\n",
117  *              (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
118  */
119 struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
120
121 /**
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).
125  *
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.
131  *
132  * Note that if ccan/time is compiled with DEBUG, then it will call this
133  * for all passed and returned times.
134  *
135  * Example:
136  *      printf("Now is %lu seconds since mono start\n",
137  *              (long)timemono_check(time_mono(), "time_mono failed?").ts.tv_sec);
138  */
139 struct timemono timemono_check(struct timemono in, const char *abortstr);
140
141 /**
142  * time_now - return the current time
143  *
144  * Example:
145  *      printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
146  */
147 struct timeabs time_now(void);
148
149 /**
150  * time_mono - return the current monotonic time
151  *
152  * This value is only really useful for measuring time intervals.
153  *
154  * See also:
155  *      timemono_since()
156  */
157 struct timemono time_mono(void);
158
159 static inline bool time_greater_(struct timespec a, struct timespec b)
160 {
161         if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
162                 return true;
163         else if (a.tv_sec < b.tv_sec)
164                  return false;
165
166         return a.tv_nsec > b.tv_nsec;
167 }
168
169 /**
170  * time_after - is a after b?
171  * @a: one abstime.
172  * @b: another abstime.
173  *
174  * Example:
175  *      static bool timed_out(const struct timeabs *start)
176  *      {
177  *      #define TIMEOUT time_from_msec(1000)
178  *              return time_after(time_now(), timeabs_add(*start, TIMEOUT));
179  *      }
180  */
181 static inline bool time_after(struct timeabs a, struct timeabs b)
182 {
183         return time_greater_(a.ts, b.ts);
184 }
185
186 /**
187  * time_greater - is a greater than b?
188  * @a: one reltime.
189  * @b: another reltime.
190  */
191 static inline bool time_greater(struct timerel a, struct timerel b)
192 {
193         return time_greater_(a.ts, b.ts);
194 }
195
196 /**
197  * timemono_after - is a after b?
198  * @a: one monotonic time.
199  * @b: another monotonic time.
200  *
201  * Example:
202  *      static bool timed_out(const struct timemono *start)
203  *      {
204  *      #define TIMEOUT time_from_msec(1000)
205  *              return timemono_after(time_mono(), timemono_add(*start, TIMEOUT));
206  *      }
207  */
208 static inline bool timemono_after(struct timemono a, struct timemono b)
209 {
210         return time_greater_(a.ts, b.ts);
211 }
212
213 static inline bool time_less_(struct timespec a, struct timespec b)
214 {
215         if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
216                 return true;
217         else if (a.tv_sec > b.tv_sec)
218                  return false;
219
220         return a.tv_nsec < b.tv_nsec;
221 }
222
223 /**
224  * time_before - is a before b?
225  * @a: one absolute time.
226  * @b: another absolute time.
227  *
228  * Example:
229  *      static bool still_valid(const struct timeabs *start)
230  *      {
231  *      #define TIMEOUT time_from_msec(1000)
232  *              return time_before(time_now(), timeabs_add(*start, TIMEOUT));
233  *      }
234  */
235 static inline bool time_before(struct timeabs a, struct timeabs b)
236 {
237         return time_less_(a.ts, b.ts);
238 }
239
240 /**
241  * timemono_before - is a before b?
242  * @a: one monotonic time.
243  * @b: another monotonic time.
244  *
245  * Example:
246  *      static bool still_valid(const struct timemono *start)
247  *      {
248  *      #define TIMEOUT time_from_msec(1000)
249  *              return timemono_before(time_mono(), timemono_add(*start, TIMEOUT));
250  *      }
251  */
252 static inline bool timemono_before(struct timemono a, struct timemono b)
253 {
254         return time_less_(a.ts, b.ts);
255 }
256
257 /**
258  * time_less - is a before b?
259  * @a: one relative time.
260  * @b: another relative time.
261  */
262 static inline bool time_less(struct timerel a, struct timerel b)
263 {
264         return time_less_(a.ts, b.ts);
265 }
266
267 /**
268  * timeabs_eq - is a equal to b?
269  * @a: one absolute time.
270  * @b: another absolute time.
271  *
272  * Example:
273  *      #include <sys/types.h>
274  *      #include <sys/wait.h>
275  *
276  *      // Can we fork in under a nanosecond?
277  *      static bool fast_fork(void)
278  *      {
279  *              struct timeabs start = time_now();
280  *              if (fork() != 0) {
281  *                      exit(0);
282  *              }
283  *              wait(NULL);
284  *              return timeabs_eq(start, time_now());
285  *      }
286  */
287 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
288 {
289         return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
290                 && a.ts.tv_nsec == b.ts.tv_nsec;
291 }
292
293 /**
294  * timemono_eq - is a equal to b?
295  * @a: one monotonic time.
296  * @b: another monotonic time.
297  *
298  * Example:
299  *      #include <sys/types.h>
300  *      #include <sys/wait.h>
301  *
302  *      // Can we fork in under a nanosecond?
303  *      static bool fast_fork(void)
304  *      {
305  *              struct timemono start = time_mono();
306  *              if (fork() != 0) {
307  *                      exit(0);
308  *              }
309  *              wait(NULL);
310  *              return timemono_eq(start, time_mono());
311  *      }
312  */
313 static inline bool timemono_eq(struct timemono a, struct timemono b)
314 {
315         return TIMEMONO_CHECK(a).ts.tv_sec == TIMEMONO_CHECK(b).ts.tv_sec
316                 && a.ts.tv_nsec == b.ts.tv_nsec;
317 }
318
319 /**
320  * timerel_eq - is a equal to b?
321  * @a: one relative time.
322  * @b: another relative time.
323  *
324  * Example:
325  *      #include <sys/types.h>
326  *      #include <sys/wait.h>
327  *
328  *      // Can we fork in under a nanosecond?
329  *      static bool fast_fork(void)
330  *      {
331  *              struct timeabs start = time_now();
332  *              struct timerel diff, zero = { .ts = { 0, 0 } };
333  *              if (fork() != 0) {
334  *                      exit(0);
335  *              }
336  *              wait(NULL);
337  *              diff = time_between(time_now(), start);
338  *              return timerel_eq(diff, zero);
339  *      }
340  */
341 static inline bool timerel_eq(struct timerel a, struct timerel b)
342 {
343         return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
344                 && a.ts.tv_nsec == b.ts.tv_nsec;
345 }
346
347 static inline struct timespec time_sub_(struct timespec recent,
348                                         struct timespec old)
349 {
350         struct timespec diff;
351
352         diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
353         if (old.tv_nsec > recent.tv_nsec) {
354                 diff.tv_sec--;
355                 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
356         } else
357                 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
358
359         return TIME_CHECK(diff);
360 }
361
362 /**
363  * time_sub - subtract two relative times
364  * @a: the larger time.
365  * @b: the smaller time.
366  *
367  * This returns a well formed struct timerel of @a - @b.
368  */
369 static inline struct timerel time_sub(struct timerel a, struct timerel b)
370 {
371         struct timerel t;
372
373         t.ts = time_sub_(a.ts, b.ts);
374         return t;
375 }
376
377 /**
378  * time_between - time between two absolute times
379  * @recent: the larger time.
380  * @old: the smaller time.
381  *
382  * This returns a well formed struct timerel of @a - @b.
383  */
384 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
385 {
386         struct timerel t;
387
388         t.ts = time_sub_(recent.ts, old.ts);
389         return t;
390 }
391
392 /**
393  * timemono_between - time between two monotonic times
394  * @recent: the larger time.
395  * @old: the smaller time.
396  *
397  * This returns a well formed struct timerel of @recent - @old.
398  */
399 static inline struct timerel timemono_between(struct timemono recent,
400                                               struct timemono old)
401 {
402         struct timerel t;
403
404         t.ts = time_sub_(recent.ts, old.ts);
405         return t;
406 }
407
408 /**
409  * timemono_since - elapsed monotonic time since @old
410  * @old: a monotonic time from the past.
411  */
412 static inline struct timerel timemono_since(struct timemono old)
413 {
414         struct timemono now = time_mono();
415
416         return timemono_between(now, TIMEMONO_CHECK(old));
417 }
418
419 /**
420  * timeabs_sub - subtract a relative time from an absolute time
421  * @abs: the absolute time.
422  * @rel: the relative time.
423  *
424  * This returns a well formed struct timeabs of @a - @b.
425  *
426  * Example:
427  *      // We do one every second.
428  *      static struct timeabs previous_time(void)
429  *      {
430  *              return timeabs_sub(time_now(), time_from_msec(1000));
431  *      }
432  */
433 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
434 {
435         struct timeabs t;
436
437         t.ts = time_sub_(abs.ts, rel.ts);
438         return t;
439 }
440
441 /**
442  * timemono_sub - subtract a relative time from a monotonic time
443  * @mono: the monotonic time.
444  * @rel: the relative time.
445  *
446  * This returns a well formed struct timemono of @mono - @rel.
447  *
448  * Example:
449  *      // We do one every second.
450  *      static struct timemono previous_time(void)
451  *      {
452  *              return timemono_sub(time_mono(), time_from_msec(1000));
453  *      }
454  */
455 static inline struct timemono timemono_sub(struct timemono mono, struct timerel rel)
456 {
457         struct timemono t;
458
459         t.ts = time_sub_(mono.ts, rel.ts);
460         return t;
461 }
462
463
464 static inline struct timespec time_add_(struct timespec a, struct timespec b)
465 {
466         struct timespec sum;
467
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) {
471                 sum.tv_sec++;
472                 sum.tv_nsec -= 1000000000;
473         }
474         return TIME_CHECK(sum);
475 }
476
477 /**
478  * timeabs_add - add a relative to an absolute time
479  * @a: the absolute time.
480  * @b: a relative time.
481  *
482  * The times must not overflow, or the results are undefined.
483  *
484  * Example:
485  *      // We do one every second.
486  *      static struct timeabs next_time(void)
487  *      {
488  *              return timeabs_add(time_now(), time_from_msec(1000));
489  *      }
490  */
491 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
492 {
493         struct timeabs t;
494
495         t.ts = time_add_(a.ts, b.ts);
496         return t;
497 }
498
499 /**
500  * timemono_add - add a relative to a monotonic time
501  * @a: the monotonic time.
502  * @b: a relative time.
503  *
504  * The times must not overflow, or the results are undefined.
505  *
506  * Example:
507  *      // We do one every second.
508  *      static struct timemono next_timem(void)
509  *      {
510  *              return timemono_add(time_mono(), time_from_msec(1000));
511  *      }
512  */
513 static inline struct timemono timemono_add(struct timemono a, struct timerel b)
514 {
515         struct timemono t;
516
517         t.ts = time_add_(a.ts, b.ts);
518         return t;
519 }
520
521 /**
522  * timerel_add - add two relative times
523  * @a: one relative time.
524  * @b: another relative time.
525  *
526  * The times must not overflow, or the results are undefined.
527  *
528  * Example:
529  *      static struct timerel double_time(struct timerel a)
530  *      {
531  *              return timerel_add(a, a);
532  *      }
533  */
534 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
535 {
536         struct timerel t;
537
538         t.ts = time_add_(a.ts, b.ts);
539         return t;
540 }
541
542 /**
543  * time_divide - divide a time by a value.
544  * @t: a time.
545  * @div: number to divide it by.
546  *
547  * Example:
548  *      #include <sys/wait.h>
549  *
550  *      // How long does it take to do a fork?
551  *      static struct timerel forking_time(void)
552  *      {
553  *              struct timeabs start = time_now();
554  *              unsigned int i;
555  *
556  *              for (i = 0; i < 1000; i++) {
557  *                      if (fork() != 0) {
558  *                              exit(0);
559  *                      }
560  *                      wait(NULL);
561  *              }
562  *              return time_divide(time_between(time_now(), start), i);
563  *      }
564  */
565 struct timerel time_divide(struct timerel t, unsigned long div);
566
567 /**
568  * time_multiply - multiply a time by a value.
569  * @t: a relative time.
570  * @mult: number to multiply it by.
571  *
572  * Example:
573  *      ...
574  *      printf("Time to do 100000 forks would be %u sec\n",
575  *             (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
576  */
577 struct timerel time_multiply(struct timerel t, unsigned long mult);
578
579 /**
580  * time_to_sec - return number of seconds
581  * @t: a time
582  *
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.
586  *
587  * Example:
588  *      ...
589  *      printf("Forking time is %u sec\n",
590  *             (unsigned)time_to_sec(forking_time()));
591  */
592 static inline uint64_t time_to_sec(struct timerel t)
593 {
594         return t.ts.tv_sec;
595 }
596
597 /**
598  * time_to_msec - return number of milliseconds
599  * @t: a relative time
600  *
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.
604  *
605  * Example:
606  *      ...
607  *      printf("Forking time is %u msec\n",
608  *             (unsigned)time_to_msec(forking_time()));
609  */
610 static inline uint64_t time_to_msec(struct timerel t)
611 {
612         uint64_t msec;
613
614         msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
615         return msec;
616 }
617
618 /**
619  * time_to_usec - return number of microseconds
620  * @t: a relative time
621  *
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.
625  *
626  * Example:
627  *      ...
628  *      printf("Forking time is %u usec\n",
629  *             (unsigned)time_to_usec(forking_time()));
630  *
631  */
632 static inline uint64_t time_to_usec(struct timerel t)
633 {
634         uint64_t usec;
635
636         usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
637         return usec;
638 }
639
640 /**
641  * time_to_nsec - return number of nanoseconds
642  * @t: a relative time
643  *
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.
647  *
648  * Example:
649  *      ...
650  *      printf("Forking time is %u nsec\n",
651  *             (unsigned)time_to_nsec(forking_time()));
652  *
653  */
654 static inline uint64_t time_to_nsec(struct timerel t)
655 {
656         uint64_t nsec;
657
658         nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
659         return nsec;
660 }
661
662 /**
663  * time_from_sec - convert seconds to a relative time
664  * @msec: time in seconds
665  *
666  * Example:
667  *      // 1 minute timeout
668  *      #define TIMEOUT time_from_sec(60)
669  */
670 static inline struct timerel time_from_sec(uint64_t sec)
671 {
672         struct timerel t;
673
674         t.ts.tv_nsec = 0;
675         t.ts.tv_sec = sec;
676         return TIMEREL_CHECK(t);
677 }
678
679 /**
680  * time_from_msec - convert milliseconds to a relative time
681  * @msec: time in milliseconds
682  *
683  * Example:
684  *      // 1/2 second timeout
685  *      #define TIMEOUT time_from_msec(500)
686  */
687 static inline struct timerel time_from_msec(uint64_t msec)
688 {
689         struct timerel t;
690
691         t.ts.tv_nsec = (msec % 1000) * 1000000;
692         t.ts.tv_sec = msec / 1000;
693         return TIMEREL_CHECK(t);
694 }
695
696 /**
697  * time_from_usec - convert microseconds to a relative time
698  * @usec: time in microseconds
699  *
700  * Example:
701  *      // 1/2 second timeout
702  *      #define TIMEOUT time_from_usec(500000)
703  */
704 static inline struct timerel time_from_usec(uint64_t usec)
705 {
706         struct timerel t;
707
708         t.ts.tv_nsec = (usec % 1000000) * 1000;
709         t.ts.tv_sec = usec / 1000000;
710         return TIMEREL_CHECK(t);
711 }
712
713 /**
714  * time_from_nsec - convert nanoseconds to a relative time
715  * @nsec: time in nanoseconds
716  *
717  * Example:
718  *      // 1/2 second timeout
719  *      #define TIMEOUT time_from_nsec(500000000)
720  */
721 static inline struct timerel time_from_nsec(uint64_t nsec)
722 {
723         struct timerel t;
724
725         t.ts.tv_nsec = nsec % 1000000000;
726         t.ts.tv_sec = nsec / 1000000000;
727         return TIMEREL_CHECK(t);
728 }
729
730 static inline struct timeval timespec_to_timeval(struct timespec ts)
731 {
732         struct timeval tv;
733         tv.tv_sec = ts.tv_sec;
734         tv.tv_usec = ts.tv_nsec / 1000;
735         return tv;
736 }
737
738 /**
739  * timerel_to_timeval - convert a relative time to a timeval.
740  * @t: a relative time.
741  *
742  * Example:
743  *      struct timerel t = { { 100, 0 } }; // 100 seconds
744  *      struct timeval tv;
745  *
746  *      tv = timerel_to_timeval(t);
747  *      printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
748  */
749 static inline struct timeval timerel_to_timeval(struct timerel t)
750 {
751         return timespec_to_timeval(t.ts);
752 }
753
754 /**
755  * timeabs_to_timeval - convert an absolute time to a timeval.
756  * @t: an absolute time.
757  *
758  * Example:
759  *      struct timeval tv;
760  *
761  *      tv = timeabs_to_timeval(time_now());
762  *      printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
763  */
764 static inline struct timeval timeabs_to_timeval(struct timeabs t)
765 {
766         return timespec_to_timeval(t.ts);
767 }
768
769 static inline struct timespec timeval_to_timespec(struct timeval tv)
770 {
771         struct timespec ts;
772         ts.tv_sec = tv.tv_sec;
773         ts.tv_nsec = tv.tv_usec * 1000;
774         return ts;
775 }
776
777 /**
778  * timeval_to_timerel - convert a timeval to a relative time.
779  * @tv: a timeval.
780  *
781  * Example:
782  *      struct timeval tv = { 0, 500 };
783  *      struct timerel t;
784  *
785  *      t = timeval_to_timerel(tv);
786  *      printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
787  */
788 static inline struct timerel timeval_to_timerel(struct timeval tv)
789 {
790         struct timerel t;
791         t.ts = timeval_to_timespec(tv);
792         return TIMEREL_CHECK(t);
793 }
794
795 /**
796  * timeval_to_timeabs - convert a timeval to an absolute time.
797  * @tv: a timeval.
798  *
799  * Example:
800  *      struct timeval tv = { 1401762008, 500 };
801  *      struct timeabs t;
802  *
803  *      t = timeval_to_timeabs(tv);
804  *      printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
805  */
806 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
807 {
808         struct timeabs t;
809         t.ts = timeval_to_timespec(tv);
810         return TIMEABS_CHECK(t);
811 }
812 #endif /* CCAN_TIME_H */