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
64 GNUNET_NETWORK_STRUCT_BEGIN
67 * Time for relative time used by GNUnet, in milliseconds and in network byte order.
69 struct GNUNET_TIME_RelativeNBO
72 * The actual value (in network byte order).
74 uint64_t rel_value__ GNUNET_PACKED;
79 * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
81 struct GNUNET_TIME_AbsoluteNBO
84 * The actual value (in network byte order).
86 uint64_t abs_value__ GNUNET_PACKED;
88 GNUNET_NETWORK_STRUCT_END
93 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero_()
98 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
101 * One millisecond, our basic time unit.
103 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit_()
108 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
113 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
118 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
123 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
128 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
131 * One month (30 days).
133 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
136 * One year (365 days).
138 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
141 * Constant used to specify "forever". This constant
142 * will be treated specially in all time operations.
144 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
147 * Constant used to specify "forever". This constant
148 * will be treated specially in all time operations.
150 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
155 * Threshold after which exponential backoff should not increase (15 m).
157 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
161 * Perform our standard exponential back-off calculation, starting at 1mst
162 * and then going by a factor of 2 up unto a maximum of 1s.
164 * @param r current backoff time, initially zero
166 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
167 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
170 * Return relative time of 0ms.
172 struct GNUNET_TIME_Relative
173 GNUNET_TIME_relative_get_zero_ (void);
177 * Return absolute time of 0ms.
179 struct GNUNET_TIME_Absolute
180 GNUNET_TIME_absolute_get_zero_ (void);
184 * Return relative time of 1ms.
186 struct GNUNET_TIME_Relative
187 GNUNET_TIME_relative_get_unit_ (void);
191 * Return relative time of 1s.
193 struct GNUNET_TIME_Relative
194 GNUNET_TIME_relative_get_second_ (void);
198 * Return relative time of 1 minute.
200 struct GNUNET_TIME_Relative
201 GNUNET_TIME_relative_get_minute_ (void);
205 * Return relative time of 1 hour.
207 struct GNUNET_TIME_Relative
208 GNUNET_TIME_relative_get_hour_ (void);
214 struct GNUNET_TIME_Relative
215 GNUNET_TIME_relative_get_forever_ (void);
221 struct GNUNET_TIME_Absolute
222 GNUNET_TIME_absolute_get_forever_ (void);
226 * Get the current time.
228 * @return the current time
230 struct GNUNET_TIME_Absolute
231 GNUNET_TIME_absolute_get (void);
235 * Convert relative time to an absolute time in the
238 * @param rel relative time to convert
239 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
241 struct GNUNET_TIME_Absolute
242 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
246 * Return the minimum of two relative time values.
248 * @param t1 first timestamp
249 * @param t2 other timestamp
250 * @return timestamp that is smaller
252 struct GNUNET_TIME_Relative
253 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
254 struct GNUNET_TIME_Relative t2);
259 * Return the maximum of two relative time values.
261 * @param t1 first timestamp
262 * @param t2 other timestamp
263 * @return timestamp that is larger
265 struct GNUNET_TIME_Relative
266 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
267 struct GNUNET_TIME_Relative t2);
271 * Return the minimum of two absolute time values.
273 * @param t1 first timestamp
274 * @param t2 other timestamp
275 * @return timestamp that is smaller
277 struct GNUNET_TIME_Absolute
278 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
279 struct GNUNET_TIME_Absolute t2);
283 * Return the maximum of two absolute time values.
285 * @param t1 first timestamp
286 * @param t2 other timestamp
287 * @return timestamp that is smaller
289 struct GNUNET_TIME_Absolute
290 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
291 struct GNUNET_TIME_Absolute t2);
295 * Given a timestamp in the future, how much time
296 * remains until then?
298 * @param future some absolute time, typically in the future
299 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
301 struct GNUNET_TIME_Relative
302 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
306 * Calculate the estimate time of arrival/completion
309 * @param start when did the operation start?
310 * @param finished how much has been done?
311 * @param total how much must be done overall (same unit as for "finished")
312 * @return remaining duration for the operation,
313 * assuming it continues at the same speed
315 struct GNUNET_TIME_Relative
316 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
321 * Compute the time difference between the given start and end times.
322 * Use this function instead of actual subtraction to ensure that
323 * "FOREVER" and overflows are handeled correctly.
325 * @param start some absolute time
326 * @param end some absolute time (typically larger or equal to start)
327 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
329 struct GNUNET_TIME_Relative
330 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
331 struct GNUNET_TIME_Absolute end);
335 * Get the duration of an operation as the
336 * difference of the current time and the given start time "hence".
338 * @param whence some absolute time, typically in the past
339 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
341 struct GNUNET_TIME_Relative
342 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
346 * Add a given relative duration to the
349 * @param start some absolute time
350 * @param duration some relative time to add
351 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
353 struct GNUNET_TIME_Absolute
354 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
355 struct GNUNET_TIME_Relative duration);
359 * Subtract a given relative duration from the
362 * @param start some absolute time
363 * @param duration some relative time to subtract
364 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
366 struct GNUNET_TIME_Absolute
367 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
368 struct GNUNET_TIME_Relative duration);
372 * Multiply relative time by a given factor.
374 * @param rel some duration
375 * @param factor integer to multiply with
376 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
378 struct GNUNET_TIME_Relative
379 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
380 unsigned int factor);
384 * Divide relative time by a given factor.
386 * @param rel some duration
387 * @param factor integer to divide by
388 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
390 struct GNUNET_TIME_Relative
391 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
392 unsigned int factor);
396 * Add relative times together.
398 * @param a1 some relative time
399 * @param a2 some other relative time
400 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
402 struct GNUNET_TIME_Relative
403 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
404 struct GNUNET_TIME_Relative a2);
408 * Subtract relative timestamp from the other.
410 * @param a1 first timestamp
411 * @param a2 second timestamp
412 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
414 struct GNUNET_TIME_Relative
415 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
416 struct GNUNET_TIME_Relative a2);
420 * Convert relative time to network byte order.
422 * @param a time to convert
423 * @return converted time value
425 struct GNUNET_TIME_RelativeNBO
426 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
430 * Convert relative time from network byte order.
432 * @param a time to convert
433 * @return converted time value
435 struct GNUNET_TIME_Relative
436 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
440 * Convert relative time to network byte order.
442 * @param a time to convert
443 * @return converted time value
445 struct GNUNET_TIME_AbsoluteNBO
446 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
450 * Convert relative time from network byte order.
452 * @param a time to convert
453 * @return converted time value
455 struct GNUNET_TIME_Absolute
456 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
460 * Set the timestamp offset for this instance.
462 * @param offset the offset to skew the locale time by
465 GNUNET_TIME_set_offset (long long offset);
468 #if 0 /* keep Emacsens' auto-indent happy */
475 /* ifndef GNUNET_TIME_LIB_H */
477 /* end of gnunet_time_lib.h */