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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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));
180 * Randomized exponential back-off, starting at 1 ms
181 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
182 * to a maximum of the given threshold.
184 * @param rt current backoff time, initially zero
185 * @param threshold maximum value for backoff
186 * @return the next backoff time
188 struct GNUNET_TIME_Relative
189 GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold);
193 * Return relative time of 0ms.
195 struct GNUNET_TIME_Relative
196 GNUNET_TIME_relative_get_zero_ (void);
200 * Return absolute time of 0ms.
202 struct GNUNET_TIME_Absolute
203 GNUNET_TIME_absolute_get_zero_ (void);
207 * Return relative time of 1 microsecond.
209 struct GNUNET_TIME_Relative
210 GNUNET_TIME_relative_get_unit_ (void);
214 * Return relative time of 1ms.
216 struct GNUNET_TIME_Relative
217 GNUNET_TIME_relative_get_millisecond_ (void);
221 * Return relative time of 1s.
223 struct GNUNET_TIME_Relative
224 GNUNET_TIME_relative_get_second_ (void);
228 * Return relative time of 1 minute.
230 struct GNUNET_TIME_Relative
231 GNUNET_TIME_relative_get_minute_ (void);
235 * Return relative time of 1 hour.
237 struct GNUNET_TIME_Relative
238 GNUNET_TIME_relative_get_hour_ (void);
244 struct GNUNET_TIME_Relative
245 GNUNET_TIME_relative_get_forever_ (void);
251 struct GNUNET_TIME_Absolute
252 GNUNET_TIME_absolute_get_forever_ (void);
256 * Get the current time.
258 * @return the current time
260 struct GNUNET_TIME_Absolute
261 GNUNET_TIME_absolute_get (void);
265 * Convert relative time to an absolute time in the
268 * @param rel relative time to convert
269 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
271 struct GNUNET_TIME_Absolute
272 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
276 * Round a time value so that it is suitable for transmission
277 * via JSON encodings.
279 * @param at time to round
280 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
281 * it was just now rounded
284 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at);
288 * Round a time value so that it is suitable for transmission
289 * via JSON encodings.
291 * @param rt time to round
292 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
293 * it was just now rounded
296 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
300 * Return the minimum of two relative time values.
302 * @param t1 first timestamp
303 * @param t2 other timestamp
304 * @return timestamp that is smaller
306 struct GNUNET_TIME_Relative
307 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
308 struct GNUNET_TIME_Relative t2);
313 * Return the maximum of two relative time values.
315 * @param t1 first timestamp
316 * @param t2 other timestamp
317 * @return timestamp that is larger
319 struct GNUNET_TIME_Relative
320 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
321 struct GNUNET_TIME_Relative t2);
325 * Return the minimum of two absolute time values.
327 * @param t1 first timestamp
328 * @param t2 other timestamp
329 * @return timestamp that is smaller
331 struct GNUNET_TIME_Absolute
332 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
333 struct GNUNET_TIME_Absolute t2);
337 * Return the maximum of two absolute time values.
339 * @param t1 first timestamp
340 * @param t2 other timestamp
341 * @return timestamp that is smaller
343 struct GNUNET_TIME_Absolute
344 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
345 struct GNUNET_TIME_Absolute t2);
349 * Given a timestamp in the future, how much time
350 * remains until then?
352 * @param future some absolute time, typically in the future
353 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
355 struct GNUNET_TIME_Relative
356 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
360 * Calculate the estimate time of arrival/completion
363 * @param start when did the operation start?
364 * @param finished how much has been done?
365 * @param total how much must be done overall (same unit as for "finished")
366 * @return remaining duration for the operation,
367 * assuming it continues at the same speed
369 struct GNUNET_TIME_Relative
370 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
376 * Compute the time difference between the given start and end times.
377 * Use this function instead of actual subtraction to ensure that
378 * "FOREVER" and overflows are handeled correctly.
380 * @param start some absolute time
381 * @param end some absolute time (typically larger or equal to start)
382 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
384 struct GNUNET_TIME_Relative
385 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
386 struct GNUNET_TIME_Absolute end);
390 * Get the duration of an operation as the
391 * difference of the current time and the given start time "hence".
393 * @param whence some absolute time, typically in the past
394 * @return 0 if hence > now, otherwise now-hence.
396 struct GNUNET_TIME_Relative
397 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
401 * Add a given relative duration to the
404 * @param start some absolute time
405 * @param duration some relative time to add
406 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
408 struct GNUNET_TIME_Absolute
409 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
410 struct GNUNET_TIME_Relative duration);
414 * Subtract a given relative duration from the
417 * @param start some absolute time
418 * @param duration some relative time to subtract
419 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
421 struct GNUNET_TIME_Absolute
422 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
423 struct GNUNET_TIME_Relative duration);
427 * Multiply relative time by a given factor.
429 * @param rel some duration
430 * @param factor integer to multiply with
431 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
433 struct GNUNET_TIME_Relative
434 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
435 unsigned long long factor);
439 * Saturating multiply relative time by a given factor.
441 * @param rel some duration
442 * @param factor integer to multiply with
443 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
445 struct GNUNET_TIME_Relative
446 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
447 unsigned long long factor);
451 * Divide relative time by a given factor.
453 * @param rel some duration
454 * @param factor integer to divide by
455 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
457 struct GNUNET_TIME_Relative
458 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
459 unsigned long long factor);
463 * Add relative times together.
465 * @param a1 some relative time
466 * @param a2 some other relative time
467 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
469 struct GNUNET_TIME_Relative
470 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
471 struct GNUNET_TIME_Relative a2);
475 * Subtract relative timestamp from the other.
477 * @param a1 first timestamp
478 * @param a2 second timestamp
479 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
481 struct GNUNET_TIME_Relative
482 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
483 struct GNUNET_TIME_Relative a2);
487 * Convert relative time to network byte order.
489 * @param a time to convert
490 * @return converted time value
492 struct GNUNET_TIME_RelativeNBO
493 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
497 * Convert relative time from network byte order.
499 * @param a time to convert
500 * @return converted time value
502 struct GNUNET_TIME_Relative
503 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
507 * Convert absolute time to network byte order.
509 * @param a time to convert
510 * @return converted time value
512 struct GNUNET_TIME_AbsoluteNBO
513 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
517 * Convert absolute time from network byte order.
519 * @param a time to convert
520 * @return converted time value
522 struct GNUNET_TIME_Absolute
523 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
527 * Set the timestamp offset for this instance.
529 * @param offset the offset to skew the locale time by
532 GNUNET_TIME_set_offset (long long offset);
536 * Get the timestamp offset for this instance.
538 * @return the offset we currently skew the locale time by
541 GNUNET_TIME_get_offset (void);
545 * Return the current year (i.e. '2011').
548 GNUNET_TIME_get_current_year (void);
552 * Convert a year to an expiration time of January 1st of that year.
554 * @param year a year (after 1970, please ;-)).
555 * @return absolute time for January 1st of that year.
557 struct GNUNET_TIME_Absolute
558 GNUNET_TIME_year_to_time (unsigned int year);
562 * Convert an expiration time to the respective year (rounds)
564 * @param at absolute time
565 * @return year a year (after 1970), 0 on error
568 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
572 * A configuration object.
574 struct GNUNET_CONFIGURATION_Handle;
578 * Obtain the current time and make sure it is monotonically
579 * increasing. Guards against systems without an RTC or
580 * clocks running backwards and other nasty surprises. Does
581 * not guarantee that the returned time is near the current
582 * time returned by #GNUNET_TIME_absolute_get(). Two
583 * subsequent calls (within a short time period) may return the
584 * same value. Persists the last returned time on disk to
585 * ensure that time never goes backwards. As a result, the
586 * resulting value can be used to check if a message is the
587 * "most recent" value and replays of older messages (from
588 * the same origin) would be discarded.
590 * @param cfg configuration, used to determine where to
591 * store the time; user can also insist RTC is working
592 * nicely and disable the feature
593 * @return monotonically increasing time
595 struct GNUNET_TIME_Absolute
596 GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg);
599 #if 0 /* keep Emacsens' auto-indent happy */
606 /* ifndef GNUNET_TIME_LIB_H */
609 /** @} */ /* end of group time */
611 /* end of gnunet_time_lib.h */