summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2014-07-16 15:21:56 +0100
committerCarl Hetherington <cth@carlh.net>2014-07-16 15:21:56 +0100
commit4b88f8048441ba782f11707d9a19aa63e836f26c (patch)
tree524fa06d3219737324c0a381258c0357a8f89478
parent26a7e197befe79f6770395b084be6a80d4895813 (diff)
Improve approximate time reporting a bit (#383).
-rw-r--r--src/lib/util.cc51
-rw-r--r--test/util_test.cc13
2 files changed, 53 insertions, 11 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc
index 86046bcf8..6f3907391 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -143,25 +143,54 @@ seconds_to_approximate_hms (int s)
m -= (h * 60);
stringstream ap;
-
- if (h > 0) {
- if (m > 30) {
+
+ bool const hours = h > 0;
+ bool const minutes = h < 10 && m > 0;
+ bool const seconds = m < 10 && s > 0;
+
+ if (hours) {
+ if (m > 30 && !minutes) {
ap << (h + 1) << N_(" ") << _("hours");
} else {
+ ap << h << N_(" ");
if (h == 1) {
- ap << N_("1 ") << _("hour");
+ ap << _("hour");
+ } else {
+ ap << _("hours");
+ }
+ }
+
+ if (minutes | seconds) {
+ ap << N_(" ");
+ }
+ }
+
+ if (minutes) {
+ /* Minutes */
+ if (s > 30 && !seconds) {
+ ap << (m + 1) << N_(" ") << _("minutes");
+ } else {
+ ap << m << N_(" ");
+ if (m == 1) {
+ ap << _("minute");
} else {
- ap << h << N_(" ") << _("hours");
+ ap << _("minutes");
}
}
- } else if (m > 0) {
- if (m == 1) {
- ap << N_("1 ") << _("minute");
+
+ if (seconds) {
+ ap << N_(" ");
+ }
+ }
+
+ if (seconds) {
+ /* Seconds */
+ ap << s << N_(" ");
+ if (s == 1) {
+ ap << _("second");
} else {
- ap << m << N_(" ") << _("minutes");
+ ap << _("seconds");
}
- } else {
- ap << s << N_(" ") << _("seconds");
}
return ap.str ();
diff --git a/test/util_test.cc b/test/util_test.cc
index 750023d9f..40a2835f1 100644
--- a/test/util_test.cc
+++ b/test/util_test.cc
@@ -66,3 +66,16 @@ BOOST_AUTO_TEST_CASE (divide_with_round_test)
BOOST_CHECK_EQUAL (divide_with_round (1000, 500), 2);
}
+
+BOOST_AUTO_TEST_CASE (seconds_to_approximate_hms_test)
+{
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1), "1 second");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2), "2 seconds");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (60), "1 minute");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1.5 * 60), "1 minute 30 seconds");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 60), "2 minutes");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (17 * 60 + 20), "17 minutes");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1 * 3600), "1 hour");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (3600 + 40 * 60), "1 hour 40 minutes");
+ BOOST_CHECK_EQUAL (seconds_to_approximate_hms (13 * 3600 + 40 * 60), "14 hours");
+}