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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @author Christian Grothoff
23 * Functions related to time
25 * @defgroup time Time library
26 * Time and time calculations.
30 #ifndef GNUNET_TIME_LIB_H
31 #define GNUNET_TIME_LIB_H
36 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_common.h"
44 * Time for absolute times used by GNUnet, in microseconds.
46 struct GNUNET_TIME_Absolute
51 uint64_t abs_value_us;
55 * Time for relative time used by GNUnet, in microseconds.
56 * Always positive, so we can only refer to future time.
58 struct GNUNET_TIME_Relative
63 uint64_t rel_value_us;
66 GNUNET_NETWORK_STRUCT_BEGIN
69 * Time for relative time used by GNUnet, in microseconds and in network byte order.
71 struct GNUNET_TIME_RelativeNBO
74 * The actual value (in network byte order).
76 uint64_t rel_value_us__ GNUNET_PACKED;
81 * Time for absolute time used by GNUnet, in microseconds and in network byte order.
83 struct GNUNET_TIME_AbsoluteNBO
86 * The actual value (in network byte order).
88 uint64_t abs_value_us__ GNUNET_PACKED;
90 GNUNET_NETWORK_STRUCT_END
95 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero_()
100 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
103 * One microsecond, our basic time unit.
105 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
110 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
115 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
120 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
125 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
130 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
135 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
138 * One month (30 days).
140 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
143 * One year (365 days).
145 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
148 * Constant used to specify "forever". This constant
149 * will be treated specially in all time operations.
151 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
154 * Constant used to specify "forever". This constant
155 * will be treated specially in all time operations.
157 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
162 * Threshold after which exponential backoff should not increase (15 m).
164 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
168 * Perform our standard exponential back-off calculation, starting at 1 ms
169 * and then going by a factor of 2 up unto a maximum of 15 m.
171 * @param r current backoff time, initially zero
173 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
174 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
178 * Randomized exponential back-off, starting at 1 ms
179 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
180 * to a maximum of the given threshold.
182 * @param rt current backoff time, initially zero
183 * @param threshold maximum value for backoff
184 * @return the next backoff time
186 struct GNUNET_TIME_Relative
187 GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold);
191 * Return relative time of 0ms.
193 struct GNUNET_TIME_Relative
194 GNUNET_TIME_relative_get_zero_ (void);
198 * Return absolute time of 0ms.
200 struct GNUNET_TIME_Absolute
201 GNUNET_TIME_absolute_get_zero_ (void);
205 * Return relative time of 1 microsecond.
207 struct GNUNET_TIME_Relative
208 GNUNET_TIME_relative_get_unit_ (void);
212 * Return relative time of 1ms.
214 struct GNUNET_TIME_Relative
215 GNUNET_TIME_relative_get_millisecond_ (void);
219 * Return relative time of 1s.
221 struct GNUNET_TIME_Relative
222 GNUNET_TIME_relative_get_second_ (void);
226 * Return relative time of 1 minute.
228 struct GNUNET_TIME_Relative
229 GNUNET_TIME_relative_get_minute_ (void);
233 * Return relative time of 1 hour.
235 struct GNUNET_TIME_Relative
236 GNUNET_TIME_relative_get_hour_ (void);
242 struct GNUNET_TIME_Relative
243 GNUNET_TIME_relative_get_forever_ (void);
249 struct GNUNET_TIME_Absolute
250 GNUNET_TIME_absolute_get_forever_ (void);
254 * Get the current time.
256 * @return the current time
258 struct GNUNET_TIME_Absolute
259 GNUNET_TIME_absolute_get (void);
263 * Convert relative time to an absolute time in the
266 * @param rel relative time to convert
267 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
269 struct GNUNET_TIME_Absolute
270 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
274 * Round a time value so that it is suitable for transmission
275 * via JSON encodings.
277 * @param at 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_abs (struct GNUNET_TIME_Absolute *at);
286 * Round a time value so that it is suitable for transmission
287 * via JSON encodings.
289 * @param rt time to round
290 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
291 * it was just now rounded
294 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
298 * Return the minimum of two relative time values.
300 * @param t1 first timestamp
301 * @param t2 other timestamp
302 * @return timestamp that is smaller
304 struct GNUNET_TIME_Relative
305 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
306 struct GNUNET_TIME_Relative t2);
311 * Return the maximum of two relative time values.
313 * @param t1 first timestamp
314 * @param t2 other timestamp
315 * @return timestamp that is larger
317 struct GNUNET_TIME_Relative
318 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
319 struct GNUNET_TIME_Relative t2);
323 * Return the minimum 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_min (struct GNUNET_TIME_Absolute t1,
331 struct GNUNET_TIME_Absolute t2);
335 * Return the maximum of two absolute time values.
337 * @param t1 first timestamp
338 * @param t2 other timestamp
339 * @return timestamp that is smaller
341 struct GNUNET_TIME_Absolute
342 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
343 struct GNUNET_TIME_Absolute t2);
347 * Given a timestamp in the future, how much time
348 * remains until then?
350 * @param future some absolute time, typically in the future
351 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
353 struct GNUNET_TIME_Relative
354 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
358 * Calculate the estimate time of arrival/completion
361 * @param start when did the operation start?
362 * @param finished how much has been done?
363 * @param total how much must be done overall (same unit as for "finished")
364 * @return remaining duration for the operation,
365 * assuming it continues at the same speed
367 struct GNUNET_TIME_Relative
368 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
373 * Compute the time difference between the given start and end times.
374 * Use this function instead of actual subtraction to ensure that
375 * "FOREVER" and overflows are handeled correctly.
377 * @param start some absolute time
378 * @param end some absolute time (typically larger or equal to start)
379 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
381 struct GNUNET_TIME_Relative
382 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
383 struct GNUNET_TIME_Absolute end);
387 * Get the duration of an operation as the
388 * difference of the current time and the given start time "hence".
390 * @param whence some absolute time, typically in the past
391 * @return 0 if hence > now, otherwise now-hence.
393 struct GNUNET_TIME_Relative
394 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
398 * Add a given relative duration to the
401 * @param start some absolute time
402 * @param duration some relative time to add
403 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
405 struct GNUNET_TIME_Absolute
406 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
407 struct GNUNET_TIME_Relative duration);
411 * Subtract a given relative duration from the
414 * @param start some absolute time
415 * @param duration some relative time to subtract
416 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
418 struct GNUNET_TIME_Absolute
419 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
420 struct GNUNET_TIME_Relative duration);
424 * Multiply relative time by a given factor.
426 * @param rel some duration
427 * @param factor integer to multiply with
428 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
430 struct GNUNET_TIME_Relative
431 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
432 unsigned long long factor);
436 * Saturating multiply relative time by a given factor.
438 * @param rel some duration
439 * @param factor integer to multiply with
440 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
442 struct GNUNET_TIME_Relative
443 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
444 unsigned long long factor);
448 * Divide relative time by a given factor.
450 * @param rel some duration
451 * @param factor integer to divide by
452 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
454 struct GNUNET_TIME_Relative
455 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
456 unsigned long long factor);
460 * Add relative times together.
462 * @param a1 some relative time
463 * @param a2 some other relative time
464 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
466 struct GNUNET_TIME_Relative
467 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
468 struct GNUNET_TIME_Relative a2);
472 * Subtract relative timestamp from the other.
474 * @param a1 first timestamp
475 * @param a2 second timestamp
476 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
478 struct GNUNET_TIME_Relative
479 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
480 struct GNUNET_TIME_Relative a2);
484 * Convert relative time to network byte order.
486 * @param a time to convert
487 * @return converted time value
489 struct GNUNET_TIME_RelativeNBO
490 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
494 * Convert relative time from network byte order.
496 * @param a time to convert
497 * @return converted time value
499 struct GNUNET_TIME_Relative
500 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
504 * Convert absolute time to network byte order.
506 * @param a time to convert
507 * @return converted time value
509 struct GNUNET_TIME_AbsoluteNBO
510 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
514 * Convert absolute time from network byte order.
516 * @param a time to convert
517 * @return converted time value
519 struct GNUNET_TIME_Absolute
520 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
524 * Set the timestamp offset for this instance.
526 * @param offset the offset to skew the locale time by
529 GNUNET_TIME_set_offset (long long offset);
533 * Get the timestamp offset for this instance.
535 * @return the offset we currently skew the locale time by
538 GNUNET_TIME_get_offset (void);
542 * Return the current year (i.e. '2011').
545 GNUNET_TIME_get_current_year (void);
549 * Convert a year to an expiration time of January 1st of that year.
551 * @param year a year (after 1970, please ;-)).
552 * @return absolute time for January 1st of that year.
554 struct GNUNET_TIME_Absolute
555 GNUNET_TIME_year_to_time (unsigned int year);
559 * Convert an expiration time to the respective year (rounds)
561 * @param at absolute time
562 * @return year a year (after 1970), 0 on error
565 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
568 #if 0 /* keep Emacsens' auto-indent happy */
575 /* ifndef GNUNET_TIME_LIB_H */
578 /** @} */ /* end of group time */
580 /* end of gnunet_time_lib.h */