2 This file is part of GNUnet.
3 (C) 2001-2013 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 3, 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 microseconds.
44 struct GNUNET_TIME_Absolute
49 uint64_t abs_value_us;
53 * Time for relative time used by GNUnet, in microseconds.
54 * Always positive, so we can only refer to future time.
56 struct GNUNET_TIME_Relative
61 uint64_t rel_value_us;
64 GNUNET_NETWORK_STRUCT_BEGIN
67 * Time for relative time used by GNUnet, in microseconds and in network byte order.
69 struct GNUNET_TIME_RelativeNBO
72 * The actual value (in network byte order).
74 uint64_t rel_value_us__ GNUNET_PACKED;
79 * Time for absolute time used by GNUnet, in microseconds and in network byte order.
81 struct GNUNET_TIME_AbsoluteNBO
84 * The actual value (in network byte order).
86 uint64_t abs_value_us__ 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 microsecond, our basic time unit.
103 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
108 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
113 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
118 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
123 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
128 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
133 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
136 * One month (30 days).
138 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
141 * One year (365 days).
143 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
146 * Constant used to specify "forever". This constant
147 * will be treated specially in all time operations.
149 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
152 * Constant used to specify "forever". This constant
153 * will be treated specially in all time operations.
155 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
160 * Threshold after which exponential backoff should not increase (15 m).
162 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
166 * Perform our standard exponential back-off calculation, starting at 1 ms
167 * and then going by a factor of 2 up unto a maximum of 15 m.
169 * @param r current backoff time, initially zero
171 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
172 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
175 * Return relative time of 0ms.
177 struct GNUNET_TIME_Relative
178 GNUNET_TIME_relative_get_zero_ (void);
182 * Return absolute time of 0ms.
184 struct GNUNET_TIME_Absolute
185 GNUNET_TIME_absolute_get_zero_ (void);
189 * Return relative time of 1 microsecond.
191 struct GNUNET_TIME_Relative
192 GNUNET_TIME_relative_get_unit_ (void);
196 * Return relative time of 1ms.
198 struct GNUNET_TIME_Relative
199 GNUNET_TIME_relative_get_millisecond_ (void);
203 * Return relative time of 1s.
205 struct GNUNET_TIME_Relative
206 GNUNET_TIME_relative_get_second_ (void);
210 * Return relative time of 1 minute.
212 struct GNUNET_TIME_Relative
213 GNUNET_TIME_relative_get_minute_ (void);
217 * Return relative time of 1 hour.
219 struct GNUNET_TIME_Relative
220 GNUNET_TIME_relative_get_hour_ (void);
226 struct GNUNET_TIME_Relative
227 GNUNET_TIME_relative_get_forever_ (void);
233 struct GNUNET_TIME_Absolute
234 GNUNET_TIME_absolute_get_forever_ (void);
238 * Get the current time.
240 * @return the current time
242 struct GNUNET_TIME_Absolute
243 GNUNET_TIME_absolute_get (void);
247 * Convert relative time to an absolute time in the
250 * @param rel relative time to convert
251 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
253 struct GNUNET_TIME_Absolute
254 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
258 * Return the minimum of two relative time values.
260 * @param t1 first timestamp
261 * @param t2 other timestamp
262 * @return timestamp that is smaller
264 struct GNUNET_TIME_Relative
265 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
266 struct GNUNET_TIME_Relative t2);
271 * Return the maximum of two relative time values.
273 * @param t1 first timestamp
274 * @param t2 other timestamp
275 * @return timestamp that is larger
277 struct GNUNET_TIME_Relative
278 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
279 struct GNUNET_TIME_Relative t2);
283 * Return the minimum 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_min (struct GNUNET_TIME_Absolute t1,
291 struct GNUNET_TIME_Absolute t2);
295 * Return the maximum of two absolute time values.
297 * @param t1 first timestamp
298 * @param t2 other timestamp
299 * @return timestamp that is smaller
301 struct GNUNET_TIME_Absolute
302 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
303 struct GNUNET_TIME_Absolute t2);
307 * Given a timestamp in the future, how much time
308 * remains until then?
310 * @param future some absolute time, typically in the future
311 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
313 struct GNUNET_TIME_Relative
314 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
318 * Calculate the estimate time of arrival/completion
321 * @param start when did the operation start?
322 * @param finished how much has been done?
323 * @param total how much must be done overall (same unit as for "finished")
324 * @return remaining duration for the operation,
325 * assuming it continues at the same speed
327 struct GNUNET_TIME_Relative
328 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
333 * Compute the time difference between the given start and end times.
334 * Use this function instead of actual subtraction to ensure that
335 * "FOREVER" and overflows are handeled correctly.
337 * @param start some absolute time
338 * @param end some absolute time (typically larger or equal to start)
339 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
341 struct GNUNET_TIME_Relative
342 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
343 struct GNUNET_TIME_Absolute end);
347 * Get the duration of an operation as the
348 * difference of the current time and the given start time "hence".
350 * @param whence some absolute time, typically in the past
351 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
353 struct GNUNET_TIME_Relative
354 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
358 * Add a given relative duration to the
361 * @param start some absolute time
362 * @param duration some relative time to add
363 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
365 struct GNUNET_TIME_Absolute
366 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
367 struct GNUNET_TIME_Relative duration);
371 * Subtract a given relative duration from the
374 * @param start some absolute time
375 * @param duration some relative time to subtract
376 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
378 struct GNUNET_TIME_Absolute
379 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
380 struct GNUNET_TIME_Relative duration);
384 * Multiply relative time by a given factor.
386 * @param rel some duration
387 * @param factor integer to multiply with
388 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
390 struct GNUNET_TIME_Relative
391 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
392 unsigned int factor);
396 * Divide relative time by a given factor.
398 * @param rel some duration
399 * @param factor integer to divide by
400 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
402 struct GNUNET_TIME_Relative
403 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
404 unsigned int factor);
408 * Add relative times together.
410 * @param a1 some relative time
411 * @param a2 some other relative time
412 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
414 struct GNUNET_TIME_Relative
415 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
416 struct GNUNET_TIME_Relative a2);
420 * Subtract relative timestamp from the other.
422 * @param a1 first timestamp
423 * @param a2 second timestamp
424 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
426 struct GNUNET_TIME_Relative
427 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
428 struct GNUNET_TIME_Relative a2);
432 * Convert relative time to network byte order.
434 * @param a time to convert
435 * @return converted time value
437 struct GNUNET_TIME_RelativeNBO
438 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
442 * Convert relative time from network byte order.
444 * @param a time to convert
445 * @return converted time value
447 struct GNUNET_TIME_Relative
448 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
452 * Convert relative time to network byte order.
454 * @param a time to convert
455 * @return converted time value
457 struct GNUNET_TIME_AbsoluteNBO
458 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
462 * Convert relative time from network byte order.
464 * @param a time to convert
465 * @return converted time value
467 struct GNUNET_TIME_Absolute
468 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
472 * Set the timestamp offset for this instance.
474 * @param offset the offset to skew the locale time by
477 GNUNET_TIME_set_offset (long long offset);
481 * Get the timestamp offset for this instance.
483 * @return the offset we currently skew the locale time by
486 GNUNET_TIME_get_offset (void);
489 #if 0 /* keep Emacsens' auto-indent happy */
496 /* ifndef GNUNET_TIME_LIB_H */
498 /* end of gnunet_time_lib.h */