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
50 * Time for relative time used by GNUnet, in milliseconds.
51 * Always positive, so we can only refer to future time.
53 struct GNUNET_TIME_Relative
60 * Time for relative time used by GNUnet, in milliseconds and in network byte order.
62 struct GNUNET_TIME_RelativeNBO
64 uint64_t value__ GNUNET_PACKED;
69 * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
71 struct GNUNET_TIME_AbsoluteNBO
73 uint64_t value__ GNUNET_PACKED;
78 * @brief constants to specify time
80 #define GNUNET_TIME_UNIT_ZERO GNUNET_TIME_relative_get_zero()
81 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero()
82 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit()
83 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1000)
84 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
85 #define GNUNET_TIME_UNIT_HOURS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
86 #define GNUNET_TIME_UNIT_DAYS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 24)
87 #define GNUNET_TIME_UNIT_WEEKS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 7)
88 #define GNUNET_TIME_UNIT_MONTHS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 30)
89 #define GNUNET_TIME_UNIT_YEARS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS, 365)
92 * Constant used to specify "forever". This constant
93 * will be treated specially in all time operations.
95 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever ()
98 * Constant used to specify "forever". This constant
99 * will be treated specially in all time operations.
101 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever ()
104 * Return relative time of 0ms.
106 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero (void);
109 * Return absolute time of 0ms.
111 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero (void);
114 * Return relative time of 1ms.
116 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit (void);
121 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever (void);
126 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever (void);
129 * Get the current time.
131 * @return the current time
133 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get (void);
136 * Convert relative time to an absolute time in the
139 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
141 struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute (struct
146 * Return the minimum of two relative time values.
148 * @return timestamp that is smaller
150 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
154 GNUNET_TIME_Relative t2);
157 * Given a timestamp in the future, how much time
158 * remains until then?
160 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
162 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct
168 * Calculate the estimate time of arrival/completion
171 * @param start when did the operation start?
172 * @param finished how much has been done?
173 * @param total how much must be done overall (same unit as for "finished")
174 * @return remaining duration for the operation,
175 * assuming it continues at the same speed
177 struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
183 * Compute the time difference between the given start and end times.
184 * Use this function instead of actual subtraction to ensure that
185 * "FOREVER" and overflows are handeled correctly.
187 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
189 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct
197 * Get the duration of an operation as the
198 * difference of the current time and the given start time "hence".
200 * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
202 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration (struct
208 * Add a given relative duration to the
211 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
213 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct
221 * Multiply relative time by a given factor.
223 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
225 struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct
232 * Add relative times together.
234 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
236 struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct
237 GNUNET_TIME_Relative a1,
244 * Convert relative time to network byte order.
246 struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton (struct
251 * Convert relative time from network byte order.
253 struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh (struct
254 GNUNET_TIME_RelativeNBO
258 * Convert relative time to network byte order.
260 struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton (struct
265 * Convert relative time from network byte order.
267 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh (struct
268 GNUNET_TIME_AbsoluteNBO
271 #if 0 /* keep Emacsens' auto-indent happy */
278 /* ifndef GNUNET_TIME_LIB_H */
280 /* end of gnunet_time_lib.h */