diff options
Diffstat (limited to 'src/lib/util/time.c')
-rw-r--r-- | src/lib/util/time.c | 991 |
1 files changed, 991 insertions, 0 deletions
diff --git a/src/lib/util/time.c b/src/lib/util/time.c new file mode 100644 index 000000000..84957c6a8 --- /dev/null +++ b/src/lib/util/time.c | |||
@@ -0,0 +1,991 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2001-2013, 2018 GNUnet e.V. | ||
4 | |||
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. | ||
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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file util/time.c | ||
23 | * @author Christian Grothoff | ||
24 | * @brief functions for handling time and time arithmetic | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #if __STDC_NO_ATOMICS__ | ||
30 | #define ATOMIC | ||
31 | #else | ||
32 | #ifdef HAVE_STDATOMIC_H | ||
33 | #include <stdatomic.h> | ||
34 | #define ATOMIC _Atomic | ||
35 | #else | ||
36 | #define __STDC_NO_ATOMICS__ 1 | ||
37 | #define ATOMIC | ||
38 | #endif | ||
39 | #endif | ||
40 | |||
41 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) | ||
42 | |||
43 | /** | ||
44 | * Variable used to simulate clock skew. Used for testing, never in production. | ||
45 | */ | ||
46 | static long long timestamp_offset; | ||
47 | |||
48 | void | ||
49 | GNUNET_TIME_set_offset (long long offset) | ||
50 | { | ||
51 | timestamp_offset = offset; | ||
52 | } | ||
53 | |||
54 | |||
55 | long long | ||
56 | GNUNET_TIME_get_offset () | ||
57 | { | ||
58 | return timestamp_offset; | ||
59 | } | ||
60 | |||
61 | |||
62 | bool | ||
63 | GNUNET_TIME_absolute_approx_eq (struct GNUNET_TIME_Absolute a1, | ||
64 | struct GNUNET_TIME_Absolute a2, | ||
65 | struct GNUNET_TIME_Relative t) | ||
66 | { | ||
67 | struct GNUNET_TIME_Relative delta; | ||
68 | |||
69 | delta = GNUNET_TIME_relative_min ( | ||
70 | GNUNET_TIME_absolute_get_difference (a1, a2), | ||
71 | GNUNET_TIME_absolute_get_difference (a2, a1)); | ||
72 | return GNUNET_TIME_relative_cmp (delta, | ||
73 | <=, | ||
74 | t); | ||
75 | } | ||
76 | |||
77 | |||
78 | struct GNUNET_TIME_Timestamp | ||
79 | GNUNET_TIME_absolute_to_timestamp (struct GNUNET_TIME_Absolute at) | ||
80 | { | ||
81 | struct GNUNET_TIME_Timestamp ts; | ||
82 | |||
83 | if (GNUNET_TIME_absolute_is_never (at)) | ||
84 | return GNUNET_TIME_UNIT_FOREVER_TS; | ||
85 | ts.abs_time.abs_value_us = at.abs_value_us - at.abs_value_us % 1000000; | ||
86 | return ts; | ||
87 | } | ||
88 | |||
89 | |||
90 | struct GNUNET_TIME_TimestampNBO | ||
91 | GNUNET_TIME_timestamp_hton (struct GNUNET_TIME_Timestamp t) | ||
92 | { | ||
93 | struct GNUNET_TIME_TimestampNBO tn; | ||
94 | |||
95 | tn.abs_time_nbo = GNUNET_TIME_absolute_hton (t.abs_time); | ||
96 | return tn; | ||
97 | } | ||
98 | |||
99 | |||
100 | struct GNUNET_TIME_Timestamp | ||
101 | GNUNET_TIME_timestamp_ntoh (struct GNUNET_TIME_TimestampNBO tn) | ||
102 | { | ||
103 | struct GNUNET_TIME_Timestamp t; | ||
104 | |||
105 | t.abs_time = GNUNET_TIME_absolute_ntoh (tn.abs_time_nbo); | ||
106 | return t; | ||
107 | } | ||
108 | |||
109 | |||
110 | struct GNUNET_TIME_Absolute | ||
111 | GNUNET_TIME_absolute_get () | ||
112 | { | ||
113 | struct GNUNET_TIME_Absolute ret; | ||
114 | struct timeval tv; | ||
115 | |||
116 | gettimeofday (&tv, NULL); | ||
117 | ret.abs_value_us = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) | ||
118 | + ((uint64_t) tv.tv_usec)) | ||
119 | + timestamp_offset; | ||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | |||
124 | struct GNUNET_TIME_Timestamp | ||
125 | GNUNET_TIME_timestamp_get () | ||
126 | { | ||
127 | return GNUNET_TIME_absolute_to_timestamp ( | ||
128 | GNUNET_TIME_absolute_get ()); | ||
129 | } | ||
130 | |||
131 | |||
132 | struct GNUNET_TIME_Relative | ||
133 | GNUNET_TIME_relative_get_zero_ () | ||
134 | { | ||
135 | static struct GNUNET_TIME_Relative zero; | ||
136 | |||
137 | return zero; | ||
138 | } | ||
139 | |||
140 | |||
141 | struct GNUNET_TIME_Absolute | ||
142 | GNUNET_TIME_absolute_get_zero_ () | ||
143 | { | ||
144 | static struct GNUNET_TIME_Absolute zero; | ||
145 | |||
146 | return zero; | ||
147 | } | ||
148 | |||
149 | |||
150 | struct GNUNET_TIME_Relative | ||
151 | GNUNET_TIME_relative_get_unit_ () | ||
152 | { | ||
153 | static struct GNUNET_TIME_Relative one = { 1 }; | ||
154 | |||
155 | return one; | ||
156 | } | ||
157 | |||
158 | |||
159 | struct GNUNET_TIME_Relative | ||
160 | GNUNET_TIME_relative_get_millisecond_ () | ||
161 | { | ||
162 | static struct GNUNET_TIME_Relative one = { 1000 }; | ||
163 | |||
164 | return one; | ||
165 | } | ||
166 | |||
167 | |||
168 | struct GNUNET_TIME_Relative | ||
169 | GNUNET_TIME_relative_get_second_ () | ||
170 | { | ||
171 | static struct GNUNET_TIME_Relative one = { 1000 * 1000LL }; | ||
172 | |||
173 | return one; | ||
174 | } | ||
175 | |||
176 | |||
177 | struct GNUNET_TIME_Relative | ||
178 | GNUNET_TIME_relative_get_minute_ () | ||
179 | { | ||
180 | static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL }; | ||
181 | |||
182 | return one; | ||
183 | } | ||
184 | |||
185 | |||
186 | struct GNUNET_TIME_Relative | ||
187 | GNUNET_TIME_relative_get_hour_ () | ||
188 | { | ||
189 | static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL }; | ||
190 | |||
191 | return one; | ||
192 | } | ||
193 | |||
194 | |||
195 | struct GNUNET_TIME_Relative | ||
196 | GNUNET_TIME_relative_get_forever_ () | ||
197 | { | ||
198 | static struct GNUNET_TIME_Relative forever = { UINT64_MAX }; | ||
199 | |||
200 | return forever; | ||
201 | } | ||
202 | |||
203 | |||
204 | struct GNUNET_TIME_Absolute | ||
205 | GNUNET_TIME_absolute_get_forever_ () | ||
206 | { | ||
207 | static struct GNUNET_TIME_Absolute forever = { UINT64_MAX }; | ||
208 | |||
209 | return forever; | ||
210 | } | ||
211 | |||
212 | |||
213 | const char * | ||
214 | GNUNET_TIME_timestamp2s (struct GNUNET_TIME_Timestamp ts) | ||
215 | { | ||
216 | static GNUNET_THREAD_LOCAL char buf[255]; | ||
217 | time_t tt; | ||
218 | struct tm *tp; | ||
219 | |||
220 | if (GNUNET_TIME_absolute_is_never (ts.abs_time)) | ||
221 | return "end of time"; | ||
222 | tt = ts.abs_time.abs_value_us / 1000LL / 1000LL; | ||
223 | tp = localtime (&tt); | ||
224 | /* This is hacky, but i don't know a way to detect libc character encoding. | ||
225 | * Just expect utf8 from glibc these days. | ||
226 | * As for msvcrt, use the wide variant, which always returns utf16 | ||
227 | * (otherwise we'd have to detect current codepage or use W32API character | ||
228 | * set conversion routines to convert to UTF8). | ||
229 | */ | ||
230 | strftime (buf, | ||
231 | sizeof(buf), | ||
232 | "%a %b %d %H:%M:%S %Y", | ||
233 | tp); | ||
234 | return buf; | ||
235 | } | ||
236 | |||
237 | |||
238 | const char * | ||
239 | GNUNET_TIME_absolute2s (struct GNUNET_TIME_Absolute t) | ||
240 | { | ||
241 | static GNUNET_THREAD_LOCAL char buf[255]; | ||
242 | time_t tt; | ||
243 | struct tm *tp; | ||
244 | |||
245 | if (GNUNET_TIME_absolute_is_never (t)) | ||
246 | return "end of time"; | ||
247 | tt = t.abs_value_us / 1000LL / 1000LL; | ||
248 | tp = localtime (&tt); | ||
249 | /* This is hacky, but i don't know a way to detect libc character encoding. | ||
250 | * Just expect utf8 from glibc these days. | ||
251 | * As for msvcrt, use the wide variant, which always returns utf16 | ||
252 | * (otherwise we'd have to detect current codepage or use W32API character | ||
253 | * set conversion routines to convert to UTF8). | ||
254 | */ | ||
255 | strftime (buf, | ||
256 | sizeof(buf), | ||
257 | "%a %b %d %H:%M:%S %Y", | ||
258 | tp); | ||
259 | return buf; | ||
260 | } | ||
261 | |||
262 | |||
263 | const char * | ||
264 | GNUNET_TIME_relative2s (struct GNUNET_TIME_Relative delta, | ||
265 | bool do_round) | ||
266 | { | ||
267 | static GNUNET_THREAD_LOCAL char buf[128]; | ||
268 | const char *unit = /* time unit */ "µs"; | ||
269 | uint64_t dval = delta.rel_value_us; | ||
270 | |||
271 | if (GNUNET_TIME_relative_is_forever (delta)) | ||
272 | return "forever"; | ||
273 | if (0 == delta.rel_value_us) | ||
274 | return "0 ms"; | ||
275 | if ( ((GNUNET_YES == do_round) && | ||
276 | (dval > 5 * 1000)) || | ||
277 | (0 == (dval % 1000))) | ||
278 | { | ||
279 | dval = dval / 1000; | ||
280 | unit = /* time unit */ "ms"; | ||
281 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) | ||
282 | { | ||
283 | dval = dval / 1000; | ||
284 | unit = /* time unit */ "s"; | ||
285 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | ||
286 | { | ||
287 | dval = dval / 60; | ||
288 | unit = /* time unit */ "m"; | ||
289 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | ||
290 | { | ||
291 | dval = dval / 60; | ||
292 | unit = /* time unit */ "h"; | ||
293 | if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || | ||
294 | (0 == (dval % 24))) | ||
295 | { | ||
296 | dval = dval / 24; | ||
297 | if (1 == dval) | ||
298 | unit = /* time unit */ "day"; | ||
299 | else | ||
300 | unit = /* time unit */ "days"; | ||
301 | } | ||
302 | } | ||
303 | } | ||
304 | } | ||
305 | } | ||
306 | GNUNET_snprintf (buf, | ||
307 | sizeof(buf), | ||
308 | "%llu %s", | ||
309 | (unsigned long long) dval, | ||
310 | unit); | ||
311 | return buf; | ||
312 | } | ||
313 | |||
314 | |||
315 | struct GNUNET_TIME_Absolute | ||
316 | GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel) | ||
317 | { | ||
318 | struct GNUNET_TIME_Absolute ret; | ||
319 | |||
320 | if (GNUNET_TIME_relative_is_forever (rel)) | ||
321 | return GNUNET_TIME_UNIT_FOREVER_ABS; | ||
322 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); | ||
323 | |||
324 | if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us) | ||
325 | { | ||
326 | GNUNET_break (0); /* overflow... */ | ||
327 | return GNUNET_TIME_UNIT_FOREVER_ABS; | ||
328 | } | ||
329 | ret.abs_value_us = rel.rel_value_us + now.abs_value_us; | ||
330 | return ret; | ||
331 | } | ||
332 | |||
333 | |||
334 | struct GNUNET_TIME_Timestamp | ||
335 | GNUNET_TIME_relative_to_timestamp (struct GNUNET_TIME_Relative rel) | ||
336 | { | ||
337 | return GNUNET_TIME_absolute_to_timestamp ( | ||
338 | GNUNET_TIME_relative_to_absolute (rel)); | ||
339 | } | ||
340 | |||
341 | |||
342 | struct GNUNET_TIME_Relative | ||
343 | GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1, | ||
344 | struct GNUNET_TIME_Relative t2) | ||
345 | { | ||
346 | return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2; | ||
347 | } | ||
348 | |||
349 | |||
350 | struct GNUNET_TIME_Relative | ||
351 | GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1, | ||
352 | struct GNUNET_TIME_Relative t2) | ||
353 | { | ||
354 | return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2; | ||
355 | } | ||
356 | |||
357 | |||
358 | struct GNUNET_TIME_Absolute | ||
359 | GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1, | ||
360 | struct GNUNET_TIME_Absolute t2) | ||
361 | { | ||
362 | return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2; | ||
363 | } | ||
364 | |||
365 | |||
366 | struct GNUNET_TIME_Absolute | ||
367 | GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1, | ||
368 | struct GNUNET_TIME_Absolute t2) | ||
369 | { | ||
370 | return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2; | ||
371 | } | ||
372 | |||
373 | |||
374 | struct GNUNET_TIME_Timestamp | ||
375 | GNUNET_TIME_timestamp_max (struct GNUNET_TIME_Timestamp t1, | ||
376 | struct GNUNET_TIME_Timestamp t2) | ||
377 | { | ||
378 | return (t1.abs_time.abs_value_us > t2.abs_time.abs_value_us) ? t1 : t2; | ||
379 | } | ||
380 | |||
381 | |||
382 | struct GNUNET_TIME_Timestamp | ||
383 | GNUNET_TIME_timestamp_min (struct GNUNET_TIME_Timestamp t1, | ||
384 | struct GNUNET_TIME_Timestamp t2) | ||
385 | { | ||
386 | return (t1.abs_time.abs_value_us < t2.abs_time.abs_value_us) ? t1 : t2; | ||
387 | } | ||
388 | |||
389 | |||
390 | struct GNUNET_TIME_Absolute | ||
391 | GNUNET_TIME_absolute_round_down (struct GNUNET_TIME_Absolute at, | ||
392 | struct GNUNET_TIME_Relative rt) | ||
393 | { | ||
394 | struct GNUNET_TIME_Absolute ret; | ||
395 | |||
396 | GNUNET_assert (! GNUNET_TIME_relative_is_zero (rt)); | ||
397 | ret.abs_value_us | ||
398 | = at.abs_value_us | ||
399 | - at.abs_value_us % rt.rel_value_us; | ||
400 | return ret; | ||
401 | } | ||
402 | |||
403 | |||
404 | struct GNUNET_TIME_Relative | ||
405 | GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future) | ||
406 | { | ||
407 | struct GNUNET_TIME_Relative ret; | ||
408 | |||
409 | if (GNUNET_TIME_absolute_is_never (future)) | ||
410 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
411 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); | ||
412 | |||
413 | if (now.abs_value_us > future.abs_value_us) | ||
414 | return GNUNET_TIME_UNIT_ZERO; | ||
415 | ret.rel_value_us = future.abs_value_us - now.abs_value_us; | ||
416 | return ret; | ||
417 | } | ||
418 | |||
419 | |||
420 | struct GNUNET_TIME_Relative | ||
421 | GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start, | ||
422 | struct GNUNET_TIME_Absolute end) | ||
423 | { | ||
424 | struct GNUNET_TIME_Relative ret; | ||
425 | |||
426 | if (GNUNET_TIME_absolute_is_never (end)) | ||
427 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
428 | if (end.abs_value_us < start.abs_value_us) | ||
429 | return GNUNET_TIME_UNIT_ZERO; | ||
430 | ret.rel_value_us = end.abs_value_us - start.abs_value_us; | ||
431 | return ret; | ||
432 | } | ||
433 | |||
434 | |||
435 | struct GNUNET_TIME_Relative | ||
436 | GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence) | ||
437 | { | ||
438 | struct GNUNET_TIME_Absolute now; | ||
439 | struct GNUNET_TIME_Relative ret; | ||
440 | |||
441 | now = GNUNET_TIME_absolute_get (); | ||
442 | if (whence.abs_value_us > now.abs_value_us) | ||
443 | return GNUNET_TIME_UNIT_ZERO; | ||
444 | ret.rel_value_us = now.abs_value_us - whence.abs_value_us; | ||
445 | return ret; | ||
446 | } | ||
447 | |||
448 | |||
449 | struct GNUNET_TIME_Absolute | ||
450 | GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start, | ||
451 | struct GNUNET_TIME_Relative duration) | ||
452 | { | ||
453 | struct GNUNET_TIME_Absolute ret; | ||
454 | |||
455 | if (GNUNET_TIME_absolute_is_never (start) || | ||
456 | GNUNET_TIME_relative_is_forever (duration)) | ||
457 | return GNUNET_TIME_UNIT_FOREVER_ABS; | ||
458 | if (start.abs_value_us + duration.rel_value_us < start.abs_value_us) | ||
459 | { | ||
460 | GNUNET_break (0); | ||
461 | return GNUNET_TIME_UNIT_FOREVER_ABS; | ||
462 | } | ||
463 | ret.abs_value_us = start.abs_value_us + duration.rel_value_us; | ||
464 | return ret; | ||
465 | } | ||
466 | |||
467 | |||
468 | struct GNUNET_TIME_Absolute | ||
469 | GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start, | ||
470 | struct GNUNET_TIME_Relative duration) | ||
471 | { | ||
472 | struct GNUNET_TIME_Absolute ret; | ||
473 | |||
474 | if (start.abs_value_us <= duration.rel_value_us) | ||
475 | return GNUNET_TIME_UNIT_ZERO_ABS; | ||
476 | if (GNUNET_TIME_absolute_is_never (start)) | ||
477 | return GNUNET_TIME_UNIT_FOREVER_ABS; | ||
478 | ret.abs_value_us = start.abs_value_us - duration.rel_value_us; | ||
479 | return ret; | ||
480 | } | ||
481 | |||
482 | |||
483 | struct GNUNET_TIME_Relative | ||
484 | GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel, | ||
485 | unsigned long long factor) | ||
486 | { | ||
487 | struct GNUNET_TIME_Relative ret; | ||
488 | |||
489 | if (0 == factor) | ||
490 | return GNUNET_TIME_UNIT_ZERO; | ||
491 | if (GNUNET_TIME_relative_is_forever (rel)) | ||
492 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
493 | ret.rel_value_us = rel.rel_value_us * factor; | ||
494 | if (ret.rel_value_us / factor != rel.rel_value_us) | ||
495 | { | ||
496 | GNUNET_break (0); | ||
497 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
498 | } | ||
499 | return ret; | ||
500 | } | ||
501 | |||
502 | |||
503 | struct GNUNET_TIME_Relative | ||
504 | relative_multiply_double (struct GNUNET_TIME_Relative rel, | ||
505 | double factor) | ||
506 | { | ||
507 | struct GNUNET_TIME_Relative out; | ||
508 | double m; | ||
509 | |||
510 | GNUNET_assert (0 <= factor); | ||
511 | |||
512 | if (0 == factor) | ||
513 | return GNUNET_TIME_UNIT_ZERO; | ||
514 | if (GNUNET_TIME_relative_is_forever (rel)) | ||
515 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
516 | |||
517 | m = ((double) rel.rel_value_us) * factor; | ||
518 | |||
519 | if (m >= (double) (GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us) | ||
520 | { | ||
521 | GNUNET_break (0); | ||
522 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
523 | } | ||
524 | |||
525 | out.rel_value_us = (uint64_t) m; | ||
526 | return out; | ||
527 | } | ||
528 | |||
529 | |||
530 | struct GNUNET_TIME_Relative | ||
531 | GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel, | ||
532 | unsigned long long factor) | ||
533 | { | ||
534 | struct GNUNET_TIME_Relative ret; | ||
535 | |||
536 | if (0 == factor) | ||
537 | return GNUNET_TIME_UNIT_ZERO; | ||
538 | if (GNUNET_TIME_relative_is_forever (rel)) | ||
539 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
540 | ret.rel_value_us = rel.rel_value_us * factor; | ||
541 | if (ret.rel_value_us / factor != rel.rel_value_us) | ||
542 | { | ||
543 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
544 | } | ||
545 | return ret; | ||
546 | } | ||
547 | |||
548 | |||
549 | struct GNUNET_TIME_Relative | ||
550 | GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel, | ||
551 | unsigned long long factor) | ||
552 | { | ||
553 | struct GNUNET_TIME_Relative ret; | ||
554 | |||
555 | if ((0 == factor) || | ||
556 | (GNUNET_TIME_relative_is_forever (rel))) | ||
557 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
558 | ret.rel_value_us = rel.rel_value_us / factor; | ||
559 | return ret; | ||
560 | } | ||
561 | |||
562 | |||
563 | struct GNUNET_TIME_Relative | ||
564 | GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, | ||
565 | uint64_t finished, | ||
566 | uint64_t total) | ||
567 | { | ||
568 | struct GNUNET_TIME_Relative due; | ||
569 | double exp; | ||
570 | struct GNUNET_TIME_Relative ret; | ||
571 | |||
572 | GNUNET_break (finished <= total); | ||
573 | if (finished >= total) | ||
574 | return GNUNET_TIME_UNIT_ZERO; | ||
575 | if (0 == finished) | ||
576 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
577 | due = GNUNET_TIME_absolute_get_duration (start); | ||
578 | exp = ((double) due.rel_value_us) * ((double) total) / ((double) finished); | ||
579 | ret.rel_value_us = ((uint64_t) exp) - due.rel_value_us; | ||
580 | return ret; | ||
581 | } | ||
582 | |||
583 | |||
584 | struct GNUNET_TIME_Relative | ||
585 | GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1, | ||
586 | struct GNUNET_TIME_Relative a2) | ||
587 | { | ||
588 | struct GNUNET_TIME_Relative ret; | ||
589 | |||
590 | if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX)) | ||
591 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
592 | if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us) | ||
593 | { | ||
594 | GNUNET_break (0); | ||
595 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
596 | } | ||
597 | ret.rel_value_us = a1.rel_value_us + a2.rel_value_us; | ||
598 | return ret; | ||
599 | } | ||
600 | |||
601 | |||
602 | struct GNUNET_TIME_Relative | ||
603 | GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1, | ||
604 | struct GNUNET_TIME_Relative a2) | ||
605 | { | ||
606 | struct GNUNET_TIME_Relative ret; | ||
607 | |||
608 | if (a2.rel_value_us >= a1.rel_value_us) | ||
609 | return GNUNET_TIME_UNIT_ZERO; | ||
610 | if (a1.rel_value_us == UINT64_MAX) | ||
611 | return GNUNET_TIME_UNIT_FOREVER_REL; | ||
612 | ret.rel_value_us = a1.rel_value_us - a2.rel_value_us; | ||
613 | return ret; | ||
614 | } | ||
615 | |||
616 | |||
617 | struct GNUNET_TIME_RelativeNBO | ||
618 | GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a) | ||
619 | { | ||
620 | struct GNUNET_TIME_RelativeNBO ret; | ||
621 | |||
622 | ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us); | ||
623 | return ret; | ||
624 | } | ||
625 | |||
626 | |||
627 | struct GNUNET_TIME_Relative | ||
628 | GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a) | ||
629 | { | ||
630 | struct GNUNET_TIME_Relative ret; | ||
631 | |||
632 | ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__); | ||
633 | return ret; | ||
634 | } | ||
635 | |||
636 | |||
637 | struct GNUNET_TIME_AbsoluteNBO | ||
638 | GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a) | ||
639 | { | ||
640 | struct GNUNET_TIME_AbsoluteNBO ret; | ||
641 | |||
642 | ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us); | ||
643 | return ret; | ||
644 | } | ||
645 | |||
646 | |||
647 | bool | ||
648 | GNUNET_TIME_absolute_is_never (struct GNUNET_TIME_Absolute abs) | ||
649 | { | ||
650 | return GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == abs.abs_value_us; | ||
651 | } | ||
652 | |||
653 | |||
654 | bool | ||
655 | GNUNET_TIME_relative_is_forever (struct GNUNET_TIME_Relative rel) | ||
656 | { | ||
657 | return GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == rel.rel_value_us; | ||
658 | } | ||
659 | |||
660 | |||
661 | bool | ||
662 | GNUNET_TIME_relative_is_zero (struct GNUNET_TIME_Relative rel) | ||
663 | { | ||
664 | return 0 == rel.rel_value_us; | ||
665 | } | ||
666 | |||
667 | |||
668 | bool | ||
669 | GNUNET_TIME_absolute_is_past (struct GNUNET_TIME_Absolute abs) | ||
670 | { | ||
671 | struct GNUNET_TIME_Absolute now; | ||
672 | |||
673 | now = GNUNET_TIME_absolute_get (); | ||
674 | return abs.abs_value_us < now.abs_value_us; | ||
675 | } | ||
676 | |||
677 | |||
678 | bool | ||
679 | GNUNET_TIME_absolute_is_future (struct GNUNET_TIME_Absolute abs) | ||
680 | { | ||
681 | struct GNUNET_TIME_Absolute now; | ||
682 | |||
683 | now = GNUNET_TIME_absolute_get (); | ||
684 | return abs.abs_value_us > now.abs_value_us; | ||
685 | } | ||
686 | |||
687 | |||
688 | struct GNUNET_TIME_Absolute | ||
689 | GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch) | ||
690 | { | ||
691 | struct GNUNET_TIME_Absolute ret; | ||
692 | |||
693 | ret.abs_value_us = GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us | ||
694 | * ms_after_epoch; | ||
695 | if (ret.abs_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us != | ||
696 | ms_after_epoch) | ||
697 | ret = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
698 | return ret; | ||
699 | } | ||
700 | |||
701 | |||
702 | struct GNUNET_TIME_Absolute | ||
703 | GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch) | ||
704 | { | ||
705 | struct GNUNET_TIME_Absolute ret; | ||
706 | |||
707 | ret.abs_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us * s_after_epoch; | ||
708 | if (ret.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us != | ||
709 | s_after_epoch) | ||
710 | ret = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
711 | return ret; | ||
712 | } | ||
713 | |||
714 | |||
715 | struct GNUNET_TIME_Timestamp | ||
716 | GNUNET_TIME_timestamp_from_s (uint64_t s_after_epoch) | ||
717 | { | ||
718 | struct GNUNET_TIME_Timestamp ret; | ||
719 | |||
720 | ret.abs_time.abs_value_us | ||
721 | = GNUNET_TIME_UNIT_SECONDS.rel_value_us * s_after_epoch; | ||
722 | if (ret.abs_time.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us | ||
723 | != s_after_epoch) | ||
724 | ret = GNUNET_TIME_UNIT_FOREVER_TS; | ||
725 | return ret; | ||
726 | } | ||
727 | |||
728 | |||
729 | uint64_t | ||
730 | GNUNET_TIME_timestamp_to_s (struct GNUNET_TIME_Timestamp ts) | ||
731 | { | ||
732 | return ts.abs_time.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us; | ||
733 | } | ||
734 | |||
735 | |||
736 | struct GNUNET_TIME_Absolute | ||
737 | GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a) | ||
738 | { | ||
739 | struct GNUNET_TIME_Absolute ret; | ||
740 | |||
741 | ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__); | ||
742 | return ret; | ||
743 | } | ||
744 | |||
745 | |||
746 | unsigned int | ||
747 | GNUNET_TIME_get_current_year () | ||
748 | { | ||
749 | time_t tp; | ||
750 | struct tm *t; | ||
751 | |||
752 | tp = time (NULL); | ||
753 | t = gmtime (&tp); | ||
754 | if (t == NULL) | ||
755 | return 0; | ||
756 | return t->tm_year + 1900; | ||
757 | } | ||
758 | |||
759 | |||
760 | unsigned int | ||
761 | GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at) | ||
762 | { | ||
763 | struct tm *t; | ||
764 | time_t tp; | ||
765 | |||
766 | tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */ | ||
767 | t = gmtime (&tp); | ||
768 | if (t == NULL) | ||
769 | return 0; | ||
770 | return t->tm_year + 1900; | ||
771 | } | ||
772 | |||
773 | |||
774 | #ifndef HAVE_TIMEGM | ||
775 | /** | ||
776 | * As suggested in the timegm() man page. | ||
777 | */ | ||
778 | static time_t | ||
779 | my_timegm (struct tm *tm) | ||
780 | { | ||
781 | time_t ret; | ||
782 | char *tz; | ||
783 | |||
784 | tz = getenv ("TZ"); | ||
785 | setenv ("TZ", "", 1); | ||
786 | tzset (); | ||
787 | ret = mktime (tm); | ||
788 | if (tz) | ||
789 | setenv ("TZ", tz, 1); | ||
790 | else | ||
791 | unsetenv ("TZ"); | ||
792 | tzset (); | ||
793 | return ret; | ||
794 | } | ||
795 | |||
796 | |||
797 | #endif | ||
798 | |||
799 | |||
800 | struct GNUNET_TIME_Absolute | ||
801 | GNUNET_TIME_year_to_time (unsigned int year) | ||
802 | { | ||
803 | struct GNUNET_TIME_Absolute ret; | ||
804 | time_t tp; | ||
805 | struct tm t; | ||
806 | |||
807 | memset (&t, 0, sizeof(t)); | ||
808 | if (year < 1900) | ||
809 | { | ||
810 | GNUNET_break (0); | ||
811 | return GNUNET_TIME_absolute_get (); /* now */ | ||
812 | } | ||
813 | t.tm_year = year - 1900; | ||
814 | t.tm_mday = 1; | ||
815 | t.tm_mon = 0; | ||
816 | t.tm_wday = 1; | ||
817 | t.tm_yday = 1; | ||
818 | #ifndef HAVE_TIMEGM | ||
819 | tp = my_timegm (&t); | ||
820 | #else | ||
821 | tp = timegm (&t); | ||
822 | #endif | ||
823 | GNUNET_break (tp != (time_t) -1); | ||
824 | ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */ | ||
825 | return ret; | ||
826 | } | ||
827 | |||
828 | |||
829 | struct GNUNET_TIME_Relative | ||
830 | GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt, | ||
831 | struct GNUNET_TIME_Relative threshold) | ||
832 | { | ||
833 | double r = (rand () % 500) / 1000.0; | ||
834 | struct GNUNET_TIME_Relative t; | ||
835 | |||
836 | t = relative_multiply_double ( | ||
837 | GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, rt), | ||
838 | 2 + r); | ||
839 | return GNUNET_TIME_relative_min (threshold, t); | ||
840 | } | ||
841 | |||
842 | |||
843 | bool | ||
844 | GNUNET_TIME_absolute_is_zero (struct GNUNET_TIME_Absolute abs) | ||
845 | { | ||
846 | return 0 == abs.abs_value_us; | ||
847 | } | ||
848 | |||
849 | |||
850 | struct GNUNET_TIME_Relative | ||
851 | GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r) | ||
852 | { | ||
853 | double d = ((rand () % 1001) + 500) / 1000.0; | ||
854 | |||
855 | return relative_multiply_double (r, d); | ||
856 | } | ||
857 | |||
858 | |||
859 | struct GNUNET_TIME_Absolute | ||
860 | GNUNET_TIME_absolute_get_monotonic ( | ||
861 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
862 | { | ||
863 | static const struct GNUNET_CONFIGURATION_Handle *last_cfg; | ||
864 | static struct GNUNET_TIME_Absolute last_time; | ||
865 | static struct GNUNET_DISK_MapHandle *map_handle; | ||
866 | static ATOMIC volatile uint64_t *map; | ||
867 | struct GNUNET_TIME_Absolute now; | ||
868 | |||
869 | now = GNUNET_TIME_absolute_get (); | ||
870 | if (last_cfg != cfg) | ||
871 | { | ||
872 | char *filename; | ||
873 | |||
874 | if (NULL != map_handle) | ||
875 | { | ||
876 | GNUNET_DISK_file_unmap (map_handle); | ||
877 | map_handle = NULL; | ||
878 | } | ||
879 | map = NULL; | ||
880 | |||
881 | last_cfg = cfg; | ||
882 | if ((NULL != cfg) && | ||
883 | (GNUNET_OK == | ||
884 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
885 | "util", | ||
886 | "MONOTONIC_TIME_FILENAME", | ||
887 | &filename))) | ||
888 | { | ||
889 | struct GNUNET_DISK_FileHandle *fh; | ||
890 | |||
891 | fh = GNUNET_DISK_file_open (filename, | ||
892 | GNUNET_DISK_OPEN_READWRITE | ||
893 | | GNUNET_DISK_OPEN_CREATE, | ||
894 | GNUNET_DISK_PERM_USER_WRITE | ||
895 | | GNUNET_DISK_PERM_GROUP_WRITE | ||
896 | | GNUNET_DISK_PERM_USER_READ | ||
897 | | GNUNET_DISK_PERM_GROUP_READ); | ||
898 | if (NULL == fh) | ||
899 | { | ||
900 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
901 | _ ("Failed to map `%s', cannot assure monotonic time!\n"), | ||
902 | filename); | ||
903 | } | ||
904 | else | ||
905 | { | ||
906 | off_t size; | ||
907 | |||
908 | size = 0; | ||
909 | GNUNET_break (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &size)); | ||
910 | if (size < (off_t) sizeof(*map)) | ||
911 | { | ||
912 | struct GNUNET_TIME_AbsoluteNBO o; | ||
913 | |||
914 | o = GNUNET_TIME_absolute_hton (now); | ||
915 | if (sizeof(o) != GNUNET_DISK_file_write (fh, &o, sizeof(o))) | ||
916 | size = 0; | ||
917 | else | ||
918 | size = sizeof(o); | ||
919 | } | ||
920 | if (size == sizeof(*map)) | ||
921 | { | ||
922 | map = GNUNET_DISK_file_map (fh, | ||
923 | &map_handle, | ||
924 | GNUNET_DISK_MAP_TYPE_READWRITE, | ||
925 | sizeof(*map)); | ||
926 | if (NULL == map) | ||
927 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
928 | _ ( | ||
929 | "Failed to map `%s', cannot assure monotonic time!\n"), | ||
930 | filename); | ||
931 | } | ||
932 | else | ||
933 | { | ||
934 | GNUNET_log ( | ||
935 | GNUNET_ERROR_TYPE_WARNING, | ||
936 | _ ( | ||
937 | "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"), | ||
938 | filename); | ||
939 | } | ||
940 | } | ||
941 | GNUNET_DISK_file_close (fh); | ||
942 | GNUNET_free (filename); | ||
943 | } | ||
944 | } | ||
945 | if (NULL != map) | ||
946 | { | ||
947 | struct GNUNET_TIME_AbsoluteNBO mt; | ||
948 | |||
949 | #if __STDC_NO_ATOMICS__ | ||
950 | #if __GNUC__ | ||
951 | mt.abs_value_us__ = __sync_fetch_and_or (map, 0); | ||
952 | #else | ||
953 | mt.abs_value_us__ = *map; /* godspeed, pray this is atomic */ | ||
954 | #endif | ||
955 | #else | ||
956 | mt.abs_value_us__ = atomic_load (map); | ||
957 | #endif | ||
958 | last_time = | ||
959 | GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), last_time); | ||
960 | } | ||
961 | if (now.abs_value_us <= last_time.abs_value_us) | ||
962 | now.abs_value_us = last_time.abs_value_us + 1; | ||
963 | last_time = now; | ||
964 | if (NULL != map) | ||
965 | { | ||
966 | uint64_t val = GNUNET_TIME_absolute_hton (now).abs_value_us__; | ||
967 | #if __STDC_NO_ATOMICS__ | ||
968 | #if __GNUC__ | ||
969 | (void) __sync_lock_test_and_set (map, val); | ||
970 | #else | ||
971 | *map = val; /* godspeed, pray this is atomic */ | ||
972 | #endif | ||
973 | #else | ||
974 | atomic_store (map, val); | ||
975 | #endif | ||
976 | } | ||
977 | return now; | ||
978 | } | ||
979 | |||
980 | |||
981 | /** | ||
982 | * Destructor | ||
983 | */ | ||
984 | void __attribute__ ((destructor)) | ||
985 | GNUNET_util_time_fini () | ||
986 | { | ||
987 | (void) GNUNET_TIME_absolute_get_monotonic (NULL); | ||
988 | } | ||
989 | |||
990 | |||
991 | /* end of time.c */ | ||