From 9d904847726295852b78b0029939d0f848793d06 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 28 May 2018 01:17:59 +0100 Subject: [PATCH] Fix seconds_to_approximate_hms sometimes saying things like 1h60m (#1314). --- src/lib/util.cc | 39 +++++++++++++++++++++------------------ test/util_test.cc | 2 ++ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/lib/util.cc b/src/lib/util.cc index 3eb5ee30e..7da5f9f90 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -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(h + 1) + _("h"); - } else { - /// TRANSLATORS: h here is an abbreviation for hours - ap += locale_convert(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(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(m + 1) + _("m"); - } else { - /// TRANSLATORS: m here is an abbreviation for minutes - ap += locale_convert(m) + _("m"); - } + /// TRANSLATORS: m here is an abbreviation for minutes + ap += locale_convert(m) + _("m"); if (seconds) { ap += N_(" "); diff --git a/test/util_test.cc b/test/util_test.cc index e0e70f902..4b3e20ae3 100644 --- a/test/util_test.cc +++ b/test/util_test.cc @@ -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"); } -- 2.30.2