]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timedelta.cpp
Added DateTime
[libs/core.git] / source / time / timedelta.cpp
index 891d56007a0316ff5b5a447383aaa72a6dd7ee54..f7b037ea7d154dac0e7b931106fbd2932a1dbbba 100644 (file)
@@ -1,91 +1,75 @@
 /*
-This file is part of libmspframework
+This file is part of libmspcore
 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #include <sstream>
+#include <iomanip>
 #include "timedelta.h"
 #include "units.h"
 
 using namespace std;
 
-namespace Msp {
-namespace Time {
+namespace {
 
 void print_part(ostream &out, int64_t &value, int64_t unit, char sep, bool &first)
 {
-       if(value>=unit || !first)
-       {
-               if(first)
-                       out<<value/unit;
-               else
-               {
-                       out.width(2);
-                       out<<value/unit;
-               }
-               if(sep) out<<sep;
-               value%=unit;
-               first=false;
-       }
+       if(!value || (value<unit && first))
+               return;
+       
+       if(!first)
+               out<<sep<<setw(2);
+       
+       out<<value/unit;
+       value%=unit;
+       first=false;
 }
 
+}
+
+namespace Msp {
+namespace Time {
+
 ostream &operator<<(ostream &out, const TimeDelta &td)
 {
        ostringstream ss;
        ss.fill('0');
-       if(td.usec<1000)
-               ss<<td.usec<<"µs";
-       else if(td.usec<1000000)
+
+       int64_t value=td.raw();
+
+       if(value<0)
        {
-               ss<<td.usec/1000;
-               if(td.usec%1000)
-               {
-                       ss<<'.';
-                       ss.width(3);
-                       ss<<td.usec%1000;
-               }
-               ss<<"ms";
+               ss<<'-';
+               value=-value;
        }
-       else if(td.usec<60000000)
+
+       if(value==0)
+               ss<<'0';
+       else if(value<1000)
+               ss<<value<<"µs";
+       else if(value<1000000)
        {
-               ss<<td.usec/1000000;
-               if(td.usec%1000000)
-               {
-                       ss<<'.';
-                       if(td.usec%1000)
-                       {
-                               ss.width(6);
-                               ss<<td.usec%1000000;
-                       }
-                       else
-                       {
-                               ss.width(3);
-                               ss<<td.usec/1000%1000;
-                       }
-               }
-               ss<<"s";
+               ss<<value/1000;
+               value%=1000;
+               if(value)
+                       ss<<'.'<<setw(3)<<value;
+               ss<<"ms";
        }
        else
        {
-               int64_t temp=td.usec;
-               bool    first=true;
-               print_part(ss, temp, day.raw(),  '-', first);
-               print_part(ss, temp, hour.raw(), ':', first);
-               print_part(ss, temp, min.raw(),  ':', first);
-               print_part(ss, temp, sec.raw(),  0,   first);
-               if(temp)
+               bool first=true;
+               print_part(ss, value, 86400000000LL,  0,  first);
+               print_part(ss, value, 3600000000LL,  '-', first);
+               print_part(ss, value, 60000000LL,    ':', first);
+               print_part(ss, value, 1000000LL,     ':', first);
+
+               if(value)
                {
                        ss<<'.';
-                       if(temp%1000)
-                       {
-                               ss.width(6);
-                               ss<<temp;
-                       }
+                       if(value%1000)
+                               ss<<setw(6)<<value;
                        else
-                       {
-                               ss.width(3);
-                               ss<<temp/1000;
-                       }
+                               ss<<setw(3)<<value/1000;
                }
                ss<<"s";
        }