2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file include/gnunet_time_lib.h
23 * @brief functions related to time
24 * @author Christian Grothoff
25 * @defgroup time Time and time calculations
29 #ifndef GNUNET_TIME_LIB_H
30 #define GNUNET_TIME_LIB_H
35 #if 0 /* keep Emacsens' auto-indent happy */
40 #include "gnunet_common.h"
43 * Time for absolute times used by GNUnet, in microseconds.
45 struct GNUNET_TIME_Absolute
50 uint64_t abs_value_us;
54 * Time for relative time used by GNUnet, in microseconds.
55 * Always positive, so we can only refer to future time.
57 struct GNUNET_TIME_Relative
62 uint64_t rel_value_us;
65 GNUNET_NETWORK_STRUCT_BEGIN
68 * Time for relative time used by GNUnet, in microseconds and in network byte order.
70 struct GNUNET_TIME_RelativeNBO
73 * The actual value (in network byte order).
75 uint64_t rel_value_us__ GNUNET_PACKED;
80 * Time for absolute time used by GNUnet, in microseconds and in network byte order.
82 struct GNUNET_TIME_AbsoluteNBO
85 * The actual value (in network byte order).
87 uint64_t abs_value_us__ GNUNET_PACKED;
89 GNUNET_NETWORK_STRUCT_END
94 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero_()
99 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
102 * One microsecond, our basic time unit.
104 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
109 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
114 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
119 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
124 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
129 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
134 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
137 * One month (30 days).
139 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
142 * One year (365 days).
144 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
147 * Constant used to specify "forever". This constant
148 * will be treated specially in all time operations.
150 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
153 * Constant used to specify "forever". This constant
154 * will be treated specially in all time operations.
156 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
161 * Threshold after which exponential backoff should not increase (15 m).
163 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
167 * Perform our standard exponential back-off calculation, starting at 1 ms
168 * and then going by a factor of 2 up unto a maximum of 15 m.
170 * @param r current backoff time, initially zero
172 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
173 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
176 * Return relative time of 0ms.
178 struct GNUNET_TIME_Relative
179 GNUNET_TIME_relative_get_zero_ (void);
183 * Return absolute time of 0ms.
185 struct GNUNET_TIME_Absolute
186 GNUNET_TIME_absolute_get_zero_ (void);
190 * Return relative time of 1 microsecond.
192 struct GNUNET_TIME_Relative
193 GNUNET_TIME_relative_get_unit_ (void);
197 * Return relative time of 1ms.
199 struct GNUNET_TIME_Relative
200 GNUNET_TIME_relative_get_millisecond_ (void);
204 * Return relative time of 1s.
206 struct GNUNET_TIME_Relative
207 GNUNET_TIME_relative_get_second_ (void);
211 * Return relative time of 1 minute.
213 struct GNUNET_TIME_Relative
214 GNUNET_TIME_relative_get_minute_ (void);
218 * Return relative time of 1 hour.
220 struct GNUNET_TIME_Relative
221 GNUNET_TIME_relative_get_hour_ (void);
227 struct GNUNET_TIME_Relative
228 GNUNET_TIME_relative_get_forever_ (void);
234 struct GNUNET_TIME_Absolute
235 GNUNET_TIME_absolute_get_forever_ (void);
239 * Get the current time.
241 * @return the current time
243 struct GNUNET_TIME_Absolute
244 GNUNET_TIME_absolute_get (void);
248 * Convert relative time to an absolute time in the
251 * @param rel relative time to convert
252 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
254 struct GNUNET_TIME_Absolute
255 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
259 * Return the minimum of two relative time values.
261 * @param t1 first timestamp
262 * @param t2 other timestamp
263 * @return timestamp that is smaller
265 struct GNUNET_TIME_Relative
266 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
267 struct GNUNET_TIME_Relative t2);
272 * Return the maximum of two relative time values.
274 * @param t1 first timestamp
275 * @param t2 other timestamp
276 * @return timestamp that is larger
278 struct GNUNET_TIME_Relative
279 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
280 struct GNUNET_TIME_Relative t2);
284 * Return the minimum of two absolute time values.
286 * @param t1 first timestamp
287 * @param t2 other timestamp
288 * @return timestamp that is smaller
290 struct GNUNET_TIME_Absolute
291 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
292 struct GNUNET_TIME_Absolute t2);
296 * Return the maximum of two absolute time values.
298 * @param t1 first timestamp
299 * @param t2 other timestamp
300 * @return timestamp that is smaller
302 struct GNUNET_TIME_Absolute
303 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
304 struct GNUNET_TIME_Absolute t2);
308 * Given a timestamp in the future, how much time
309 * remains until then?
311 * @param future some absolute time, typically in the future
312 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
314 struct GNUNET_TIME_Relative
315 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
319 * Calculate the estimate time of arrival/completion
322 * @param start when did the operation start?
323 * @param finished how much has been done?
324 * @param total how much must be done overall (same unit as for "finished")
325 * @return remaining duration for the operation,
326 * assuming it continues at the same speed
328 struct GNUNET_TIME_Relative
329 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
334 * Compute the time difference between the given start and end times.
335 * Use this function instead of actual subtraction to ensure that
336 * "FOREVER" and overflows are handeled correctly.
338 * @param start some absolute time
339 * @param end some absolute time (typically larger or equal to start)
340 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
342 struct GNUNET_TIME_Relative
343 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
344 struct GNUNET_TIME_Absolute end);
348 * Get the duration of an operation as the
349 * difference of the current time and the given start time "hence".
351 * @param whence some absolute time, typically in the past
352 * @return 0 if hence > now, otherwise now-hence.
354 struct GNUNET_TIME_Relative
355 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
359 * Add a given relative duration to the
362 * @param start some absolute time
363 * @param duration some relative time to add
364 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
366 struct GNUNET_TIME_Absolute
367 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
368 struct GNUNET_TIME_Relative duration);
372 * Subtract a given relative duration from the
375 * @param start some absolute time
376 * @param duration some relative time to subtract
377 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
379 struct GNUNET_TIME_Absolute
380 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
381 struct GNUNET_TIME_Relative duration);
385 * Multiply relative time by a given factor.
387 * @param rel some duration
388 * @param factor integer to multiply with
389 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
391 struct GNUNET_TIME_Relative
392 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
393 unsigned int factor);
397 * Divide relative time by a given factor.
399 * @param rel some duration
400 * @param factor integer to divide by
401 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
403 struct GNUNET_TIME_Relative
404 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
405 unsigned int factor);
409 * Add relative times together.
411 * @param a1 some relative time
412 * @param a2 some other relative time
413 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
415 struct GNUNET_TIME_Relative
416 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
417 struct GNUNET_TIME_Relative a2);
421 * Subtract relative timestamp from the other.
423 * @param a1 first timestamp
424 * @param a2 second timestamp
425 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
427 struct GNUNET_TIME_Relative
428 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
429 struct GNUNET_TIME_Relative a2);
433 * Convert relative time to network byte order.
435 * @param a time to convert
436 * @return converted time value
438 struct GNUNET_TIME_RelativeNBO
439 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
443 * Convert relative time from network byte order.
445 * @param a time to convert
446 * @return converted time value
448 struct GNUNET_TIME_Relative
449 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
453 * Convert absolute time to network byte order.
455 * @param a time to convert
456 * @return converted time value
458 struct GNUNET_TIME_AbsoluteNBO
459 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
463 * Convert absolute time from network byte order.
465 * @param a time to convert
466 * @return converted time value
468 struct GNUNET_TIME_Absolute
469 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
473 * Set the timestamp offset for this instance.
475 * @param offset the offset to skew the locale time by
478 GNUNET_TIME_set_offset (long long offset);
482 * Get the timestamp offset for this instance.
484 * @return the offset we currently skew the locale time by
487 GNUNET_TIME_get_offset (void);
491 * Return the current year (i.e. '2011').
494 GNUNET_TIME_get_current_year (void);
498 * Convert a year to an expiration time of January 1st of that year.
500 * @param year a year (after 1970, please ;-)).
501 * @return absolute time for January 1st of that year.
503 struct GNUNET_TIME_Absolute
504 GNUNET_TIME_year_to_time (unsigned int year);
508 * Convert an expiration time to the respective year (rounds)
510 * @param at absolute time
511 * @return year a year (after 1970), 0 on error
514 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
517 #if 0 /* keep Emacsens' auto-indent happy */
524 /** @} */ /* end of group time */
527 /* ifndef GNUNET_TIME_LIB_H */
529 /* end of gnunet_time_lib.h */