GNUnet 0.25.2-12-g8d1cef5f6
 
Loading...
Searching...
No Matches
time.c
Go to the documentation of this file.
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
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
46static long long timestamp_offset;
47
48void
49GNUNET_TIME_set_offset (long long offset)
50{
51 timestamp_offset = offset;
52}
53
54
55long long
60
61
62bool
76
77
80{
81 struct GNUNET_TIME_Timestamp ts;
82
85 ts.abs_time.abs_value_us = at.abs_value_us - at.abs_value_us % 1000000;
86 return ts;
87}
88
89
98
99
102{
104
105 t.abs_time = GNUNET_TIME_absolute_ntoh (tn.abs_time_nbo);
106 return t;
107}
108
109
112{
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))
120 return ret;
121}
122
123
130
131
134{
135 static struct GNUNET_TIME_Relative zero;
136
137 return zero;
138}
139
140
143{
144 static struct GNUNET_TIME_Absolute zero;
145
146 return zero;
147}
148
149
152{
153 static struct GNUNET_TIME_Relative one = { 1 };
154
155 return one;
156}
157
158
161{
162 static struct GNUNET_TIME_Relative one = { 1000 };
163
164 return one;
165}
166
167
170{
171 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
172
173 return one;
174}
175
176
179{
180 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
181
182 return one;
183}
184
185
188{
189 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
190
191 return one;
192}
193
194
197{
198 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
199
200 return forever;
201}
202
203
206{
207 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
208
209 return forever;
210}
211
212
213const char *
215{
216 static GNUNET_THREAD_LOCAL char buf[255];
217 time_t tt;
218 struct tm *tp;
219
221 return "never";
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
238const char *
240{
241 static GNUNET_THREAD_LOCAL char buf[255];
242 time_t tt;
243 struct tm *tp;
244
246 return "never";
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
263const char *
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
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
317{
319 struct GNUNET_TIME_Absolute now;
320
324
325 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
326 {
327 GNUNET_break (0); /* overflow... */
329 }
330 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
331 return ret;
332}
333
334
341
342
345 struct GNUNET_TIME_Relative t2)
346{
347 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
348}
349
350
353 struct GNUNET_TIME_Relative t2)
354{
355 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
356}
357
358
361 struct GNUNET_TIME_Absolute t2)
362{
363 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
364}
365
366
369 struct GNUNET_TIME_Absolute t2)
370{
371 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
372}
373
374
377 struct GNUNET_TIME_Timestamp t2)
378{
379 return (t1.abs_time.abs_value_us > t2.abs_time.abs_value_us) ? t1 : t2;
380}
381
382
385 struct GNUNET_TIME_Timestamp t2)
386{
387 return (t1.abs_time.abs_value_us < t2.abs_time.abs_value_us) ? t1 : t2;
388}
389
390
393 struct GNUNET_TIME_Relative rt)
394{
396
398 ret.abs_value_us
399 = at.abs_value_us
400 - at.abs_value_us % rt.rel_value_us;
401 return ret;
402}
403
404
407{
409 struct GNUNET_TIME_Absolute now;
410
414
415 if (now.abs_value_us > future.abs_value_us)
417 ret.rel_value_us = future.abs_value_us - now.abs_value_us;
418 return ret;
419}
420
421
425{
427
430 if (end.abs_value_us < start.abs_value_us)
432 ret.rel_value_us = end.abs_value_us - start.abs_value_us;
433 return ret;
434}
435
436
439{
440 struct GNUNET_TIME_Absolute now;
442
444 if (whence.abs_value_us > now.abs_value_us)
446 ret.rel_value_us = now.abs_value_us - whence.abs_value_us;
447 return ret;
448}
449
450
454{
456
460 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
461 {
462 GNUNET_break (0);
464 }
465 ret.abs_value_us = start.abs_value_us + duration.rel_value_us;
466 return ret;
467}
468
469
483
484
487 unsigned long long factor)
488{
490
491 if (0 == factor)
495 ret.rel_value_us = rel.rel_value_us * factor;
496 if (ret.rel_value_us / factor != rel.rel_value_us)
497 {
498 GNUNET_break (0);
500 }
501 return ret;
502}
503
504
507 double factor)
508{
509 struct GNUNET_TIME_Relative out;
510 double m;
511
512 GNUNET_assert (0.0 <= factor);
513 if (0.0 >= factor)
517 m = ((double) rel.rel_value_us) * factor;
519 {
520 GNUNET_break (0);
522 }
523 out.rel_value_us = (uint64_t) m;
524 return out;
525}
526
527
530 unsigned long long factor)
531{
533
534 if (0 == factor)
538 ret.rel_value_us = rel.rel_value_us * factor;
539 if (ret.rel_value_us / factor != rel.rel_value_us)
540 {
542 }
543 return ret;
544}
545
546
549 unsigned long long factor)
550{
552
553 if ((0 == factor) ||
556 ret.rel_value_us = rel.rel_value_us / factor;
557 return ret;
558}
559
560
563 uint64_t finished,
564 uint64_t total)
565{
566 struct GNUNET_TIME_Relative due;
567 double exp;
569
570 GNUNET_break (finished <= total);
571 if (finished >= total)
573 if (0 == finished)
576 exp = ((double) due.rel_value_us) * ((double) total) / ((double) finished);
577 ret.rel_value_us = ((uint64_t) exp) - due.rel_value_us;
578 return ret;
579}
580
581
584 struct GNUNET_TIME_Relative a2)
585{
587
588 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
590 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
591 {
592 GNUNET_break (0);
594 }
595 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
596 return ret;
597}
598
599
602 struct GNUNET_TIME_Relative a2)
603{
605
606 if (a2.rel_value_us >= a1.rel_value_us)
608 if (a1.rel_value_us == UINT64_MAX)
610 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
611 return ret;
612}
613
614
617{
619
620 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
621 return ret;
622}
623
624
627{
629
630 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
631 return ret;
632}
633
634
637{
639
640 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
641 return ret;
642}
643
644
645bool
650
651
652bool
657
658
659bool
661{
662 return 0 == rel.rel_value_us;
663}
664
665
666bool
668{
669 struct GNUNET_TIME_Absolute now;
670
672 return abs.abs_value_us < now.abs_value_us;
673}
674
675
676bool
678{
679 struct GNUNET_TIME_Absolute now;
680
682 return abs.abs_value_us > now.abs_value_us;
683}
684
685
687GNUNET_TIME_absolute_from_ms (uint64_t ms_after_epoch)
688{
690
692 * ms_after_epoch;
693 if (ret.abs_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us !=
694 ms_after_epoch)
696 return ret;
697}
698
699
701GNUNET_TIME_absolute_from_s (uint64_t s_after_epoch)
702{
704
705 ret.abs_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us * s_after_epoch;
706 if (ret.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us !=
707 s_after_epoch)
709 return ret;
710}
711
712
714GNUNET_TIME_timestamp_from_s (uint64_t s_after_epoch)
715{
717
719 = GNUNET_TIME_UNIT_SECONDS.rel_value_us * s_after_epoch;
720 if (ret.abs_time.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us
721 != s_after_epoch)
723 return ret;
724}
725
726
727uint64_t
729{
731 return UINT64_MAX;
732 return ts.abs_time.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
733}
734
735
738{
740
741 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
742 return ret;
743}
744
745
746unsigned int
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
760unsigned int
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
778static time_t
779my_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
801GNUNET_TIME_year_to_time (unsigned int year)
802{
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
831 struct GNUNET_TIME_Relative threshold)
832{
833 double r = (rand () % 500) / 1000.0;
834 struct GNUNET_TIME_Relative t;
835
838 2 + r);
839 return GNUNET_TIME_relative_min (threshold, t);
840}
841
842
843bool
845{
846 return 0 == abs.abs_value_us;
847}
848
849
852{
853 double d = ((rand () % 1001) + 500) / 1000.0;
854
856}
857
858
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
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 ==
885 "util",
886 "MONOTONIC_TIME_FILENAME",
887 &filename)))
888 {
889 struct GNUNET_DISK_FileHandle *fh;
890
898 if (NULL == fh)
899 {
901 _ ("Failed to map `%s', cannot assure monotonic time!\n"),
902 filename);
903 }
904 else
905 {
906 off_t size;
907
908 size = 0;
910 if (size < (off_t) sizeof(*map))
911 {
913
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 {
923 &map_handle,
925 sizeof(*map));
926 if (NULL == map)
928 _ (
929 "Failed to map `%s', cannot assure monotonic time!\n")
930 ,
931 filename);
932 }
933 else
934 {
935 GNUNET_log (
937 _ (
938 "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
939 filename);
940 }
941 }
944 }
945 }
946 if (NULL != map)
947 {
948 struct GNUNET_TIME_AbsoluteNBO mt;
949
950#if __STDC_NO_ATOMICS__
951#if __GNUC__
952 mt.abs_value_us__ = __sync_fetch_and_or (map, 0);
953#else
954 mt.abs_value_us__ = *map; /* godspeed, pray this is atomic */
955#endif
956#else
957 mt.abs_value_us__ = atomic_load (map);
958#endif
959 last_time =
961 }
962 if (now.abs_value_us <= last_time.abs_value_us)
963 now.abs_value_us = last_time.abs_value_us + 1;
964 last_time = now;
965 if (NULL != map)
966 {
967 uint64_t val = GNUNET_TIME_absolute_hton (now).abs_value_us__;
968#if __STDC_NO_ATOMICS__
969#if __GNUC__
970 (void) __sync_lock_test_and_set (map, val);
971#else
972 *map = val; /* godspeed, pray this is atomic */
973#endif
974#else
975 atomic_store (map, val);
976#endif
977 }
978 return now;
979}
980
981
985{
987 time_t seconds;
988 struct tm timeinfo;
989
991 return at;
992 if (GNUNET_TIME_RI_NONE == ri)
993 return at;
994 seconds = at.abs_value_us / 1000000;
995 if (NULL == localtime_r (&seconds,
996 &timeinfo))
997 {
998 GNUNET_break (0);
999 return at;
1000 }
1001 switch (ri)
1002 {
1004 /* eh, we did this above!? */
1005 GNUNET_break (0);
1006 return at;
1008 if (0 != at.abs_value_us % 1000000)
1009 result.abs_value_us = (seconds + 1) * 1000000ULL;
1010 else
1011 result = at;
1012 return result;
1014 if ( (timeinfo.tm_sec > 0) ||
1015 (0 != at.abs_value_us % 1000000) )
1016 {
1017 timeinfo.tm_sec = 0;
1018 timeinfo.tm_min += 1;
1019 }
1020 break;
1022 /* Round up to next hour */
1023 if ( (timeinfo.tm_min > 0) ||
1024 (timeinfo.tm_sec > 0) ||
1025 (0 != (at.abs_value_us % 1000000)) )
1026 {
1027 timeinfo.tm_sec = 0;
1028 timeinfo.tm_min = 0;
1029 timeinfo.tm_hour += 1;
1030 }
1031 break;
1032 case GNUNET_TIME_RI_DAY:
1033 if ( (timeinfo.tm_hour > 0) ||
1034 (timeinfo.tm_min > 0) ||
1035 (timeinfo.tm_sec > 0) ||
1036 (0 != at.abs_value_us % 1000000) )
1037 {
1038 timeinfo.tm_sec = 0;
1039 timeinfo.tm_min = 0;
1040 timeinfo.tm_hour = 0;
1041 timeinfo.tm_mday += 1;
1042 }
1043 break;
1045 /* Round up to next Monday (ISO 8601 week starts on Monday) */
1046 {
1047 /* tm_way == 0 => Sunday */
1048 int days_until_monday = (8 - timeinfo.tm_wday) % 7;
1049
1050 if ( (0 == days_until_monday) &&
1051 ( (timeinfo.tm_hour > 0) ||
1052 (timeinfo.tm_min > 0) ||
1053 (timeinfo.tm_sec > 0) ||
1054 (0 != at.abs_value_us % 1000000) ) )
1055 days_until_monday = 7;
1056 if (days_until_monday > 0)
1057 {
1058 timeinfo.tm_sec = 0;
1059 timeinfo.tm_min = 0;
1060 timeinfo.tm_hour = 0;
1061 timeinfo.tm_mday += days_until_monday;
1062 }
1063 }
1064 break;
1066 if ( (timeinfo.tm_mday > 1) ||
1067 (timeinfo.tm_hour > 0) ||
1068 (timeinfo.tm_min > 0) ||
1069 (timeinfo.tm_sec > 0) ||
1070 (0 != (at.abs_value_us % 1000000)) )
1071 {
1072 timeinfo.tm_sec = 0;
1073 timeinfo.tm_min = 0;
1074 timeinfo.tm_hour = 0;
1075 timeinfo.tm_mday = 1;
1076 timeinfo.tm_mon += 1;
1077 }
1078 break;
1080 {
1081 int next_quarter_month = ((timeinfo.tm_mon / 3) + 1) * 3;
1082
1083 if ( (next_quarter_month > timeinfo.tm_mon) ||
1084 (timeinfo.tm_mday > 1) ||
1085 (timeinfo.tm_hour > 0) ||
1086 (timeinfo.tm_min > 0) ||
1087 (timeinfo.tm_sec > 0) ||
1088 (0 != (at.abs_value_us % 1000000)) )
1089 {
1090 timeinfo.tm_sec = 0;
1091 timeinfo.tm_min = 0;
1092 timeinfo.tm_hour = 0;
1093 timeinfo.tm_mday = 1;
1094 timeinfo.tm_mon = next_quarter_month;
1095 }
1096 }
1097 break;
1099 if ( (timeinfo.tm_mon > 0) ||
1100 (timeinfo.tm_mday > 1) ||
1101 (timeinfo.tm_hour > 0) ||
1102 (timeinfo.tm_min > 0) ||
1103 (timeinfo.tm_sec > 0) ||
1104 (0 != (at.abs_value_us % 1000000)) )
1105 {
1106 timeinfo.tm_sec = 0;
1107 timeinfo.tm_min = 0;
1108 timeinfo.tm_hour = 0;
1109 timeinfo.tm_mday = 1;
1110 timeinfo.tm_mon = 0;
1111 timeinfo.tm_year += 1;
1112 }
1113 break;
1114 }
1115 result.abs_value_us = ((uint64_t) mktime (&timeinfo)) * 1000000ULL;
1116 return result;
1117}
1118
1119
1123{
1125 time_t seconds;
1126 struct tm timeinfo;
1127
1129 return at;
1130 if (GNUNET_TIME_RI_NONE == ri)
1131 return at;
1132 seconds = at.abs_value_us / 1000000;
1133 if (NULL == localtime_r (&seconds,
1134 &timeinfo))
1135 {
1136 GNUNET_break (0);
1137 return at;
1138 }
1139 switch (ri)
1140 {
1142 /* eh, we did this above!? */
1143 GNUNET_break (0);
1144 return at;
1146 result.abs_value_us = seconds * 1000000ULL;
1147 return result;
1149 timeinfo.tm_sec = 0;
1150 break;
1152 timeinfo.tm_sec = 0;
1153 timeinfo.tm_min = 0;
1154 break;
1155 case GNUNET_TIME_RI_DAY:
1156 timeinfo.tm_sec = 0;
1157 timeinfo.tm_min = 0;
1158 timeinfo.tm_hour = 0;
1159 break;
1161 /* Round down to Monday of current week (ISO 8601 week starts Monday) */
1162 {
1163 /* tm_way == 0 => Sunday */
1164 int days_since_monday = (0 == timeinfo.tm_wday)
1165 ? 6
1166 : (timeinfo.tm_wday - 1);
1167
1168 timeinfo.tm_sec = 0;
1169 timeinfo.tm_min = 0;
1170 timeinfo.tm_hour = 0;
1171 timeinfo.tm_mday -= days_since_monday;
1172 }
1173 break;
1175 timeinfo.tm_sec = 0;
1176 timeinfo.tm_min = 0;
1177 timeinfo.tm_hour = 0;
1178 timeinfo.tm_mday = 1;
1179 break;
1181 {
1182 int quarter_start_month = (timeinfo.tm_mon / 3) * 3;
1183
1184 timeinfo.tm_sec = 0;
1185 timeinfo.tm_min = 0;
1186 timeinfo.tm_hour = 0;
1187 timeinfo.tm_mday = 1;
1188 timeinfo.tm_mon = quarter_start_month;
1189 }
1190 break;
1192 timeinfo.tm_sec = 0;
1193 timeinfo.tm_min = 0;
1194 timeinfo.tm_hour = 0;
1195 timeinfo.tm_mday = 1;
1196 timeinfo.tm_mon = 0;
1197 break;
1198 }
1199 result.abs_value_us = (uint64_t) mktime (&timeinfo) * 1000000ULL;
1200 return result;
1201}
1202
1203
1244
1245
1249static const struct
1250{
1252 const char *str;
1253} ri_lookup_table[] = {
1254 { GNUNET_TIME_RI_NONE, "NONE" },
1255 { GNUNET_TIME_RI_SECOND, "SECOND" },
1256 { GNUNET_TIME_RI_MINUTE, "MINUTE" },
1257 { GNUNET_TIME_RI_HOUR, "HOUR" },
1258 { GNUNET_TIME_RI_DAY, "DAY" },
1259 { GNUNET_TIME_RI_WEEK, "WEEK" },
1260 { GNUNET_TIME_RI_MONTH, "MONTH" },
1261 { GNUNET_TIME_RI_QUARTER, "QUARTER" },
1262 { GNUNET_TIME_RI_YEAR, "YEAR" },
1263 { 0, NULL }
1265
1266
1269 const char *ri_str,
1271{
1272 if (NULL == ri_str)
1273 {
1274 GNUNET_break (0);
1275 return GNUNET_SYSERR;
1276 }
1277 for (unsigned int i = 0;
1278 NULL != ri_lookup_table[i].str;
1279 i++)
1280 {
1281 if (0 == strcasecmp (ri_str,
1282 ri_lookup_table[i].str))
1283 {
1284 *ri = ri_lookup_table[i].ri;
1285 return GNUNET_OK;
1286 }
1287 }
1288 return GNUNET_SYSERR;
1289}
1290
1291
1292const char *
1294{
1295 for (unsigned int i = 0;
1296 NULL != ri_lookup_table[i].str;
1297 i++)
1298 {
1299 if (ri == ri_lookup_table[i].ri)
1300 return ri_lookup_table[i].str;
1301 }
1302 GNUNET_break (0);
1303 return NULL;
1304}
1305
1306
1307void
1309
1313void __attribute__ ((destructor))
1315{
1317}
1318
1319
1320/* end of time.c */
static mp_limb_t d[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
char * getenv()
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition gnunet-arm.c:103
static int start
Set if we are to start default services (including ARM).
Definition gnunet-arm.c:38
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
static bool finished
Set to true once we are finished and should exit after sending our final message to the parent.
static struct GNUNET_SCHEDULER_Task * tt
Task scheduled to handle timeout.
static char * filename
static struct GNUNET_SCHEDULER_Task * t
Main task.
static int result
Global testing status.
static const struct GNUNET_CRYPTO_BlindablePrivateKey zero
Public key of all zeros.
static void do_round(void *cls)
Send out PUSHes and PULLs, possibly update #view, samplers.
static struct GNUNET_TIME_Relative duration
Option '-d': duration of the mapping.
Definition gnunet-vpn.c:90
struct GNUNET_PQ_ResultSpec __attribute__
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition disk.c:1258
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition disk.c:710
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition disk.c:1332
void * GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m, enum GNUNET_DISK_MapType access, size_t len)
Map a file into memory.
Definition disk.c:1404
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition disk.c:206
enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition disk.c:1435
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_OPEN_READWRITE
Open the file for both reading and writing.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_GROUP_READ
Group can read.
@ GNUNET_DISK_PERM_GROUP_WRITE
Group can write.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_DISK_MAP_TYPE_READWRITE
Read-write memory map.
#define GNUNET_log(kind,...)
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_TIME_Absolute GNUNET_TIME_year_to_time(unsigned int year)
Convert a year to an expiration time of January 1st of that year.
Definition time.c:801
struct GNUNET_TIME_Relative GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the minimum of two relative time values.
Definition time.c:344
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero_()
Return relative time of 0ms.
Definition time.c:133
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_hour_()
Return relative time of 1 hour.
Definition time.c:187
const char * GNUNET_TIME_relative2s(struct GNUNET_TIME_Relative delta, bool do_round)
Give relative time in human-readable fancy format.
Definition time.c:264
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_from_s(uint64_t s_after_epoch)
Convert seconds after the UNIX epoch to absolute time.
Definition time.c:701
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition time.c:626
bool GNUNET_TIME_absolute_is_future(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the future (excluding now).
Definition time.c:677
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_millisecond_()
Return relative time of 1ms.
Definition time.c:160
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition time.c:438
struct GNUNET_TIME_Relative GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Saturating multiply relative time by a given factor.
Definition time.c:529
struct GNUNET_TIME_Relative GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the maximum of two relative time values.
Definition time.c:352
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_minute_()
Return relative time of 1 minute.
Definition time.c:178
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_min(struct GNUNET_TIME_Timestamp t1, struct GNUNET_TIME_Timestamp t2)
Return the minimum of two timestamps.
Definition time.c:384
enum GNUNET_GenericReturnValue GNUNET_TIME_string_to_round_interval(const char *ri_str, enum GNUNET_TIME_RounderInterval *ri)
Convert rounding interval given as a string to the enum value.
Definition time.c:1268
#define GNUNET_TIME_UNIT_HOURS
One hour.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero_()
Return absolute time of 0ms.
Definition time.c:142
struct GNUNET_TIME_Timestamp GNUNET_TIME_relative_to_timestamp(struct GNUNET_TIME_Relative rel)
Convert relative time to a timestamp in the future.
Definition time.c:336
#define GNUNET_TIME_relative_cmp(t1, op, t2)
Compare two relative times.
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_from_s(uint64_t s_after_epoch)
Convert seconds after the UNIX epoch to timestamp.
Definition time.c:714
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition time.c:406
GNUNET_TIME_RounderInterval
Quantities by which we support round up absolute time values.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_max(struct GNUNET_TIME_Absolute t1, struct GNUNET_TIME_Absolute t2)
Return the maximum of two absolute time values.
Definition time.c:368
unsigned int GNUNET_TIME_get_current_year()
Return the current year (e.g.
Definition time.c:747
bool GNUNET_TIME_absolute_is_zero(struct GNUNET_TIME_Absolute abs)
Test if abs is truly zero.
Definition time.c:844
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_from_ms(uint64_t ms_after_epoch)
Convert milliseconds after the UNIX epoch to absolute time.
Definition time.c:687
bool GNUNET_TIME_relative_is_zero(struct GNUNET_TIME_Relative rel)
Test if rel is zero.
Definition time.c:660
bool GNUNET_TIME_absolute_is_never(struct GNUNET_TIME_Absolute abs)
Test if abs is never.
Definition time.c:646
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
struct GNUNET_TIME_Relative GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold)
Randomized exponential back-off, starting at 1 ms and going up by a factor of 2+r,...
Definition time.c:830
#define GNUNET_TIME_UNIT_DAYS
One day.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_subtract(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Subtract relative timestamp from the other.
Definition time.c:601
#define GNUNET_TIME_UNIT_MONTHS
One month (30 days).
const char * GNUNET_TIME_absolute2s(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition time.c:239
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_forever_()
Return "forever".
Definition time.c:205
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get()
Get the current time.
Definition time.c:111
#define GNUNET_TIME_UNIT_MINUTES
One minute.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_forever_()
Return "forever".
Definition time.c:196
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_get()
Get timestamp representing the current time.
Definition time.c:125
uint64_t GNUNET_TIME_timestamp_to_s(struct GNUNET_TIME_Timestamp ts)
Convert timestamp to number of seconds after the UNIX epoch.
Definition time.c:728
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply_double(struct GNUNET_TIME_Relative rel, double factor)
Multiply relative time by a given factor.
Definition time.c:506
bool GNUNET_TIME_relative_is_forever(struct GNUNET_TIME_Relative rel)
Test if rel is forever.
Definition time.c:653
long long GNUNET_TIME_get_offset()
Get the timestamp offset for this instance.
Definition time.c:56
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition time.c:737
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_round_down(struct GNUNET_TIME_Absolute at, struct GNUNET_TIME_Relative rt)
Round down absolute time at to multiple of rt.
Definition time.c:392
#define GNUNET_TIME_UNIT_WEEKS
One week.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_add(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Add relative times together.
Definition time.c:583
void GNUNET_TIME_set_offset(long long offset)
Set the timestamp offset for this instance.
Definition time.c:49
struct GNUNET_TIME_Relative GNUNET_TIME_randomize(struct GNUNET_TIME_Relative r)
Return a random time value between 0.5*r and 1.5*r.
Definition time.c:851
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Subtract a given relative duration from the given start time.
Definition time.c:471
unsigned int GNUNET_TIME_time_to_year(struct GNUNET_TIME_Absolute at)
Convert an expiration time to the respective year (rounds)
Definition time.c:761
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_second_()
Return relative time of 1s.
Definition time.c:169
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_unit_()
Return relative time of 1 microsecond.
Definition time.c:151
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition time.c:316
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition time.c:486
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_min(struct GNUNET_TIME_Absolute t1, struct GNUNET_TIME_Absolute t2)
Return the minimum of two absolute time values.
Definition time.c:360
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
Convert relative time to network byte order.
Definition time.c:616
#define GNUNET_TIME_UNIT_FOREVER_TS
Constant used to specify "forever".
enum GNUNET_TIME_RounderInterval GNUNET_TIME_relative_to_round_interval(struct GNUNET_TIME_Relative rel)
Convert a relative time to the corresponding rounding interval.
Definition time.c:1205
bool GNUNET_TIME_absolute_approx_eq(struct GNUNET_TIME_Absolute a1, struct GNUNET_TIME_Absolute a2, struct GNUNET_TIME_Relative t)
Test if a1 and a2 are equal within a margin of error of t.
Definition time.c:63
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition time.c:452
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_max(struct GNUNET_TIME_Timestamp t1, struct GNUNET_TIME_Timestamp t2)
Return the maximum of two timestamps.
Definition time.c:376
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_monotonic(const struct GNUNET_CONFIGURATION_Handle *cfg)
Obtain the current time and make sure it is monotonically increasing.
Definition time.c:860
struct GNUNET_TIME_Absolute GNUNET_TIME_round_down(struct GNUNET_TIME_Absolute at, enum GNUNET_TIME_RounderInterval ri)
Round @at down to the start of the next interval ri.
Definition time.c:1121
struct GNUNET_TIME_Absolute GNUNET_TIME_round_up(struct GNUNET_TIME_Absolute at, enum GNUNET_TIME_RounderInterval ri)
Round up the given at to the interval ri.
Definition time.c:983
struct GNUNET_TIME_TimestampNBO GNUNET_TIME_timestamp_hton(struct GNUNET_TIME_Timestamp t)
Convert timestamp to network byte order.
Definition time.c:91
struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Divide relative time by a given factor.
Definition time.c:548
const char * GNUNET_TIME_round_interval2s(enum GNUNET_TIME_RounderInterval ri)
Convert rounding interval to string.
Definition time.c:1293
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_difference(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute end)
Compute the time difference between the given start and end times.
Definition time.c:423
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition time.c:636
bool GNUNET_TIME_absolute_is_past(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the past (excluding now).
Definition time.c:667
struct GNUNET_TIME_Relative GNUNET_TIME_calculate_eta(struct GNUNET_TIME_Absolute start, uint64_t finished, uint64_t total)
Calculate the estimate time of arrival/completion for an operation.
Definition time.c:562
struct GNUNET_TIME_Timestamp GNUNET_TIME_timestamp_ntoh(struct GNUNET_TIME_TimestampNBO tn)
Convert timestamp from network byte order.
Definition time.c:101
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
const char * GNUNET_TIME_timestamp2s(struct GNUNET_TIME_Timestamp ts)
Convert ts to human-readable timestamp.
Definition time.c:214
#define GNUNET_TIME_UNIT_YEARS
One year (365 days).
struct GNUNET_TIME_Timestamp GNUNET_TIME_absolute_to_timestamp(struct GNUNET_TIME_Absolute at)
Round an absolute time to a timestamp.
Definition time.c:79
@ GNUNET_TIME_RI_NONE
No rounding up.
@ GNUNET_TIME_RI_MINUTE
Round up to the next minute.
@ GNUNET_TIME_RI_QUARTER
Round up to the next quarter.
@ GNUNET_TIME_RI_HOUR
Round up to the next hour.
@ GNUNET_TIME_RI_SECOND
Round up to a multiple of seconds.
@ GNUNET_TIME_RI_WEEK
Round up to the next calendar week.
@ GNUNET_TIME_RI_DAY
Round up to the next day.
@ GNUNET_TIME_RI_YEAR
Round up to the next year.
@ GNUNET_TIME_RI_MONTH
Round up to the next month.
static struct GNUNET_CONTAINER_MultiPeerMap * map
Peermap of PeerIdentities to "struct PeerEntry" (for fast lookup).
Definition peer.c:63
static unsigned int size
Size of the "table".
Definition peer.c:68
#define _(String)
GNU gettext support macro.
Definition platform.h:179
#define GNUNET_THREAD_LOCAL
Definition platform.h:248
static struct GNUNET_TIME_Relative delta
Definition speedup.c:36
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition disk.c:1385
Time for absolute time used by GNUnet, in microseconds and in network byte order.
uint64_t abs_value_us__
The actual value (in network byte order).
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds and in network byte order.
uint64_t rel_value_us__
The actual value (in network byte order).
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Time for timestamps used by GNUnet, in seconds and in network byte order.
struct GNUNET_TIME_AbsoluteNBO abs_time_nbo
The actual value.
Time for timestamps used by GNUnet, in microseconds rounded to seconds.
struct GNUNET_TIME_Absolute abs_time
The actual value.
static long long timestamp_offset
Variable used to simulate clock skew.
Definition time.c:46
#define ATOMIC
Definition time.c:37
static const struct @41 ri_lookup_table[]
Lookup table mapping rounding interval enum values to their string representations.
enum GNUNET_TIME_RounderInterval ri
Definition time.c:1251
void GNUNET_util_time_fini(void)
static time_t my_timegm(struct tm *tm)
As suggested in the timegm() man page.
Definition time.c:779
const char * str
Definition time.c:1252