stuff
[oweals/gnunet.git] / src / include / gnunet_time_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file include/gnunet_time_lib.h
23  * @brief functions related to time
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_TIME_LIB_H
29 #define GNUNET_TIME_LIB_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40
41 /**
42  * Time for absolute times used by GNUnet, in milliseconds.
43  */
44 struct GNUNET_TIME_Absolute
45 {
46   uint64_t value;
47 };
48
49 /**
50  * Time for relative time used by GNUnet, in milliseconds.
51  * Always positive, so we can only refer to future time.
52  */
53 struct GNUNET_TIME_Relative
54 {
55   uint64_t value;
56 };
57
58
59 /**
60  * Time for relative time used by GNUnet, in milliseconds and in network byte order.
61  */
62 struct GNUNET_TIME_RelativeNBO
63 {
64   uint64_t value GNUNET_PACKED;
65 };
66
67
68 /**
69  * Time for absolute time used by GNUnet, in milliseconds and in network byte order.
70  */
71 struct GNUNET_TIME_AbsoluteNBO
72 {
73   uint64_t value GNUNET_PACKED;
74 };
75
76 /**
77  * @brief constants to specify time
78  */
79 #define GNUNET_TIME_UNIT_ZERO     GNUNET_TIME_relative_get_zero()
80 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_unit()
81 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 1000)
82 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
83 #define GNUNET_TIME_UNIT_HOURS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
84 #define GNUNET_TIME_UNIT_DAYS    GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS,   24)
85 #define GNUNET_TIME_UNIT_WEEKS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,     7)
86 #define GNUNET_TIME_UNIT_MONTHS  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,    30)
87 #define GNUNET_TIME_UNIT_YEARS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,   365)
88
89 /**
90  * Constant used to specify "forever".  This constant
91  * will be treated specially in all time operations.
92  */
93 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever ()
94
95 /**
96  * Constant used to specify "forever".  This constant
97  * will be treated specially in all time operations.
98  */
99 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever ()
100
101 /**
102  * Return relative time of 0ms.
103  */
104 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero (void);
105
106 /**
107  * Return relative time of 1ms.
108  */
109 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit (void);
110
111 /**
112  * Return "forever".
113  */
114 struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever (void);
115
116 /**
117  * Return "forever".
118  */
119 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever (void);
120
121 /**
122  * Get the current time.
123  *
124  * @return the current time
125  */
126 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get (void);
127
128 /**
129  * Convert relative time to an absolute time in the
130  * future.
131  *
132  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
133  */
134 struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute (struct
135                                                               GNUNET_TIME_Relative
136                                                               rel);
137
138 /**
139  * Return the minimum of two relative time values.
140  *
141  * @return timestamp that is smaller
142  */
143 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
144                                                       GNUNET_TIME_Relative
145                                                       t1,
146                                                       struct
147                                                       GNUNET_TIME_Relative t2);
148
149 /**
150  * Given a timestamp in the future, how much time
151  * remains until then?
152  *
153  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
154  */
155 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining (struct
156                                                                 GNUNET_TIME_Absolute
157                                                                 future);
158
159 /**
160  * Compute the time difference between the given start and end times.
161  * Use this function instead of actual subtraction to ensure that
162  * "FOREVER" and overflows are handeled correctly.
163  *
164  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
165  */
166 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference (struct
167                                                                  GNUNET_TIME_Absolute
168                                                                  start,
169                                                                  struct
170                                                                  GNUNET_TIME_Absolute
171                                                                  end);
172
173 /**
174  * Get the duration of an operation as the
175  * difference of the current time and the given start time "hence".
176  *
177  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
178  */
179 struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration (struct
180                                                                GNUNET_TIME_Absolute
181                                                                hence);
182
183
184 /**
185  * Add a given relative duration to the
186  * given start time.
187  *
188  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
189  */
190 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add (struct
191                                                       GNUNET_TIME_Absolute
192                                                       start,
193                                                       struct
194                                                       GNUNET_TIME_Relative
195                                                       duration);
196
197 /**
198  * Multiply relative time by a given factor.
199  *
200  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
201  */
202 struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply (struct
203                                                            GNUNET_TIME_Relative
204                                                            rel,
205                                                            unsigned int
206                                                            factor);
207
208 /**
209  * Add relative times together.
210  *
211  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
212  */
213 struct GNUNET_TIME_Relative GNUNET_TIME_relative_add (struct
214                                                       GNUNET_TIME_Relative a1,
215                                                       struct
216                                                       GNUNET_TIME_Relative
217                                                       a2);
218
219
220 /**
221  * Convert relative time to network byte order.
222  */
223 struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton (struct
224                                                           GNUNET_TIME_Relative
225                                                           a);
226
227 /**
228  * Convert relative time from network byte order.
229  */
230 struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh (struct
231                                                        GNUNET_TIME_RelativeNBO
232                                                        a);
233
234 /**
235  * Convert relative time to network byte order.
236  */
237 struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton (struct
238                                                           GNUNET_TIME_Absolute
239                                                           a);
240
241 /**
242  * Convert relative time from network byte order.
243  */
244 struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh (struct
245                                                        GNUNET_TIME_AbsoluteNBO
246                                                        a);
247
248 #if 0                           /* keep Emacsens' auto-indent happy */
249 {
250 #endif
251 #ifdef __cplusplus
252 }
253 #endif
254
255 /* ifndef GNUNET_TIME_LIB_H */
256 #endif
257 /* end of gnunet_time_lib.h */