Improve approximate time reporting a bit (#383).
authorCarl Hetherington <cth@carlh.net>
Wed, 16 Jul 2014 14:21:56 +0000 (15:21 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 16 Jul 2014 14:21:56 +0000 (15:21 +0100)
src/lib/util.cc
test/util_test.cc

index 86046bcf8cf8a7ff1a2657b87ec07584bb850d17..6f39073910e8ee055c3197535ee3cc98dd35cee0 100644 (file)
@@ -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 ();
index 750023d9f1a689bb4414706b66c352a4b6cda2d8..40a2835f1515f68a47ba5b9981a812778ad10295 100644 (file)
@@ -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");
+}