2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_time_lib.h
23 * @brief functions related to time
25 * @author Christian Grothoff
28 #ifndef GNUNET_TIME_LIB_H
29 #define GNUNET_TIME_LIB_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_common.h"
42 * Time for absolute times used by GNUnet, in milliseconds.
44 struct GNUNET_TIME_Absolute
53 * Time for relative time used by GNUnet, in milliseconds.
54 * Always positive, so we can only refer to future time.
56 struct GNUNET_TIME_Relative
66 * Time for relative time used by GNUnet, in milliseconds and in network byte order.
68 struct GNUNET_TIME_RelativeNBO
71 * The actual value (in network byte order).
73 uint64_t value__ GNUNET_PACKED;
78 * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
80 struct GNUNET_TIME_AbsoluteNBO
83 * The actual value (in network byte order).
85 uint64_t value__ GNUNET_PACKED;
92 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero()
97 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero()
100 * One millisecond, our basic time unit.
102 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit()
107 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1000)
112 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
117 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
122 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
127 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
130 * One month (30 days).
132 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
135 * One year (365 days).
137 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
140 * Constant used to specify "forever". This constant
141 * will be treated specially in all time operations.
143 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever ()
146 * Constant used to specify "forever". This constant
147 * will be treated specially in all time operations.
149 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever ()
152 * Return relative time of 0ms.
154 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero (void);
157 * Return absolute time of 0ms.
159 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero (void);
162 * Return relative time of 1ms.
164 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit (void);
169 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever (void);
174 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever (void);
177 * Get the current time.
179 * @return the current time
181 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get (void);
184 * Convert relative time to an absolute time in the
187 * @param rel relative time to convert
188 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
190 struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute (struct
195 * Return the minimum of two relative time values.
197 * @param t1 first timestamp
198 * @param t2 other timestamp
199 * @return timestamp that is smaller
201 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
205 GNUNET_TIME_Relative t2);
208 * Return the minimum of two absolute time values.
210 * @param t1 first timestamp
211 * @param t2 other timestamp
212 * @return timestamp that is smaller
214 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_min (struct
218 GNUNET_TIME_Absolute t2);
221 * Return the maximum of two absolute time values.
223 * @param t1 first timestamp
224 * @param t2 other timestamp
225 * @return timestamp that is smaller
227 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_max (struct
231 GNUNET_TIME_Absolute t2);
234 * Given a timestamp in the future, how much time
235 * remains until then?
237 * @param future some absolute time, typically in the future
238 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
240 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct
246 * Calculate the estimate time of arrival/completion
249 * @param start when did the operation start?
250 * @param finished how much has been done?
251 * @param total how much must be done overall (same unit as for "finished")
252 * @return remaining duration for the operation,
253 * assuming it continues at the same speed
255 struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
261 * Compute the time difference between the given start and end times.
262 * Use this function instead of actual subtraction to ensure that
263 * "FOREVER" and overflows are handeled correctly.
265 * @param start some absolute time
266 * @param end some absolute time (typically larger or equal to start)
267 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
269 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct
277 * Get the duration of an operation as the
278 * difference of the current time and the given start time "hence".
280 * @param hence some absolute time, typically in the past
281 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
283 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration (struct
289 * Add a given relative duration to the
292 * @param start some absolute time
293 * @param duration some relative time to add
294 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
296 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct
304 * Multiply relative time by a given factor.
306 * @param rel some duration
307 * @param factor integer to multiply with
308 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
310 struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct
317 * Add relative times together.
319 * @param a1 some relative time
320 * @param a2 some other relative time
321 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
323 struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct
324 GNUNET_TIME_Relative a1,
331 * Convert relative time to network byte order.
333 * @param a time to convert
334 * @return converted time value
336 struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton (struct
341 * Convert relative time from network byte order.
343 * @param a time to convert
344 * @return converted time value
346 struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh (struct
347 GNUNET_TIME_RelativeNBO
351 * Convert relative time to network byte order.
353 * @param a time to convert
354 * @return converted time value
356 struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton (struct
361 * Convert relative time from network byte order.
363 * @param a time to convert
364 * @return converted time value
366 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh (struct
367 GNUNET_TIME_AbsoluteNBO
370 #if 0 /* keep Emacsens' auto-indent happy */
377 /* ifndef GNUNET_TIME_LIB_H */
379 /* end of gnunet_time_lib.h */