2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_time_lib.h
23 * @brief functions related to time
25 * @author Christian Grothoff
28 #ifndef GNUNET_TIME_LIB_H
29 #define GNUNET_TIME_LIB_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_common.h"
42 * Time for absolute times used by GNUnet, in milliseconds.
44 struct GNUNET_TIME_Absolute
53 * Time for relative time used by GNUnet, in milliseconds.
54 * Always positive, so we can only refer to future time.
56 struct GNUNET_TIME_Relative
64 GNUNET_NETWORK_STRUCT_BEGIN
67 * Time for relative time used by GNUnet, in milliseconds and in network byte order.
69 struct GNUNET_TIME_RelativeNBO
72 * The actual value (in network byte order).
74 uint64_t rel_value__ GNUNET_PACKED;
79 * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
81 struct GNUNET_TIME_AbsoluteNBO
84 * The actual value (in network byte order).
86 uint64_t abs_value__ GNUNET_PACKED;
88 GNUNET_NETWORK_STRUCT_END
93 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero_()
98 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
101 * One millisecond, our basic time unit.
103 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit_()
108 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
113 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
118 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_get_hour_()
123 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
128 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
131 * One month (30 days).
133 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
136 * One year (365 days).
138 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
141 * Constant used to specify "forever". This constant
142 * will be treated specially in all time operations.
144 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
147 * Constant used to specify "forever". This constant
148 * will be treated specially in all time operations.
150 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
154 * Return relative time of 0ms.
156 struct GNUNET_TIME_Relative
157 GNUNET_TIME_relative_get_zero_ (void);
161 * Return absolute time of 0ms.
163 struct GNUNET_TIME_Absolute
164 GNUNET_TIME_absolute_get_zero_ (void);
168 * Return relative time of 1ms.
170 struct GNUNET_TIME_Relative
171 GNUNET_TIME_relative_get_unit_ (void);
175 * Return relative time of 1s.
177 struct GNUNET_TIME_Relative
178 GNUNET_TIME_relative_get_second_ (void);
182 * Return relative time of 1 minute.
184 struct GNUNET_TIME_Relative
185 GNUNET_TIME_relative_get_minute_ (void);
189 * Return relative time of 1 hour.
191 struct GNUNET_TIME_Relative
192 GNUNET_TIME_relative_get_hour_ (void);
198 struct GNUNET_TIME_Relative
199 GNUNET_TIME_relative_get_forever_ (void);
205 struct GNUNET_TIME_Absolute
206 GNUNET_TIME_absolute_get_forever_ (void);
210 * Get the current time.
212 * @return the current time
214 struct GNUNET_TIME_Absolute
215 GNUNET_TIME_absolute_get (void);
219 * Convert relative time to an absolute time in the
222 * @param rel relative time to convert
223 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
225 struct GNUNET_TIME_Absolute
226 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
230 * Return the minimum of two relative time values.
232 * @param t1 first timestamp
233 * @param t2 other timestamp
234 * @return timestamp that is smaller
236 struct GNUNET_TIME_Relative
237 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
238 struct GNUNET_TIME_Relative t2);
243 * Return the maximum of two relative time values.
245 * @param t1 first timestamp
246 * @param t2 other timestamp
247 * @return timestamp that is larger
249 struct GNUNET_TIME_Relative
250 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
251 struct GNUNET_TIME_Relative t2);
255 * Return the minimum of two absolute time values.
257 * @param t1 first timestamp
258 * @param t2 other timestamp
259 * @return timestamp that is smaller
261 struct GNUNET_TIME_Absolute
262 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
263 struct GNUNET_TIME_Absolute t2);
267 * Return the maximum of two absolute time values.
269 * @param t1 first timestamp
270 * @param t2 other timestamp
271 * @return timestamp that is smaller
273 struct GNUNET_TIME_Absolute
274 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
275 struct GNUNET_TIME_Absolute t2);
279 * Given a timestamp in the future, how much time
280 * remains until then?
282 * @param future some absolute time, typically in the future
283 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
285 struct GNUNET_TIME_Relative
286 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
290 * Calculate the estimate time of arrival/completion
293 * @param start when did the operation start?
294 * @param finished how much has been done?
295 * @param total how much must be done overall (same unit as for "finished")
296 * @return remaining duration for the operation,
297 * assuming it continues at the same speed
299 struct GNUNET_TIME_Relative
300 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
305 * Compute the time difference between the given start and end times.
306 * Use this function instead of actual subtraction to ensure that
307 * "FOREVER" and overflows are handeled correctly.
309 * @param start some absolute time
310 * @param end some absolute time (typically larger or equal to start)
311 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
313 struct GNUNET_TIME_Relative
314 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
315 struct GNUNET_TIME_Absolute end);
319 * Get the duration of an operation as the
320 * difference of the current time and the given start time "hence".
322 * @param whence some absolute time, typically in the past
323 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
325 struct GNUNET_TIME_Relative
326 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
330 * Add a given relative duration to the
333 * @param start some absolute time
334 * @param duration some relative time to add
335 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
337 struct GNUNET_TIME_Absolute
338 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
339 struct GNUNET_TIME_Relative duration);
343 * Subtract a given relative duration from the
346 * @param start some absolute time
347 * @param duration some relative time to subtract
348 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
350 struct GNUNET_TIME_Absolute
351 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
352 struct GNUNET_TIME_Relative duration);
356 * Multiply relative time by a given factor.
358 * @param rel some duration
359 * @param factor integer to multiply with
360 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
362 struct GNUNET_TIME_Relative
363 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
364 unsigned int factor);
368 * Divide relative time by a given factor.
370 * @param rel some duration
371 * @param factor integer to divide by
372 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
374 struct GNUNET_TIME_Relative
375 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
376 unsigned int factor);
380 * Add relative times together.
382 * @param a1 some relative time
383 * @param a2 some other relative time
384 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
386 struct GNUNET_TIME_Relative
387 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
388 struct GNUNET_TIME_Relative a2);
392 * Subtract relative timestamp from the other.
394 * @param a1 first timestamp
395 * @param a2 second timestamp
396 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
398 struct GNUNET_TIME_Relative
399 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
400 struct GNUNET_TIME_Relative a2);
404 * Convert relative time to network byte order.
406 * @param a time to convert
407 * @return converted time value
409 struct GNUNET_TIME_RelativeNBO
410 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
414 * Convert relative time from network byte order.
416 * @param a time to convert
417 * @return converted time value
419 struct GNUNET_TIME_Relative
420 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
424 * Convert relative time to network byte order.
426 * @param a time to convert
427 * @return converted time value
429 struct GNUNET_TIME_AbsoluteNBO
430 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
434 * Convert relative time from network byte order.
436 * @param a time to convert
437 * @return converted time value
439 struct GNUNET_TIME_Absolute
440 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
444 * Convert a relative time to a string.
445 * NOT reentrant! FIXME: duplicate with GNUNET_STRINGS_relative_time_to_string!
447 * @param time the time to print
449 * @return string form of the time (as milliseconds)
452 GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time);
456 * Set the timestamp offset for this instance.
458 * @param offset the offset to skew the locale time by
461 GNUNET_TIME_set_offset (long long offset);
464 #if 0 /* keep Emacsens' auto-indent happy */
471 /* ifndef GNUNET_TIME_LIB_H */
473 /* end of gnunet_time_lib.h */