diff --git a/src/util.cc b/src/util.cc index 3ea64abf..efe05c70 100644 --- a/src/util.cc +++ b/src/util.cc @@ -521,26 +521,19 @@ bool isPowerOf(int num, int base) { } std::string secfmt(time_t sec) { + time_t tsec = sec; std::string str; if(sec >= 3600) { - str = itos(sec/3600); - str += "h"; + str = fmt("%lldh", static_cast(sec/3600)); sec %= 3600; } if(sec >= 60) { - int min = sec/60; - if(min < 10) { - str += "0"; - } - str += itos(min); - str += "m"; + str += fmt("%dm", static_cast(sec/60)); sec %= 60; } - if(sec < 10) { - str += "0"; + if(sec || tsec == 0) { + str += fmt("%ds", static_cast(sec)); } - str += itos(sec); - str += "s"; return str; } diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 2a30945a..5f3c99b5 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -86,6 +86,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testNextParam); CPPUNIT_TEST(testNoProxyDomainMatch); CPPUNIT_TEST(testInPrivateAddress); + CPPUNIT_TEST(testSecfmt); CPPUNIT_TEST_SUITE_END(); private: @@ -157,6 +158,7 @@ public: void testNextParam(); void testNoProxyDomainMatch(); void testInPrivateAddress(); + void testSecfmt(); }; @@ -1844,4 +1846,17 @@ void UtilTest::testInPrivateAddress() CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0")); } +void UtilTest::testSecfmt() +{ + CPPUNIT_ASSERT_EQUAL(std::string("0s"), util::secfmt(0)); + CPPUNIT_ASSERT_EQUAL(std::string("1s"), util::secfmt(1)); + CPPUNIT_ASSERT_EQUAL(std::string("9s"), util::secfmt(9)); + CPPUNIT_ASSERT_EQUAL(std::string("10s"), util::secfmt(10)); + CPPUNIT_ASSERT_EQUAL(std::string("1m"), util::secfmt(60)); + CPPUNIT_ASSERT_EQUAL(std::string("1m59s"), util::secfmt(119)); + CPPUNIT_ASSERT_EQUAL(std::string("2m"), util::secfmt(120)); + CPPUNIT_ASSERT_EQUAL(std::string("59m59s"), util::secfmt(3599)); + CPPUNIT_ASSERT_EQUAL(std::string("1h"), util::secfmt(3600)); +} + } // namespace aria2