Add samba3.samba3badnameblob test to check regressions in bug #9215.
[Samba/gbeck.git] / lib / ccan / time / time.h
blobfb2ee459d694a99c40f31efcbacc1530c549eeed
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 #include <stdint.h>
7 #include <stdbool.h>
9 /**
10 * time_now - return the current time
12 * Example:
13 * printf("Now is %lu seconds since epoch\n", (long)time_now().tv_sec);
15 struct timeval time_now(void);
17 /**
18 * time_greater - is a after b?
19 * @a: one time.
20 * @b: another time.
22 * Example:
23 * static bool timed_out(const struct timeval *start)
24 * {
25 * #define TIMEOUT time_from_msec(1000)
26 * return time_greater(time_now(), time_add(*start, TIMEOUT));
27 * }
29 bool time_greater(struct timeval a, struct timeval b);
31 /**
32 * time_less - is a before b?
33 * @a: one time.
34 * @b: another time.
36 * Example:
37 * static bool still_valid(const struct timeval *start)
38 * {
39 * #define TIMEOUT time_from_msec(1000)
40 * return time_less(time_now(), time_add(*start, TIMEOUT));
41 * }
43 bool time_less(struct timeval a, struct timeval b);
45 /**
46 * time_eq - is a equal to b?
47 * @a: one time.
48 * @b: another time.
50 * Example:
51 * #include <sys/types.h>
52 * #include <sys/wait.h>
54 * // Can we fork in under a microsecond?
55 * static bool fast_fork(void)
56 * {
57 * struct timeval start = time_now();
58 * if (fork() != 0) {
59 * exit(0);
60 * }
61 * wait(NULL);
62 * return time_eq(start, time_now());
63 * }
65 bool time_eq(struct timeval a, struct timeval b);
67 /**
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.
74 * Example:
75 * static bool was_recent(const struct timeval *start)
76 * {
77 * return time_sub(time_now(), *start).tv_sec < 1;
78 * }
80 struct timeval time_sub(struct timeval recent, struct timeval old);
82 /**
83 * time_add - add two times
84 * @a: one time.
85 * @b: another time.
87 * The times must not overflow, or the results are undefined.
89 * Example:
90 * // We do one every second.
91 * static struct timeval next_time(void)
92 * {
93 * return time_add(time_now(), time_from_msec(1000));
94 * }
96 struct timeval time_add(struct timeval a, struct timeval b);
98 /**
99 * time_divide - divide a time by a value.
100 * @t: a time.
101 * @div: number to divide it by.
103 * Example:
104 * // How long does it take to do a fork?
105 * static struct timeval forking_time(void)
107 * struct timeval start = time_now();
108 * unsigned int i;
110 * for (i = 0; i < 1000; i++) {
111 * if (fork() != 0) {
112 * exit(0);
114 * wait(NULL);
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.
123 * @t: a time.
124 * @mult: number to multiply it by.
126 * Example:
127 * ...
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
135 * @t: a time
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.
141 * Example:
142 * ...
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
150 * @t: a time
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.
156 * Example:
157 * ...
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
168 * Example:
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
178 * Example:
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 */