aboutsummaryrefslogtreecommitdiff
path: root/src/util/strings.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/strings.c')
-rw-r--r--src/util/strings.c40
1 files changed, 28 insertions, 12 deletions
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"));