Fix seconds_to_approximate_hms sometimes saying things like 1h60m (#1314).
authorCarl Hetherington <cth@carlh.net>
Mon, 28 May 2018 00:17:59 +0000 (01:17 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 28 May 2018 00:17:59 +0000 (01:17 +0100)
src/lib/util.cc
test/util_test.cc

index 3eb5ee30e09e06831924988fd7d85e37f9d81ab4..7da5f9f9078f44f10b20b3d3f50dbbf86c3fb16e 100644 (file)
@@ -157,18 +157,27 @@ seconds_to_approximate_hms (int s)
 
        string ap;
 
-       bool const hours = h > 0;
-       bool const minutes = h < 6 && m > 0;
-       bool const seconds = h == 0 && m < 10 && s > 0;
+       bool hours = h > 0;
+       bool minutes = h < 6 && m > 0;
+       bool seconds = h == 0 && m < 10 && s > 0;
 
-       if (hours) {
-               if (m > 30 && !minutes) {
-                       /// TRANSLATORS: h here is an abbreviation for hours
-                       ap += locale_convert<string>(h + 1) + _("h");
-               } else {
-                       /// TRANSLATORS: h here is an abbreviation for hours
-                       ap += locale_convert<string>(h) + _("h");
+       if (m > 30 && !minutes) {
+               /* round up the hours */
+               ++h;
+       }
+       if (s > 30 && !seconds) {
+               /* round up the minutes */
+               ++m;
+               if (m == 60) {
+                       m = 0;
+                       minutes = false;
+                       ++h;
                }
+       }
+
+       if (hours) {
+               /// TRANSLATORS: h here is an abbreviation for hours
+               ap += locale_convert<string>(h) + _("h");
 
                if (minutes || seconds) {
                        ap += N_(" ");
@@ -176,14 +185,8 @@ seconds_to_approximate_hms (int s)
        }
 
        if (minutes) {
-               /* Minutes */
-               if (s > 30 && !seconds) {
-                       /// TRANSLATORS: m here is an abbreviation for minutes
-                       ap += locale_convert<string>(m + 1) + _("m");
-               } else {
-                       /// TRANSLATORS: m here is an abbreviation for minutes
-                       ap += locale_convert<string>(m) + _("m");
-               }
+               /// TRANSLATORS: m here is an abbreviation for minutes
+               ap += locale_convert<string>(m) + _("m");
 
                if (seconds) {
                        ap += N_(" ");
index e0e70f902c4d0b8bbc7edbbdc39490d8521ea45c..4b3e20ae308580c847a38b26f5c4dc926d48a04b 100644 (file)
@@ -68,6 +68,8 @@ BOOST_AUTO_TEST_CASE (seconds_to_approximate_hms_test)
        BOOST_CHECK_EQUAL (seconds_to_approximate_hms (17 * 60 + 20), "17m");
        BOOST_CHECK_EQUAL (seconds_to_approximate_hms (1 * 3600), "1h");
        BOOST_CHECK_EQUAL (seconds_to_approximate_hms (3600 + 40 * 60), "1h 40m");
+       BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 3600), "2h");
+       BOOST_CHECK_EQUAL (seconds_to_approximate_hms (2 * 3600 - 1), "2h");
        BOOST_CHECK_EQUAL (seconds_to_approximate_hms (13 * 3600 + 40 * 60), "14h");
 }