Browse Source

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".
Tatsuhiro Tsujikawa 14 years ago
parent
commit
c42dd7e755
2 changed files with 20 additions and 12 deletions
  1. 5 12
      src/util.cc
  2. 15 0
      test/UtilTest.cc

+ 5 - 12
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<long long int>(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<int>(sec/60));
     sec %= 60;
   }
-  if(sec < 10) {
-    str += "0";
+  if(sec || tsec == 0) {
+    str += fmt("%ds", static_cast<int>(sec));
   }
-  str += itos(sec);
-  str += "s";
   return str;
 }
 

+ 15 - 0
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