aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-16 11:27:08 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-16 11:27:08 +0000
commitf736b24935d66733e9a53798fbc87dbc8aff9fca (patch)
treeb7f865216ca4b40a4a6e4535dfa6677700a27efc /src/util
parentc5784d2c94adcf1fe80e88c9ab1c152ce2fcbe95 (diff)
downloadgnunet-f736b24935d66733e9a53798fbc87dbc8aff9fca.tar.gz
gnunet-f736b24935d66733e9a53798fbc87dbc8aff9fca.zip
-fixing #2546
Diffstat (limited to 'src/util')
-rw-r--r--src/util/common_logging.c5
-rw-r--r--src/util/container_meta_data.c3
-rw-r--r--src/util/strings.c40
-rw-r--r--src/util/test_strings.c39
-rw-r--r--src/util/time.c24
5 files changed, 53 insertions, 58 deletions
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 873420460..fbc0de50c 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -798,7 +798,7 @@ flush_bulk (const char *datestr)
798 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256]; 798 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
799 int rev; 799 int rev;
800 char *last; 800 char *last;
801 char *ft; 801 const char *ft;
802 802
803 if ((last_bulk_time.abs_value == 0) || (last_bulk_repeat == 0)) 803 if ((last_bulk_time.abs_value == 0) || (last_bulk_repeat == 0))
804 return; 804 return;
@@ -814,11 +814,10 @@ flush_bulk (const char *datestr)
814 last[0] = '\0'; 814 last[0] = '\0';
815 } 815 }
816 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration 816 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
817 (last_bulk_time)); 817 (last_bulk_time), GNUNET_YES);
818 snprintf (msg, sizeof (msg), 818 snprintf (msg, sizeof (msg),
819 _("Message `%.*s' repeated %u times in the last %s\n"), 819 _("Message `%.*s' repeated %u times in the last %s\n"),
820 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); 820 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
821 GNUNET_free (ft);
822 if (rev == 1) 821 if (rev == 1)
823 last[0] = '\n'; 822 last[0] = '\n';
824 output_message (last_bulk_kind, last_bulk_comp, datestr, msg); 823 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
diff --git a/src/util/container_meta_data.c b/src/util/container_meta_data.c
index e725486b7..9e60a746a 100644
--- a/src/util/container_meta_data.c
+++ b/src/util/container_meta_data.c
@@ -427,7 +427,7 @@ void
427GNUNET_CONTAINER_meta_data_add_publication_date (struct 427GNUNET_CONTAINER_meta_data_add_publication_date (struct
428 GNUNET_CONTAINER_MetaData *md) 428 GNUNET_CONTAINER_MetaData *md)
429{ 429{
430 char *dat; 430 const char *dat;
431 struct GNUNET_TIME_Absolute t; 431 struct GNUNET_TIME_Absolute t;
432 432
433 t = GNUNET_TIME_absolute_get (); 433 t = GNUNET_TIME_absolute_get ();
@@ -438,7 +438,6 @@ GNUNET_CONTAINER_meta_data_add_publication_date (struct
438 EXTRACTOR_METATYPE_PUBLICATION_DATE, 438 EXTRACTOR_METATYPE_PUBLICATION_DATE,
439 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 439 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
440 dat, strlen (dat) + 1); 440 dat, strlen (dat) + 1);
441 GNUNET_free (dat);
442} 441}
443 442
444 443
diff --git a/src/util/strings.c b/src/util/strings.c
index ef7658697..efa6029b4 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -616,33 +616,46 @@ GNUNET_STRINGS_filename_expand (const char *fil)
616 616
617/** 617/**
618 * Give relative time in human-readable fancy format. 618 * Give relative time in human-readable fancy format.
619 * This is one of the very few calls in the entire API that is
620 * NOT reentrant!
619 * 621 *
620 * @param delta time in milli seconds 622 * @param delta time in milli seconds
623 * @param do_round are we allowed to round a bit?
621 * @return time as human-readable string 624 * @return time as human-readable string
622 */ 625 */
623char * 626const char *
624GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta) 627GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
628 int do_round)
625{ 629{
630 static char buf[128];
626 const char *unit = _( /* time unit */ "ms"); 631 const char *unit = _( /* time unit */ "ms");
627 char *ret; 632 char *ret;
628 uint64_t dval = delta.rel_value; 633 uint64_t dval = delta.rel_value;
629 634
630 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value) 635 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
631 return GNUNET_strdup (_("forever")); 636 return _("forever");
632 if ( (dval > 5 * 1000) || (0 == (dval % 1000) )) 637 if ( ( (GNUNET_YES == do_round) &&
638 (dval > 5 * 1000) ) ||
639 (0 == (dval % 1000) ))
633 { 640 {
634 dval = dval / 1000; 641 dval = dval / 1000;
635 unit = _( /* time unit */ "s"); 642 unit = _( /* time unit */ "s");
636 if ( (dval > 5 * 60) || (0 == (dval % 60) ) ) 643 if ( ( (GNUNET_YES == do_round) &&
644 (dval > 5 * 60) ) ||
645 (0 == (dval % 60) ) )
637 { 646 {
638 dval = dval / 60; 647 dval = dval / 60;
639 unit = _( /* time unit */ "m"); 648 unit = _( /* time unit */ "m");
640 if ( (dval > 5 * 60) || (0 == (dval % 60) )) 649 if ( ( (GNUNET_YES == do_round) &&
650 (dval > 5 * 60) ) ||
651 (0 == (dval % 60) ))
641 { 652 {
642 dval = dval / 60; 653 dval = dval / 60;
643 unit = _( /* time unit */ "h"); 654 unit = _( /* time unit */ "h");
644 if ( (dval > 5 * 24) || (0 == (dval % 24)) ) 655 if ( ( (GNUNET_YES == do_round) &&
645 { 656 (dval > 5 * 24) ) ||
657 (0 == (dval % 24)) )
658 {
646 dval = dval / 24; 659 dval = dval / 24;
647 if (1 == dval) 660 if (1 == dval)
648 unit = _( /* time unit */ "day"); 661 unit = _( /* time unit */ "day");
@@ -652,23 +665,26 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
652 } 665 }
653 } 666 }
654 } 667 }
655 GNUNET_asprintf (&ret, "%llu %s", dval, unit); 668 GNUNET_snprintf (buf, sizeof (buf),
656 return ret; 669 "%llu %s", dval, unit);
670 return buf;
657} 671}
658 672
659 673
660/** 674/**
661 * "asctime", except for GNUnet time. 675 * "asctime", except for GNUnet time.
676 * This is one of the very few calls in the entire API that is
677 * NOT reentrant!
662 * 678 *
663 * @param t time to convert 679 * @param t time to convert
664 * @return absolute time in human-readable format 680 * @return absolute time in human-readable format
665 */ 681 */
666char * 682const char *
667GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) 683GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
668{ 684{
685 static char buf[255];
669 time_t tt; 686 time_t tt;
670 struct tm *tp; 687 struct tm *tp;
671 char buf[255];
672 688
673 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value) 689 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
674 return GNUNET_strdup (_("end of time")); 690 return GNUNET_strdup (_("end of time"));
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
index 749650589..a77acaa59 100644
--- a/src/util/test_strings.c
+++ b/src/util/test_strings.c
@@ -27,6 +27,7 @@
27 27
28 28
29#define WANT(a,b) if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_free(b); GNUNET_break(0); return 1;} else { GNUNET_free (b); } 29#define WANT(a,b) if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_free(b); GNUNET_break(0); return 1;} else { GNUNET_free (b); }
30#define WANTNF(a,b) do { if (0 != strcmp(a,b)) { fprintf(stderr, "Got `%s', wanted `%s'\n", b, a); GNUNET_break(0); return 1;} } while (0)
30#define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { } 31#define WANTB(a,b,l) if (0 != memcmp(a,b,l)) { GNUNET_break(0); return 1;} else { }
31 32
32int 33int
@@ -35,6 +36,7 @@ main (int argc, char *argv[])
35 char buf[128]; 36 char buf[128];
36 char *r; 37 char *r;
37 char *b; 38 char *b;
39 const char *bc;
38 struct GNUNET_TIME_Absolute at; 40 struct GNUNET_TIME_Absolute at;
39 struct GNUNET_TIME_Absolute atx; 41 struct GNUNET_TIME_Absolute atx;
40 const char *hdir; 42 const char *hdir;
@@ -50,20 +52,20 @@ main (int argc, char *argv[])
50 b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL); 52 b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
51 WANT (buf, b); 53 WANT (buf, b);
52 sprintf (buf, "4 %s", _( /* time unit */ "ms")); 54 sprintf (buf, "4 %s", _( /* time unit */ "ms"));
53 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply 55 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
54 (GNUNET_TIME_UNIT_MILLISECONDS, 56 (GNUNET_TIME_UNIT_MILLISECONDS,
55 4)); 57 4), GNUNET_YES);
56 WANT (buf, b); 58 WANTNF (buf, bc);
57 sprintf (buf, "7 %s", _( /* time unit */ "s")); 59 sprintf (buf, "7 %s", _( /* time unit */ "s"));
58 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply 60 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
59 (GNUNET_TIME_UNIT_MILLISECONDS, 61 (GNUNET_TIME_UNIT_MILLISECONDS,
60 7 * 1000)); 62 7 * 1000), GNUNET_YES);
61 WANT (buf, b); 63 WANTNF (buf, bc);
62 sprintf (buf, "7 %s", _( /* time unit */ "h")); 64 sprintf (buf, "7 %s", _( /* time unit */ "h"));
63 b = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply 65 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
64 (GNUNET_TIME_UNIT_MILLISECONDS, 66 (GNUNET_TIME_UNIT_MILLISECONDS,
65 7 * 60 * 60 * 1000)); 67 7 * 60 * 60 * 1000), GNUNET_YES);
66 WANT (buf, b); 68 WANTNF (buf, bc);
67#ifndef MINGW 69#ifndef MINGW
68 hdir = getenv ("HOME"); 70 hdir = getenv ("HOME");
69#else 71#else
@@ -84,27 +86,24 @@ main (int argc, char *argv[])
84 if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b)) 86 if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
85 return 1; 87 return 1;
86 at.abs_value = 5000; 88 at.abs_value = 5000;
87 r = GNUNET_STRINGS_absolute_time_to_string (at); 89 bc = GNUNET_STRINGS_absolute_time_to_string (at);
88 /* r should be something like "Wed Dec 31 17:00:05 1969" 90 /* bc should be something like "Wed Dec 31 17:00:05 1969"
89 * where the details of the day and hour depend on the timezone; 91 * where the details of the day and hour depend on the timezone;
90 * however, the "0:05 19" should always be there; hence: */ 92 * however, the "0:05 19" should always be there; hence: */
91 if (NULL == strstr (r, "0:05 19")) 93 if (NULL == strstr (bc, "0:05 19"))
92 { 94 {
93 FPRINTF (stderr, "Got %s\n", r); 95 FPRINTF (stderr, "Got %s\n", bc);
94 GNUNET_break (0); 96 GNUNET_break (0);
95 GNUNET_free (r);
96 return 1; 97 return 1;
97 } 98 }
98 GNUNET_free (r);
99 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII"); 99 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
100 WANT ("TEST", b); 100 WANT ("TEST", b);
101 101
102 at = GNUNET_TIME_UNIT_FOREVER_ABS; 102 at = GNUNET_TIME_UNIT_FOREVER_ABS;
103 b = GNUNET_STRINGS_absolute_time_to_string (at); 103 bc = GNUNET_STRINGS_absolute_time_to_string (at);
104 GNUNET_assert (GNUNET_OK == 104 GNUNET_assert (GNUNET_OK ==
105 GNUNET_STRINGS_fancy_time_to_absolute (b, &atx)); 105 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
106 GNUNET_assert (atx.abs_value == at.abs_value); 106 GNUNET_assert (atx.abs_value == at.abs_value);
107 GNUNET_free (b);
108 107
109#if ENABLE_NLS && HAVE_ICONV 108#if ENABLE_NLS && HAVE_ICONV
110 GNUNET_log_skip (2, GNUNET_NO); 109 GNUNET_log_skip (2, GNUNET_NO);
diff --git a/src/util/time.c b/src/util/time.c
index 7467b4497..afb0c00c1 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -484,6 +484,7 @@ GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
484 return ret; 484 return ret;
485} 485}
486 486
487
487/** 488/**
488 * Convert relative time from network byte order. 489 * Convert relative time from network byte order.
489 * 490 *
@@ -500,6 +501,7 @@ GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
500 501
501} 502}
502 503
504
503/** 505/**
504 * Convert absolute time to network byte order. 506 * Convert absolute time to network byte order.
505 * 507 *
@@ -515,6 +517,7 @@ GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
515 return ret; 517 return ret;
516} 518}
517 519
520
518/** 521/**
519 * Convert absolute time from network byte order. 522 * Convert absolute time from network byte order.
520 * 523 *
@@ -531,26 +534,5 @@ GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
531 534
532} 535}
533 536
534/**
535 * Convert a relative time to a string.
536 * This is one of the very few calls in the entire API that is
537 * NOT reentrant!
538 *
539 * @param time the time to print
540 *
541 * @return string form of the time (as milliseconds)
542 */
543const char *
544GNUNET_TIME_relative_to_string (struct GNUNET_TIME_Relative time)
545{
546 static char time_string[21];
547
548 memset (time_string, 0, sizeof (time_string));
549
550 sprintf (time_string, "%llu", (unsigned long long) time.rel_value);
551 return (const char *) time_string;
552}
553
554
555 537
556/* end of time.c */ 538/* end of time.c */