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 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.
17 * @author Christian Grothoff
20 * Functions related to time
22 * @defgroup time Time library
23 * Time and time calculations.
27 #ifndef GNUNET_TIME_LIB_H
28 #define GNUNET_TIME_LIB_H
33 #if 0 /* keep Emacsens' auto-indent happy */
38 #include "gnunet_common.h"
41 * Time for absolute times used by GNUnet, in microseconds.
43 struct GNUNET_TIME_Absolute
48 uint64_t abs_value_us;
52 * Time for relative time used by GNUnet, in microseconds.
53 * Always positive, so we can only refer to future time.
55 struct GNUNET_TIME_Relative
60 uint64_t rel_value_us;
63 GNUNET_NETWORK_STRUCT_BEGIN
66 * Time for relative time used by GNUnet, in microseconds and in network byte order.
68 struct GNUNET_TIME_RelativeNBO
71 * The actual value (in network byte order).
73 uint64_t rel_value_us__ GNUNET_PACKED;
78 * Time for absolute time used by GNUnet, in microseconds and in network byte order.
80 struct GNUNET_TIME_AbsoluteNBO
83 * The actual value (in network byte order).
85 uint64_t abs_value_us__ GNUNET_PACKED;
87 GNUNET_NETWORK_STRUCT_END
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 microsecond, our basic time unit.
102 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
107 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
112 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
117 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
122 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
127 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
132 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
135 * One month (30 days).
137 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
140 * One year (365 days).
142 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
145 * Constant used to specify "forever". This constant
146 * will be treated specially in all time operations.
148 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
151 * Constant used to specify "forever". This constant
152 * will be treated specially in all time operations.
154 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
159 * Threshold after which exponential backoff should not increase (15 m).
161 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
165 * Perform our standard exponential back-off calculation, starting at 1 ms
166 * and then going by a factor of 2 up unto a maximum of 15 m.
168 * @param r current backoff time, initially zero
170 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
171 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
174 * Return relative time of 0ms.
176 struct GNUNET_TIME_Relative
177 GNUNET_TIME_relative_get_zero_ (void);
181 * Return absolute time of 0ms.
183 struct GNUNET_TIME_Absolute
184 GNUNET_TIME_absolute_get_zero_ (void);
188 * Return relative time of 1 microsecond.
190 struct GNUNET_TIME_Relative
191 GNUNET_TIME_relative_get_unit_ (void);
195 * Return relative time of 1ms.
197 struct GNUNET_TIME_Relative
198 GNUNET_TIME_relative_get_millisecond_ (void);
202 * Return relative time of 1s.
204 struct GNUNET_TIME_Relative
205 GNUNET_TIME_relative_get_second_ (void);
209 * Return relative time of 1 minute.
211 struct GNUNET_TIME_Relative
212 GNUNET_TIME_relative_get_minute_ (void);
216 * Return relative time of 1 hour.
218 struct GNUNET_TIME_Relative
219 GNUNET_TIME_relative_get_hour_ (void);
225 struct GNUNET_TIME_Relative
226 GNUNET_TIME_relative_get_forever_ (void);
232 struct GNUNET_TIME_Absolute
233 GNUNET_TIME_absolute_get_forever_ (void);
237 * Get the current time.
239 * @return the current time
241 struct GNUNET_TIME_Absolute
242 GNUNET_TIME_absolute_get (void);
246 * Convert relative time to an absolute time in the
249 * @param rel relative time to convert
250 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
252 struct GNUNET_TIME_Absolute
253 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
257 * Round a time value so that it is suitable for transmission
258 * via JSON encodings.
260 * @param at time to round
261 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
262 * it was just now rounded
265 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at);
269 * Round a time value so that it is suitable for transmission
270 * via JSON encodings.
272 * @param rt time to round
273 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
274 * it was just now rounded
277 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
281 * Return the minimum of two relative time values.
283 * @param t1 first timestamp
284 * @param t2 other timestamp
285 * @return timestamp that is smaller
287 struct GNUNET_TIME_Relative
288 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
289 struct GNUNET_TIME_Relative t2);
294 * Return the maximum of two relative time values.
296 * @param t1 first timestamp
297 * @param t2 other timestamp
298 * @return timestamp that is larger
300 struct GNUNET_TIME_Relative
301 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
302 struct GNUNET_TIME_Relative t2);
306 * Return the minimum of two absolute time values.
308 * @param t1 first timestamp
309 * @param t2 other timestamp
310 * @return timestamp that is smaller
312 struct GNUNET_TIME_Absolute
313 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
314 struct GNUNET_TIME_Absolute t2);
318 * Return the maximum of two absolute time values.
320 * @param t1 first timestamp
321 * @param t2 other timestamp
322 * @return timestamp that is smaller
324 struct GNUNET_TIME_Absolute
325 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
326 struct GNUNET_TIME_Absolute t2);
330 * Given a timestamp in the future, how much time
331 * remains until then?
333 * @param future some absolute time, typically in the future
334 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
336 struct GNUNET_TIME_Relative
337 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
341 * Calculate the estimate time of arrival/completion
344 * @param start when did the operation start?
345 * @param finished how much has been done?
346 * @param total how much must be done overall (same unit as for "finished")
347 * @return remaining duration for the operation,
348 * assuming it continues at the same speed
350 struct GNUNET_TIME_Relative
351 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
356 * Compute the time difference between the given start and end times.
357 * Use this function instead of actual subtraction to ensure that
358 * "FOREVER" and overflows are handeled correctly.
360 * @param start some absolute time
361 * @param end some absolute time (typically larger or equal to start)
362 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
364 struct GNUNET_TIME_Relative
365 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
366 struct GNUNET_TIME_Absolute end);
370 * Get the duration of an operation as the
371 * difference of the current time and the given start time "hence".
373 * @param whence some absolute time, typically in the past
374 * @return 0 if hence > now, otherwise now-hence.
376 struct GNUNET_TIME_Relative
377 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
381 * Add a given relative duration to the
384 * @param start some absolute time
385 * @param duration some relative time to add
386 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
388 struct GNUNET_TIME_Absolute
389 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
390 struct GNUNET_TIME_Relative duration);
394 * Subtract a given relative duration from the
397 * @param start some absolute time
398 * @param duration some relative time to subtract
399 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
401 struct GNUNET_TIME_Absolute
402 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
403 struct GNUNET_TIME_Relative duration);
407 * Multiply relative time by a given factor.
409 * @param rel some duration
410 * @param factor integer to multiply with
411 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
413 struct GNUNET_TIME_Relative
414 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
415 unsigned long long factor);
419 * Saturating multiply relative time by a given factor.
421 * @param rel some duration
422 * @param factor integer to multiply with
423 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
425 struct GNUNET_TIME_Relative
426 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
427 unsigned long long factor);
431 * Divide relative time by a given factor.
433 * @param rel some duration
434 * @param factor integer to divide by
435 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
437 struct GNUNET_TIME_Relative
438 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
439 unsigned long long factor);
443 * Add relative times together.
445 * @param a1 some relative time
446 * @param a2 some other relative time
447 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
449 struct GNUNET_TIME_Relative
450 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
451 struct GNUNET_TIME_Relative a2);
455 * Subtract relative timestamp from the other.
457 * @param a1 first timestamp
458 * @param a2 second timestamp
459 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
461 struct GNUNET_TIME_Relative
462 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
463 struct GNUNET_TIME_Relative a2);
467 * Convert relative time to network byte order.
469 * @param a time to convert
470 * @return converted time value
472 struct GNUNET_TIME_RelativeNBO
473 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
477 * Convert relative time from network byte order.
479 * @param a time to convert
480 * @return converted time value
482 struct GNUNET_TIME_Relative
483 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
487 * Convert absolute time to network byte order.
489 * @param a time to convert
490 * @return converted time value
492 struct GNUNET_TIME_AbsoluteNBO
493 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
497 * Convert absolute time from network byte order.
499 * @param a time to convert
500 * @return converted time value
502 struct GNUNET_TIME_Absolute
503 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
507 * Set the timestamp offset for this instance.
509 * @param offset the offset to skew the locale time by
512 GNUNET_TIME_set_offset (long long offset);
516 * Get the timestamp offset for this instance.
518 * @return the offset we currently skew the locale time by
521 GNUNET_TIME_get_offset (void);
525 * Return the current year (i.e. '2011').
528 GNUNET_TIME_get_current_year (void);
532 * Convert a year to an expiration time of January 1st of that year.
534 * @param year a year (after 1970, please ;-)).
535 * @return absolute time for January 1st of that year.
537 struct GNUNET_TIME_Absolute
538 GNUNET_TIME_year_to_time (unsigned int year);
542 * Convert an expiration time to the respective year (rounds)
544 * @param at absolute time
545 * @return year a year (after 1970), 0 on error
548 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
551 #if 0 /* keep Emacsens' auto-indent happy */
558 /* ifndef GNUNET_TIME_LIB_H */
561 /** @} */ /* end of group time */
563 /* end of gnunet_time_lib.h */