e4b9dd970640e81d532a54f77a2255b7f45e8151
[oweals/gnunet.git] / src / include / gnunet_time_lib.h
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 /**
78  * @brief constants to specify time
79  */
80 #define GNUNET_TIME_UNIT_ZERO     GNUNET_TIME_relative_get_zero()
81 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero()
82 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit()
83 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1000)
84 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
85 #define GNUNET_TIME_UNIT_HOURS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
86 #define GNUNET_TIME_UNIT_DAYS    GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS,   24)
87 #define GNUNET_TIME_UNIT_WEEKS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,     7)
88 #define GNUNET_TIME_UNIT_MONTHS  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,    30)
89 #define GNUNET_TIME_UNIT_YEARS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,   365)
90
91 /**
92  * Constant used to specify "forever".  This constant
93  * will be treated specially in all time operations.
94  */
95 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever ()
96
97 /**
98  * Constant used to specify "forever".  This constant
99  * will be treated specially in all time operations.
100  */
101 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever ()
102
103 /**
104  * Return relative time of 0ms.
105  */
106 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero (void);
107
108 /**
109  * Return absolute time of 0ms.
110  */
111 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero (void);
112
113 /**
114  * Return relative time of 1ms.
115  */
116 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit (void);
117
118 /**
119  * Return "forever".
120  */
121 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever (void);
122
123 /**
124  * Return "forever".
125  */
126 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever (void);
127
128 /**
129  * Get the current time.
130  *
131  * @return the current time
132  */
133 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get (void);
134
135 /**
136  * Convert relative time to an absolute time in the
137  * future.
138  *
139  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
140  */
141 struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute (struct
142                                                               GNUNET_TIME_Relative
143                                                               rel);
144
145 /**
146  * Return the minimum of two relative time values.
147  *
148  * @return timestamp that is smaller
149  */
150 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
151                                                       GNUNET_TIME_Relative
152                                                       t1,
153                                                       struct
154                                                       GNUNET_TIME_Relative t2);
155
156 /**
157  * Given a timestamp in the future, how much time
158  * remains until then?
159  *
160  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
161  */
162 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct
163                                                                 GNUNET_TIME_Absolute
164                                                                 future);
165
166
167 /**
168  * Calculate the estimate time of arrival/completion 
169  * for an operation.
170  *
171  * @param start when did the operation start?
172  * @param finished how much has been done?
173  * @param total how much must be done overall (same unit as for "finished")
174  * @return remaining duration for the operation,
175  *        assuming it continues at the same speed
176  */
177 struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
178                                                        uint64_t finished,
179                                                        uint64_t total);
180
181
182 /**
183  * Compute the time difference between the given start and end times.
184  * Use this function instead of actual subtraction to ensure that
185  * "FOREVER" and overflows are handeled correctly.
186  *
187  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
188  */
189 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct
190                                                                  GNUNET_TIME_Absolute
191                                                                  start,
192                                                                  struct
193                                                                  GNUNET_TIME_Absolute
194                                                                  end);
195
196 /**
197  * Get the duration of an operation as the
198  * difference of the current time and the given start time "hence".
199  *
200  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
201  */
202 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration (struct
203                                                                GNUNET_TIME_Absolute
204                                                                hence);
205
206
207 /**
208  * Add a given relative duration to the
209  * given start time.
210  *
211  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
212  */
213 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct
214                                                       GNUNET_TIME_Absolute
215                                                       start,
216                                                       struct
217                                                       GNUNET_TIME_Relative
218                                                       duration);
219
220 /**
221  * Multiply relative time by a given factor.
222  *
223  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
224  */
225 struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct
226                                                            GNUNET_TIME_Relative
227                                                            rel,
228                                                            unsigned int
229                                                            factor);
230
231 /**
232  * Add relative times together.
233  *
234  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
235  */
236 struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct
237                                                       GNUNET_TIME_Relative a1,
238                                                       struct
239                                                       GNUNET_TIME_Relative
240                                                       a2);
241
242
243 /**
244  * Convert relative time to network byte order.
245  */
246 struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton (struct
247                                                           GNUNET_TIME_Relative
248                                                           a);
249
250 /**
251  * Convert relative time from network byte order.
252  */
253 struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh (struct
254                                                        GNUNET_TIME_RelativeNBO
255                                                        a);
256
257 /**
258  * Convert relative time to network byte order.
259  */
260 struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton (struct
261                                                           GNUNET_TIME_Absolute
262                                                           a);
263
264 /**
265  * Convert relative time from network byte order.
266  */
267 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh (struct
268                                                        GNUNET_TIME_AbsoluteNBO
269                                                        a);
270
271 #if 0                           /* keep Emacsens' auto-indent happy */
272 {
273 #endif
274 #ifdef __cplusplus
275 }
276 #endif
277
278 /* ifndef GNUNET_TIME_LIB_H */
279 #endif
280 /* end of gnunet_time_lib.h */