Changed format of ETA.

Now no leading 0 is used. If hour part and/or min part is non-zero,
and sec part is 0, sec part is omitted, like this "1h3m".
pull/4/head
Tatsuhiro Tsujikawa 2011-11-13 20:59:15 +09:00
parent 1687741303
commit c42dd7e755
2 changed files with 20 additions and 12 deletions

View File

@ -521,26 +521,19 @@ bool isPowerOf(int num, int base) {
} }
std::string secfmt(time_t sec) { std::string secfmt(time_t sec) {
time_t tsec = sec;
std::string str; std::string str;
if(sec >= 3600) { if(sec >= 3600) {
str = itos(sec/3600); str = fmt("%lldh", static_cast<long long int>(sec/3600));
str += "h";
sec %= 3600; sec %= 3600;
} }
if(sec >= 60) { if(sec >= 60) {
int min = sec/60; str += fmt("%dm", static_cast<int>(sec/60));
if(min < 10) {
str += "0";
}
str += itos(min);
str += "m";
sec %= 60; sec %= 60;
} }
if(sec < 10) { if(sec || tsec == 0) {
str += "0"; str += fmt("%ds", static_cast<int>(sec));
} }
str += itos(sec);
str += "s";
return str; return str;
} }

View File

@ -86,6 +86,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testNextParam); CPPUNIT_TEST(testNextParam);
CPPUNIT_TEST(testNoProxyDomainMatch); CPPUNIT_TEST(testNoProxyDomainMatch);
CPPUNIT_TEST(testInPrivateAddress); CPPUNIT_TEST(testInPrivateAddress);
CPPUNIT_TEST(testSecfmt);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
private: private:
@ -157,6 +158,7 @@ public:
void testNextParam(); void testNextParam();
void testNoProxyDomainMatch(); void testNoProxyDomainMatch();
void testInPrivateAddress(); void testInPrivateAddress();
void testSecfmt();
}; };
@ -1844,4 +1846,17 @@ void UtilTest::testInPrivateAddress()
CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0")); 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 } // namespace aria2