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 ( \
133 GNUNET_TIME_UNIT_HOURS, 24)
138 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply ( \
139 GNUNET_TIME_UNIT_DAYS, 7)
142 * One month (30 days).
144 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply ( \
145 GNUNET_TIME_UNIT_DAYS, 30)
148 * One year (365 days).
150 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply ( \
151 GNUNET_TIME_UNIT_DAYS, 365)
154 * Constant used to specify "forever". This constant
155 * will be treated specially in all time operations.
157 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
160 * Constant used to specify "forever". This constant
161 * will be treated specially in all time operations.
163 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
167 * Threshold after which exponential backoff should not increase (15 m).
169 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD \
170 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
174 * Perform our standard exponential back-off calculation, starting at 1 ms
175 * and then going by a factor of 2 up unto a maximum of 15 m.
177 * @param r current backoff time, initially zero
179 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min ( \
180 GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
181 GNUNET_TIME_relative_multiply ( \
182 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
186 * Randomized exponential back-off, starting at 1 ms
187 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
188 * to a maximum of the given threshold.
190 * @param rt current backoff time, initially zero
191 * @param threshold maximum value for backoff
192 * @return the next backoff time
194 struct GNUNET_TIME_Relative
195 GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt, struct
196 GNUNET_TIME_Relative threshold);
200 * Return a random time value between 0.5*r and 1.5*r.
202 * @param r input time for scaling
203 * @return randomized time
205 struct GNUNET_TIME_Relative
206 GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r);
210 * Return relative time of 0ms.
212 struct GNUNET_TIME_Relative
213 GNUNET_TIME_relative_get_zero_ (void);
217 * Return absolute time of 0ms.
219 struct GNUNET_TIME_Absolute
220 GNUNET_TIME_absolute_get_zero_ (void);
224 * Return relative time of 1 microsecond.
226 struct GNUNET_TIME_Relative
227 GNUNET_TIME_relative_get_unit_ (void);
231 * Return relative time of 1ms.
233 struct GNUNET_TIME_Relative
234 GNUNET_TIME_relative_get_millisecond_ (void);
238 * Return relative time of 1s.
240 struct GNUNET_TIME_Relative
241 GNUNET_TIME_relative_get_second_ (void);
245 * Return relative time of 1 minute.
247 struct GNUNET_TIME_Relative
248 GNUNET_TIME_relative_get_minute_ (void);
252 * Return relative time of 1 hour.
254 struct GNUNET_TIME_Relative
255 GNUNET_TIME_relative_get_hour_ (void);
261 struct GNUNET_TIME_Relative
262 GNUNET_TIME_relative_get_forever_ (void);
268 struct GNUNET_TIME_Absolute
269 GNUNET_TIME_absolute_get_forever_ (void);
273 * Get the current time.
275 * @return the current time
277 struct GNUNET_TIME_Absolute
278 GNUNET_TIME_absolute_get (void);
282 * Convert relative time to an absolute time in the
285 * @param rel relative time to convert
286 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
288 struct GNUNET_TIME_Absolute
289 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
293 * Round a time value so that it is suitable for transmission
294 * via JSON encodings.
296 * @param at time to round
297 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
298 * it was just now rounded
301 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at);
305 * Round a time value so that it is suitable for transmission
306 * via JSON encodings.
308 * @param rt time to round
309 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
310 * it was just now rounded
313 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt);
317 * Return the minimum of two relative time values.
319 * @param t1 first timestamp
320 * @param t2 other timestamp
321 * @return timestamp that is smaller
323 struct GNUNET_TIME_Relative
324 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
325 struct GNUNET_TIME_Relative t2);
329 * Return the maximum of two relative time values.
331 * @param t1 first timestamp
332 * @param t2 other timestamp
333 * @return timestamp that is larger
335 struct GNUNET_TIME_Relative
336 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
337 struct GNUNET_TIME_Relative t2);
341 * Return the minimum of two absolute time values.
343 * @param t1 first timestamp
344 * @param t2 other timestamp
345 * @return timestamp that is smaller
347 struct GNUNET_TIME_Absolute
348 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
349 struct GNUNET_TIME_Absolute t2);
353 * Return the maximum of two absolute time values.
355 * @param t1 first timestamp
356 * @param t2 other timestamp
357 * @return timestamp that is smaller
359 struct GNUNET_TIME_Absolute
360 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
361 struct GNUNET_TIME_Absolute t2);
365 * Given a timestamp in the future, how much time
366 * remains until then?
368 * @param future some absolute time, typically in the future
369 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
371 struct GNUNET_TIME_Relative
372 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
376 * Calculate the estimate time of arrival/completion
379 * @param start when did the operation start?
380 * @param finished how much has been done?
381 * @param total how much must be done overall (same unit as for "finished")
382 * @return remaining duration for the operation,
383 * assuming it continues at the same speed
385 struct GNUNET_TIME_Relative
386 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
392 * Compute the time difference between the given start and end times.
393 * Use this function instead of actual subtraction to ensure that
394 * "FOREVER" and overflows are handeled correctly.
396 * @param start some absolute time
397 * @param end some absolute time (typically larger or equal to start)
398 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
400 struct GNUNET_TIME_Relative
401 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
402 struct GNUNET_TIME_Absolute end);
406 * Get the duration of an operation as the
407 * difference of the current time and the given start time "hence".
409 * @param whence some absolute time, typically in the past
410 * @return 0 if hence > now, otherwise now-hence.
412 struct GNUNET_TIME_Relative
413 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
417 * Add a given relative duration to the
420 * @param start some absolute time
421 * @param duration some relative time to add
422 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
424 struct GNUNET_TIME_Absolute
425 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
426 struct GNUNET_TIME_Relative duration);
430 * Subtract a given relative duration from the
433 * @param start some absolute time
434 * @param duration some relative time to subtract
435 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
437 struct GNUNET_TIME_Absolute
438 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
439 struct GNUNET_TIME_Relative duration);
443 * Multiply relative time by a given factor.
445 * @param rel some duration
446 * @param factor integer to multiply with
447 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
449 struct GNUNET_TIME_Relative
450 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
451 unsigned long long factor);
455 * Saturating multiply relative time by a given factor.
457 * @param rel some duration
458 * @param factor integer to multiply with
459 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
461 struct GNUNET_TIME_Relative
462 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
463 unsigned long long factor);
467 * Divide relative time by a given factor.
469 * @param rel some duration
470 * @param factor integer to divide by
471 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
473 struct GNUNET_TIME_Relative
474 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
475 unsigned long long factor);
479 * Add relative times together.
481 * @param a1 some relative time
482 * @param a2 some other relative time
483 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
485 struct GNUNET_TIME_Relative
486 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
487 struct GNUNET_TIME_Relative a2);
491 * Subtract relative timestamp from the other.
493 * @param a1 first timestamp
494 * @param a2 second timestamp
495 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
497 struct GNUNET_TIME_Relative
498 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
499 struct GNUNET_TIME_Relative a2);
503 * Convert relative time to network byte order.
505 * @param a time to convert
506 * @return converted time value
508 struct GNUNET_TIME_RelativeNBO
509 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
513 * Convert relative time from network byte order.
515 * @param a time to convert
516 * @return converted time value
518 struct GNUNET_TIME_Relative
519 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
523 * Convert absolute time to network byte order.
525 * @param a time to convert
526 * @return converted time value
528 struct GNUNET_TIME_AbsoluteNBO
529 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
533 * Convert absolute time from network byte order.
535 * @param a time to convert
536 * @return converted time value
538 struct GNUNET_TIME_Absolute
539 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
543 * Set the timestamp offset for this instance.
545 * @param offset the offset to skew the locale time by
548 GNUNET_TIME_set_offset (long long offset);
552 * Get the timestamp offset for this instance.
554 * @return the offset we currently skew the locale time by
557 GNUNET_TIME_get_offset (void);
561 * Return the current year (i.e. '2011').
564 GNUNET_TIME_get_current_year (void);
568 * Convert a year to an expiration time of January 1st of that year.
570 * @param year a year (after 1970, please ;-)).
571 * @return absolute time for January 1st of that year.
573 struct GNUNET_TIME_Absolute
574 GNUNET_TIME_year_to_time (unsigned int year);
578 * Convert an expiration time to the respective year (rounds)
580 * @param at absolute time
581 * @return year a year (after 1970), 0 on error
584 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at);
588 * A configuration object.
590 struct GNUNET_CONFIGURATION_Handle;
594 * Obtain the current time and make sure it is monotonically
595 * increasing. Guards against systems without an RTC or
596 * clocks running backwards and other nasty surprises. Does
597 * not guarantee that the returned time is near the current
598 * time returned by #GNUNET_TIME_absolute_get(). Two
599 * subsequent calls (within a short time period) may return the
600 * same value. Persists the last returned time on disk to
601 * ensure that time never goes backwards. As a result, the
602 * resulting value can be used to check if a message is the
603 * "most recent" value and replays of older messages (from
604 * the same origin) would be discarded.
606 * @param cfg configuration, used to determine where to
607 * store the time; user can also insist RTC is working
608 * nicely and disable the feature
609 * @return monotonically increasing time
611 struct GNUNET_TIME_Absolute
612 GNUNET_TIME_absolute_get_monotonic (const struct
613 GNUNET_CONFIGURATION_Handle *cfg);
616 #if 0 /* keep Emacsens' auto-indent happy */
623 /* ifndef GNUNET_TIME_LIB_H */
626 /** @} */ /* end of group time */
628 /* end of gnunet_time_lib.h */