diff options
Diffstat (limited to 'src/include/gnunet_time_lib.h')
-rw-r--r-- | src/include/gnunet_time_lib.h | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/src/include/gnunet_time_lib.h b/src/include/gnunet_time_lib.h new file mode 100644 index 000000000..4ce1e3f1e --- /dev/null +++ b/src/include/gnunet_time_lib.h | |||
@@ -0,0 +1,246 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file include/gnunet_time_lib.h | ||
23 | * @brief functions related to time | ||
24 | * | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #ifndef GNUNET_TIME_LIB_H | ||
29 | #define GNUNET_TIME_LIB_H | ||
30 | |||
31 | #ifdef __cplusplus | ||
32 | extern "C" | ||
33 | { | ||
34 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
35 | } | ||
36 | #endif | ||
37 | #endif | ||
38 | |||
39 | #include "gnunet_common.h" | ||
40 | |||
41 | /** | ||
42 | * Time for absolute times used by GNUnet, in milliseconds. | ||
43 | */ | ||
44 | struct GNUNET_TIME_Absolute | ||
45 | { | ||
46 | uint64_t value; | ||
47 | }; | ||
48 | |||
49 | /** | ||
50 | * Time for relative time used by GNUnet, in milliseconds. | ||
51 | * Always positive, so we can only refer to future time. | ||
52 | */ | ||
53 | struct GNUNET_TIME_Relative | ||
54 | { | ||
55 | uint64_t value; | ||
56 | }; | ||
57 | |||
58 | |||
59 | /** | ||
60 | * Time for relative time used by GNUnet, in milliseconds and in network byte order. | ||
61 | */ | ||
62 | struct GNUNET_TIME_RelativeNBO | ||
63 | { | ||
64 | uint64_t value GNUNET_PACKED; | ||
65 | }; | ||
66 | |||
67 | |||
68 | /** | ||
69 | * Time for absolute time used by GNUnet, in milliseconds and in network byte order. | ||
70 | */ | ||
71 | struct GNUNET_TIME_AbsoluteNBO | ||
72 | { | ||
73 | uint64_t value GNUNET_PACKED; | ||
74 | }; | ||
75 | |||
76 | /** | ||
77 | * @brief constants to specify time | ||
78 | */ | ||
79 | #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero() | ||
80 | #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit() | ||
81 | #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1000) | ||
82 | #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60) | ||
83 | #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60) | ||
84 | #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24) | ||
85 | #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7) | ||
86 | #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30) | ||
87 | #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365) | ||
88 | |||
89 | /** | ||
90 | * Constant used to specify "forever". This constant | ||
91 | * will be treated specially in all time operations. | ||
92 | */ | ||
93 | #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever () | ||
94 | |||
95 | /** | ||
96 | * Constant used to specify "forever". This constant | ||
97 | * will be treated specially in all time operations. | ||
98 | */ | ||
99 | #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever () | ||
100 | |||
101 | /** | ||
102 | * Return relative time of 0ms. | ||
103 | */ | ||
104 | struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero (void); | ||
105 | |||
106 | /** | ||
107 | * Return relative time of 1ms. | ||
108 | */ | ||
109 | struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit (void); | ||
110 | |||
111 | /** | ||
112 | * Return "forever". | ||
113 | */ | ||
114 | struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever (void); | ||
115 | |||
116 | /** | ||
117 | * Return "forever". | ||
118 | */ | ||
119 | struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever (void); | ||
120 | |||
121 | /** | ||
122 | * Get the current time. | ||
123 | * | ||
124 | * @return the current time | ||
125 | */ | ||
126 | struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get (void); | ||
127 | |||
128 | /** | ||
129 | * Convert relative time to an absolute time in the | ||
130 | * future. | ||
131 | * | ||
132 | * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow) | ||
133 | */ | ||
134 | struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute (struct | ||
135 | GNUNET_TIME_Relative | ||
136 | rel); | ||
137 | |||
138 | /** | ||
139 | * Given a timestamp in the future, how much time | ||
140 | * remains until then? | ||
141 | * | ||
142 | * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER. | ||
143 | */ | ||
144 | struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct | ||
145 | GNUNET_TIME_Absolute | ||
146 | future); | ||
147 | |||
148 | /** | ||
149 | * Compute the time difference between the given start and end times. | ||
150 | * Use this function instead of actual subtraction to ensure that | ||
151 | * "FOREVER" and overflows are handeled correctly. | ||
152 | * | ||
153 | * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start | ||
154 | */ | ||
155 | struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct | ||
156 | GNUNET_TIME_Absolute | ||
157 | start, | ||
158 | struct | ||
159 | GNUNET_TIME_Absolute | ||
160 | end); | ||
161 | |||
162 | /** | ||
163 | * Get the duration of an operation as the | ||
164 | * difference of the current time and the given start time "hence". | ||
165 | * | ||
166 | * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence. | ||
167 | */ | ||
168 | struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration (struct | ||
169 | GNUNET_TIME_Absolute | ||
170 | hence); | ||
171 | |||
172 | |||
173 | /** | ||
174 | * Add a given relative duration to the | ||
175 | * given start time. | ||
176 | * | ||
177 | * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise | ||
178 | */ | ||
179 | struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct | ||
180 | GNUNET_TIME_Absolute | ||
181 | start, | ||
182 | struct | ||
183 | GNUNET_TIME_Relative | ||
184 | duration); | ||
185 | |||
186 | /** | ||
187 | * Multiply relative time by a given factor. | ||
188 | * | ||
189 | * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor | ||
190 | */ | ||
191 | struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct | ||
192 | GNUNET_TIME_Relative | ||
193 | rel, | ||
194 | unsigned int | ||
195 | factor); | ||
196 | |||
197 | /** | ||
198 | * Add relative times together. | ||
199 | * | ||
200 | * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise | ||
201 | */ | ||
202 | struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct | ||
203 | GNUNET_TIME_Relative a1, | ||
204 | struct | ||
205 | GNUNET_TIME_Relative | ||
206 | a2); | ||
207 | |||
208 | |||
209 | /** | ||
210 | * Convert relative time to network byte order. | ||
211 | */ | ||
212 | struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton (struct | ||
213 | GNUNET_TIME_Relative | ||
214 | a); | ||
215 | |||
216 | /** | ||
217 | * Convert relative time from network byte order. | ||
218 | */ | ||
219 | struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh (struct | ||
220 | GNUNET_TIME_RelativeNBO | ||
221 | a); | ||
222 | |||
223 | /** | ||
224 | * Convert relative time to network byte order. | ||
225 | */ | ||
226 | struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton (struct | ||
227 | GNUNET_TIME_Absolute | ||
228 | a); | ||
229 | |||
230 | /** | ||
231 | * Convert relative time from network byte order. | ||
232 | */ | ||
233 | struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh (struct | ||
234 | GNUNET_TIME_AbsoluteNBO | ||
235 | a); | ||
236 | |||
237 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
238 | { | ||
239 | #endif | ||
240 | #ifdef __cplusplus | ||
241 | } | ||
242 | #endif | ||
243 | |||
244 | /* ifndef GNUNET_TIME_LIB_H */ | ||
245 | #endif | ||
246 | /* end of gnunet_time_lib.h */ | ||