f818fc9a12c282b4befef1596af106b4528d0b4e
[oweals/gnunet.git] / src / util / time.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2006, 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 util/time.c
23  * @author Christian Grothoff
24  * @brief functions for handling time and time arithmetic
25  */
26 #include "platform.h"
27 #include "gnunet_time_lib.h"
28
29
30 /**
31  * Get the current time (works just as "time", just that we use the
32  * unit of time that the cron-jobs use (and is 64 bit)).
33  *
34  * @return the current time
35  */
36 struct GNUNET_TIME_Absolute
37 GNUNET_TIME_absolute_get ()
38 {
39   struct GNUNET_TIME_Absolute ret;
40   struct timeval tv;
41
42   GETTIMEOFDAY (&tv, NULL);
43   ret.value = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL) + ((uint64_t) tv.tv_usec / 1000LL));
44   return ret;
45 }
46
47
48 /**
49  * Return relative time of 0ms.
50  */
51 struct GNUNET_TIME_Relative
52 GNUNET_TIME_relative_get_zero ()
53 {
54   static struct GNUNET_TIME_Relative zero;
55   return zero;
56 }
57
58
59 /**
60  * Return absolute time of 0ms.
61  */
62 struct GNUNET_TIME_Absolute
63 GNUNET_TIME_absolute_get_zero ()
64 {
65   static struct GNUNET_TIME_Absolute zero;
66   return zero;
67 }
68
69 /**
70  * Return relative time of 1ms.
71  */
72 struct GNUNET_TIME_Relative
73 GNUNET_TIME_relative_get_unit ()
74 {
75   static struct GNUNET_TIME_Relative one = { 1 };
76   return one;
77 }
78
79 /**
80  * Return "forever".
81  */
82 struct GNUNET_TIME_Relative
83 GNUNET_TIME_relative_get_forever ()
84 {
85   static struct GNUNET_TIME_Relative forever = { (uint64_t) - 1LL };
86   return forever;
87 }
88
89 /**
90  * Return "forever".
91  */
92 struct GNUNET_TIME_Absolute
93 GNUNET_TIME_absolute_get_forever ()
94 {
95   static struct GNUNET_TIME_Absolute forever = { (uint64_t) - 1LL };
96   return forever;
97 }
98
99 /**
100  * Convert relative time to an absolute time in the
101  * future.
102  *
103  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
104  */
105 struct GNUNET_TIME_Absolute
106 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
107 {
108   struct GNUNET_TIME_Absolute ret;
109   if (rel.value == (uint64_t) -1LL)
110     return GNUNET_TIME_absolute_get_forever ();
111   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
112   if (rel.value + now.value < rel.value)
113     {
114       GNUNET_break (0);         /* overflow... */
115       return GNUNET_TIME_absolute_get_forever ();
116     }
117   ret.value = rel.value + now.value;
118   return ret;
119 }
120
121
122 /**
123  * Return the minimum of two relative time values.
124  *
125  * @return timestamp that is smaller
126  */
127 struct GNUNET_TIME_Relative GNUNET_TIME_relative_min (struct
128                                                       GNUNET_TIME_Relative
129                                                       t1,
130                                                       struct
131                                                       GNUNET_TIME_Relative t2)
132 {
133   return (t1.value < t2.value) ? t1 : t2;
134 }
135
136 /**
137  * Given a timestamp in the future, how much time
138  * remains until then?
139  *
140  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
141  */
142 struct GNUNET_TIME_Relative
143 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
144 {
145   struct GNUNET_TIME_Relative ret;
146   if (future.value == (uint64_t) - 1LL)
147     return GNUNET_TIME_relative_get_forever ();
148   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
149   if (now.value > future.value)
150     return GNUNET_TIME_relative_get_zero ();
151   ret.value = future.value - now.value;
152   return ret;
153 }
154
155 /**
156  * Compute the time difference between the given start and end times.
157  * Use this function instead of actual subtraction to ensure that
158  * "FOREVER" and overflows are handeled correctly.
159  *
160  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
161  */
162 struct GNUNET_TIME_Relative
163 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
164                                      struct GNUNET_TIME_Absolute end)
165 {
166   struct GNUNET_TIME_Relative ret;
167   if (end.value == (uint64_t) - 1LL)
168     return GNUNET_TIME_relative_get_forever ();
169   if (end.value < start.value)
170     return GNUNET_TIME_relative_get_zero ();
171   ret.value = end.value - start.value;
172   return ret;
173 }
174
175 /**
176  * Get the duration of an operation as the
177  * difference of the current time and the given start time "hence".
178  *
179  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
180  */
181 struct GNUNET_TIME_Relative
182 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute hence)
183 {
184   struct GNUNET_TIME_Absolute now;
185   struct GNUNET_TIME_Relative ret;
186
187   now = GNUNET_TIME_absolute_get ();
188   GNUNET_assert (hence.value != (uint64_t) - 1LL);
189   if (hence.value > now.value)
190     return GNUNET_TIME_relative_get_zero ();
191   ret.value = now.value - hence.value;
192   return ret;
193 }
194
195
196 /**
197  * Add a given relative duration to the
198  * given start time.
199  *
200  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
201  */
202 struct GNUNET_TIME_Absolute
203 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
204                           struct GNUNET_TIME_Relative duration)
205 {
206   struct GNUNET_TIME_Absolute ret;
207
208   if ((start.value == (uint64_t) - 1LL) ||
209       (duration.value == (uint64_t) - 1LL))
210     return GNUNET_TIME_absolute_get_forever ();
211   if (start.value + duration.value < start.value)
212     {
213       GNUNET_break (0);
214       return GNUNET_TIME_absolute_get_forever ();
215     }
216   ret.value = start.value + duration.value;
217   return ret;
218 }
219
220 /**
221  * Multiply relative time by a given factor.
222  *
223  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
224  */
225 struct GNUNET_TIME_Relative
226 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
227                                unsigned int factor)
228 {
229   struct GNUNET_TIME_Relative ret;
230   if (factor == 0)
231     return GNUNET_TIME_relative_get_zero ();
232   ret.value = rel.value * (unsigned long long) factor;
233   if (ret.value / factor != rel.value)
234     {
235       GNUNET_break (0);
236       return GNUNET_TIME_relative_get_forever ();
237     }  
238   return ret;
239 }
240
241
242 /**
243  * Calculate the estimate time of arrival/completion 
244  * for an operation.
245  *
246  * @param start when did the operation start?
247  * @param finished how much has been done?
248  * @param total how much must be done overall (same unit as for "finished")
249  * @return remaining duration for the operation,
250  *        assuming it continues at the same speed
251  */
252 struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
253                                                        uint64_t finished,
254                                                        uint64_t total)
255 {
256   struct GNUNET_TIME_Relative dur;
257   double exp;
258   struct GNUNET_TIME_Relative ret;
259
260   GNUNET_break (finished > total);
261   if (finished >= total)
262     return GNUNET_TIME_UNIT_ZERO;
263   if (finished == 0)
264     return GNUNET_TIME_UNIT_FOREVER_REL;
265   dur = GNUNET_TIME_absolute_get_duration (start);
266   exp = ((double)dur.value) * ((double) total) / ((double)finished);
267   ret.value = ((uint64_t) exp) - dur.value;
268   return ret;
269 }
270
271
272 /**
273  * Add relative times together.
274  *
275  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
276  */
277 struct GNUNET_TIME_Relative
278 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
279                           struct GNUNET_TIME_Relative a2)
280 {
281   struct GNUNET_TIME_Relative ret;
282
283   if ((a1.value == (uint64_t) - 1LL) || (a2.value == (uint64_t) - 1LL))
284     return GNUNET_TIME_relative_get_forever ();
285   if (a1.value + a2.value < a1.value)
286     {
287       GNUNET_break (0);
288       return GNUNET_TIME_relative_get_forever ();
289     }
290   ret.value = a1.value + a2.value;
291   return ret;
292 }
293
294
295 /**
296  * Convert relative time to network byte order.
297  */
298 struct GNUNET_TIME_RelativeNBO
299 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
300 {
301   struct GNUNET_TIME_RelativeNBO ret;
302   ret.value__ = GNUNET_htonll (a.value);
303   return ret;
304 }
305
306 /**
307  * Convert relative time from network byte order.
308  */
309 struct GNUNET_TIME_Relative
310 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
311 {
312   struct GNUNET_TIME_Relative ret;
313   ret.value = GNUNET_ntohll (a.value__);
314   return ret;
315
316 }
317
318 /**
319  * Convert absolute time to network byte order.
320  */
321 struct GNUNET_TIME_AbsoluteNBO
322 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
323 {
324   struct GNUNET_TIME_AbsoluteNBO ret;
325   ret.value__ = GNUNET_htonll (a.value);
326   return ret;
327 }
328
329 /**
330  * Convert absolute time from network byte order.
331  */
332 struct GNUNET_TIME_Absolute
333 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
334 {
335   struct GNUNET_TIME_Absolute ret;
336   ret.value = GNUNET_ntohll (a.value__);
337   return ret;
338
339 }
340
341
342
343 /* end of time.c */