]> git.ozlabs.org Git - ccan/blobdiff - ccan/time/time.h
time: use timespec instead of timeval.
[ccan] / ccan / time / time.h
index e3a579115c56c9c680ca873fbe0d61e2b715d53a..6fb622c6224b86d5daeeddcf73fb721398598a99 100644 (file)
@@ -1,7 +1,16 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
 #ifndef CCAN_TIME_H
 #define CCAN_TIME_H
 #include "config.h"
 #include <sys/time.h>
+#if HAVE_STRUCT_TIMESPEC
+#include <time.h>
+#else
+struct timespec {
+       time_t   tv_sec;        /* seconds */
+       long     tv_nsec;       /* nanoseconds */
+};
+#endif
 #include <stdint.h>
 #include <stdbool.h>
 
@@ -11,7 +20,7 @@
  * Example:
  *     printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
  */
-struct timeval time_now(void);
+struct timespec time_now(void);
 
 /**
  * time_greater - is a after b?
@@ -19,13 +28,13 @@ struct timeval time_now(void);
  * @b: another time.
  *
  * Example:
- *     static bool timed_out(const struct timeval *start)
+ *     static bool timed_out(const struct timespec *start)
  *     {
  *     #define TIMEOUT time_from_msec(1000)
  *             return time_greater(time_now(), time_add(*start, TIMEOUT));
  *     }
  */
-bool time_greater(struct timeval a, struct timeval b);
+bool time_greater(struct timespec a, struct timespec b);
 
 /**
  * time_less - is a before b?
@@ -33,13 +42,13 @@ bool time_greater(struct timeval a, struct timeval b);
  * @b: another time.
  *
  * Example:
- *     static bool still_valid(const struct timeval *start)
+ *     static bool still_valid(const struct timespec *start)
  *     {
  *     #define TIMEOUT time_from_msec(1000)
  *             return time_less(time_now(), time_add(*start, TIMEOUT));
  *     }
  */
-bool time_less(struct timeval a, struct timeval b);
+bool time_less(struct timespec a, struct timespec b);
 
 /**
  * time_eq - is a equal to b?
@@ -53,7 +62,7 @@ bool time_less(struct timeval a, struct timeval b);
  *     // Can we fork in under a microsecond?
  *     static bool fast_fork(void)
  *     {
- *             struct timeval start = time_now();
+ *             struct timespec start = time_now();
  *             if (fork() != 0) {
  *                     exit(0);
  *             }
@@ -61,22 +70,22 @@ bool time_less(struct timeval a, struct timeval b);
  *             return time_eq(start, time_now());
  *     }
  */
-bool time_eq(struct timeval a, struct timeval b);
+bool time_eq(struct timespec a, struct timespec b);
 
 /**
  * time_sub - subtract two times
  * @recent: the larger (more recent) time.
  * @old: the smaller (less recent) time.
  *
- * This returns a well formed struct timeval.
+ * This returns a well formed struct timespec.
  *
  * Example:
- *     static bool was_recent(const struct timeval *start)
+ *     static bool was_recent(const struct timespec *start)
  *     {
  *             return time_sub(time_now(), *start).tv_sec < 1;
  *     }
  */
-struct timeval time_sub(struct timeval recent, struct timeval old);
+struct timespec time_sub(struct timespec recent, struct timespec old);
 
 /**
  * time_add - add two times
@@ -87,12 +96,12 @@ struct timeval time_sub(struct timeval recent, struct timeval old);
  *
  * Example:
  *     // We do one every second.
- *     static struct timeval next_time(void)
+ *     static struct timespec next_time(void)
  *     {
  *             return time_add(time_now(), time_from_msec(1000));
  *     }
  */
-struct timeval time_add(struct timeval a, struct timeval b);
+struct timespec time_add(struct timespec a, struct timespec b);
 
 /**
  * time_divide - divide a time by a value.
@@ -101,9 +110,9 @@ struct timeval time_add(struct timeval a, struct timeval b);
  *
  * Example:
  *     // How long does it take to do a fork?
- *     static struct timeval forking_time(void)
+ *     static struct timespec forking_time(void)
  *     {
- *             struct timeval start = time_now();
+ *             struct timespec start = time_now();
  *             unsigned int i;
  *
  *             for (i = 0; i < 1000; i++) {
@@ -115,7 +124,7 @@ struct timeval time_add(struct timeval a, struct timeval b);
  *             return time_divide(time_sub(time_now(), start), i);
  *     }
  */
-struct timeval time_divide(struct timeval t, unsigned long div);
+struct timespec time_divide(struct timespec t, unsigned long div);
 
 /**
  * time_multiply - multiply a time by a value.
@@ -127,7 +136,7 @@ struct timeval time_divide(struct timeval t, unsigned long div);
  *     printf("Time to do 100000 forks would be %u sec\n",
  *            (unsigned)time_multiply(forking_time(), 1000000).tv_sec);
  */
-struct timeval time_multiply(struct timeval t, unsigned long mult);
+struct timespec time_multiply(struct timespec t, unsigned long mult);
 
 /**
  * time_to_msec - return number of milliseconds
@@ -142,7 +151,7 @@ struct timeval time_multiply(struct timeval t, unsigned long mult);
  *     printf("Forking time is %u msec\n",
  *            (unsigned)time_to_msec(forking_time()));
  */
-uint64_t time_to_msec(struct timeval t);
+uint64_t time_to_msec(struct timespec t);
 
 /**
  * time_to_usec - return number of microseconds
@@ -158,26 +167,86 @@ uint64_t time_to_msec(struct timeval t);
  *            (unsigned)time_to_usec(forking_time()));
  *
  */
-uint64_t time_to_usec(struct timeval t);
+uint64_t time_to_usec(struct timespec t);
 
 /**
- * time_from_msec - convert milliseconds to a timeval
+ * time_to_nsec - return number of nanoseconds
+ * @t: a time
+ *
+ * It's sometimes more convenient to deal with time values as
+ * nanoseconds.  Note that this will fit into a 32-bit variable if
+ * it's a time difference of less than ~4 seconds.
+ *
+ * Example:
+ *     ...
+ *     printf("Forking time is %u nsec\n",
+ *            (unsigned)time_to_nsec(forking_time()));
+ *
+ */
+uint64_t time_to_nsec(struct timespec t);
+
+/**
+ * time_from_msec - convert milliseconds to a timespec
  * @msec: time in milliseconds
  *
  * Example:
  *     // 1/2 second timeout
  *     #define TIMEOUT time_from_msec(500)
  */
-struct timeval time_from_msec(uint64_t msec);
+struct timespec time_from_msec(uint64_t msec);
 
 /**
- * time_from_usec - convert microseconds to a timeval
+ * time_from_usec - convert microseconds to a timespec
  * @usec: time in microseconds
  *
  * Example:
  *     // 1/2 second timeout
  *     #define TIMEOUT time_from_usec(500000)
  */
-struct timeval time_from_usec(uint64_t usec);
+struct timespec time_from_usec(uint64_t usec);
+
+/**
+ * time_from_nsec - convert nanoseconds to a timespec
+ * @nsec: time in nanoseconds
+ *
+ * Example:
+ *     // 1/2 second timeout
+ *     #define TIMEOUT time_from_nsec(500000000)
+ */
+struct timespec time_from_nsec(uint64_t nsec);
 
+/**
+ * timespec_to_timeval - convert a timespec to a timeval.
+ * @ts: a timespec.
+ *
+ * Example:
+ *     struct timeval tv;
+ *
+ *     tv = timespec_to_timeval(time_now());
+ */
+static inline struct timeval timespec_to_timeval(struct timespec ts)
+{
+       struct timeval tv;
+       tv.tv_sec = ts.tv_sec;
+       tv.tv_usec = ts.tv_nsec / 1000;
+       return tv;
+}
+
+/**
+ * timeval_to_timespec - convert a timeval to a timespec.
+ * @tv: a timeval.
+ *
+ * Example:
+ *     struct timeval tv = { 0, 500 };
+ *     struct timespec ts;
+ *
+ *     ts = timeval_to_timespec(tv);
+ */
+static inline struct timespec timeval_to_timespec(struct timeval tv)
+{
+       struct timespec ts;
+       ts.tv_sec = tv.tv_sec;
+       ts.tv_nsec = tv.tv_usec * 1000;
+       return ts;
+}
 #endif /* CCAN_TIME_H */