2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
25 * Functions related to time
27 * @defgroup time Time library
28 * Time and time calculations.
32 #ifndef GNUNET_TIME_LIB_H
33 #define GNUNET_TIME_LIB_H
38 #if 0 /* keep Emacsens' auto-indent happy */
43 #include "gnunet_common.h"
46 * Time for absolute times used by GNUnet, in microseconds.
48 struct GNUNET_TIME_Absolute
53 uint64_t abs_value_us;
57 * Time for relative time used by GNUnet, in microseconds.
58 * Always positive, so we can only refer to future time.
60 struct GNUNET_TIME_Relative
65 uint64_t rel_value_us;
68 GNUNET_NETWORK_STRUCT_BEGIN
71 * Time for relative time used by GNUnet, in microseconds and in network byte order.
73 struct GNUNET_TIME_RelativeNBO
76 * The actual value (in network byte order).
78 uint64_t rel_value_us__ GNUNET_PACKED;
83 * Time for absolute time used by GNUnet, in microseconds and in network byte order.
85 struct GNUNET_TIME_AbsoluteNBO
88 * The actual value (in network byte order).
90 uint64_t abs_value_us__ GNUNET_PACKED;
92 GNUNET_NETWORK_STRUCT_END
97 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero_()
100 * Absolute time zero.
102 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
105 * One microsecond, our basic time unit.
107 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
112 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
117 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
122 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
127 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
132 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
137 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
140 * One month (30 days).
142 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
145 * One year (365 days).
147 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
150 * Constant used to specify "forever". This constant
151 * will be treated specially in all time operations.
153 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
156 * Constant used to specify "forever". This constant
157 * will be treated specially in all time operations.
159 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
164 * Threshold after which exponential backoff should not increase (15 m).
166 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
170 * Perform our standard exponential back-off calculation, starting at 1 ms
171 * and then going by a factor of 2 up unto a maximum of 15 m.
173 * @param r current backoff time, initially zero
175 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
176 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
179 * Return relative time of 0ms.
181 struct GNUNET_TIME_Relative
182 GNUNET_TIME_relative_get_zero_ (void);
186 * Return absolute time of 0ms.
188 struct GNUNET_TIME_Absolute
189 GNUNET_TIME_absolute_get_zero_ (void);
193 * Return relative time of 1 microsecond.
195 struct GNUNET_TIME_Relative
196 GNUNET_TIME_relative_get_unit_ (void);
200 * Return relative time of 1ms.
202 struct GNUNET_TIME_Relative
203 GNUNET_TIME_relative_get_millisecond_ (void);
207 * Return relative time of 1s.
209 struct GNUNET_TIME_Relative
210 GNUNET_TIME_relative_get_second_ (void);
214 * Return relative time of 1 minute.
216 struct GNUNET_TIME_Relative
217 GNUNET_TIME_relative_get_minute_ (void);
221 * Return relative time of 1 hour.
223 struct GNUNET_TIME_Relative
224 GNUNET_TIME_relative_get_hour_ (void);
230 struct GNUNET_TIME_Relative
231 GNUNET_TIME_relative_get_forever_ (void);
237 struct GNUNET_TIME_Absolute
238 GNUNET_TIME_absolute_get_forever_ (void);
242 * Get the current time.
244 * @return the current time
246 struct GNUNET_TIME_Absolute
247 GNUNET_TIME_absolute_get (void);
251 * Convert relative time to an absolute time in the
254 * @param rel relative time to convert
255 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
257 struct GNUNET_TIME_Absolute
258 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
262 * Round a time value so that it is suitable for transmission
263 * via JSON encodings.
265 * @param at time to round
266 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
267 * it was just now rounded
270 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at);
274 * Round a time value so that it is suitable for transmission
275 * via JSON encodings.
277 * @param rt time to round
278 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
279 * it was just now rounded
282 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
286 * Return the minimum of two relative time values.
288 * @param t1 first timestamp
289 * @param t2 other timestamp
290 * @return timestamp that is smaller
292 struct GNUNET_TIME_Relative
293 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
294 struct GNUNET_TIME_Relative t2);
299 * Return the maximum of two relative time values.
301 * @param t1 first timestamp
302 * @param t2 other timestamp
303 * @return timestamp that is larger
305 struct GNUNET_TIME_Relative
306 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
307 struct GNUNET_TIME_Relative t2);
311 * Return the minimum of two absolute time values.
313 * @param t1 first timestamp
314 * @param t2 other timestamp
315 * @return timestamp that is smaller
317 struct GNUNET_TIME_Absolute
318 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
319 struct GNUNET_TIME_Absolute t2);
323 * Return the maximum of two absolute time values.
325 * @param t1 first timestamp
326 * @param t2 other timestamp
327 * @return timestamp that is smaller
329 struct GNUNET_TIME_Absolute
330 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
331 struct GNUNET_TIME_Absolute t2);
335 * Given a timestamp in the future, how much time
336 * remains until then?
338 * @param future some absolute time, typically in the future
339 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
341 struct GNUNET_TIME_Relative
342 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
346 * Calculate the estimate time of arrival/completion
349 * @param start when did the operation start?
350 * @param finished how much has been done?
351 * @param total how much must be done overall (same unit as for "finished")
352 * @return remaining duration for the operation,
353 * assuming it continues at the same speed
355 struct GNUNET_TIME_Relative
356 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
361 * Compute the time difference between the given start and end times.
362 * Use this function instead of actual subtraction to ensure that
363 * "FOREVER" and overflows are handeled correctly.
365 * @param start some absolute time
366 * @param end some absolute time (typically larger or equal to start)
367 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
369 struct GNUNET_TIME_Relative
370 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
371 struct GNUNET_TIME_Absolute end);
375 * Get the duration of an operation as the
376 * difference of the current time and the given start time "hence".
378 * @param whence some absolute time, typically in the past
379 * @return 0 if hence > now, otherwise now-hence.
381 struct GNUNET_TIME_Relative
382 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
386 * Add a given relative duration to the
389 * @param start some absolute time
390 * @param duration some relative time to add
391 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
393 struct GNUNET_TIME_Absolute
394 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
395 struct GNUNET_TIME_Relative duration);
399 * Subtract a given relative duration from the
402 * @param start some absolute time
403 * @param duration some relative time to subtract
404 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
406 struct GNUNET_TIME_Absolute
407 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
408 struct GNUNET_TIME_Relative duration);
412 * Multiply relative time by a given factor.
414 * @param rel some duration
415 * @param factor integer to multiply with
416 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
418 struct GNUNET_TIME_Relative
419 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
420 unsigned long long factor);
424 * Divide relative time by a given factor.
426 * @param rel some duration
427 * @param factor integer to divide by
428 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
430 struct GNUNET_TIME_Relative
431 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
432 unsigned long long factor);
436 * Add relative times together.
438 * @param a1 some relative time
439 * @param a2 some other relative time
440 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
442 struct GNUNET_TIME_Relative
443 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
444 struct GNUNET_TIME_Relative a2);
448 * Subtract relative timestamp from the other.
450 * @param a1 first timestamp
451 * @param a2 second timestamp
452 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
454 struct GNUNET_TIME_Relative
455 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
456 struct GNUNET_TIME_Relative a2);
460 * Convert relative time to network byte order.
462 * @param a time to convert
463 * @return converted time value
465 struct GNUNET_TIME_RelativeNBO
466 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
470 * Convert relative time from network byte order.
472 * @param a time to convert
473 * @return converted time value
475 struct GNUNET_TIME_Relative
476 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
480 * Convert absolute time to network byte order.
482 * @param a time to convert
483 * @return converted time value
485 struct GNUNET_TIME_AbsoluteNBO
486 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
490 * Convert absolute time from network byte order.
492 * @param a time to convert
493 * @return converted time value
495 struct GNUNET_TIME_Absolute
496 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
500 * Set the timestamp offset for this instance.
502 * @param offset the offset to skew the locale time by
505 GNUNET_TIME_set_offset (long long offset);
509 * Get the timestamp offset for this instance.
511 * @return the offset we currently skew the locale time by
514 GNUNET_TIME_get_offset (void);
518 * Return the current year (i.e. '2011').
521 GNUNET_TIME_get_current_year (void);
525 * Convert a year to an expiration time of January 1st of that year.
527 * @param year a year (after 1970, please ;-)).
528 * @return absolute time for January 1st of that year.
530 struct GNUNET_TIME_Absolute
531 GNUNET_TIME_year_to_time (unsigned int year);
535 * Convert an expiration time to the respective year (rounds)
537 * @param at absolute time
538 * @return year a year (after 1970), 0 on error
541 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
544 #if 0 /* keep Emacsens' auto-indent happy */
551 /* ifndef GNUNET_TIME_LIB_H */
554 /** @} */ /* end of group time */
556 /* end of gnunet_time_lib.h */