]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/timedelta.cpp
Move non-oneliner functions out of RefPtr class declaration
[libs/core.git] / source / time / timedelta.cpp
index 891d56007a0316ff5b5a447383aaa72a6dd7ee54..a56e0fe8a3944763240b79689a55adc3d69c9ad5 100644 (file)
@@ -1,99 +1,88 @@
-/*
-This file is part of libmspframework
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-#include <sstream>
+#include <msp/strings/format.h>
 #include "timedelta.h"
-#include "units.h"
 
 using namespace std;
 
-namespace Msp {
-namespace Time {
+namespace {
+
+using Msp::Time::RawTime;
 
-void print_part(ostream &out, int64_t &value, int64_t unit, char sep, bool &first)
+string format_part(RawTime &value, RawTime unit, char sep, bool &first)
 {
-       if(value>=unit || !first)
+       if(value<unit && first)
+               return string();
+
+       RawTime v = value/unit;
+       value %= unit;
+       if(first)
        {
-               if(first)
-                       out<<value/unit;
-               else
-               {
-                       out.width(2);
-                       out<<value/unit;
-               }
-               if(sep) out<<sep;
-               value%=unit;
-               first=false;
+               first = false;
+               return Msp::format("%d", v);
        }
+       else
+               return Msp::format("%c%02d", sep, v);
+}
+
 }
 
-ostream &operator<<(ostream &out, const TimeDelta &td)
+namespace Msp {
+namespace Time {
+
+void operator<<(LexicalConverter &conv, const TimeDelta &td)
 {
-       ostringstream ss;
-       ss.fill('0');
-       if(td.usec<1000)
-               ss<<td.usec<<"µs";
-       else if(td.usec<1000000)
+       string result;
+
+       RawTime value = td.raw();
+
+       if(value<0)
        {
-               ss<<td.usec/1000;
-               if(td.usec%1000)
-               {
-                       ss<<'.';
-                       ss.width(3);
-                       ss<<td.usec%1000;
-               }
-               ss<<"ms";
+               result += '-';
+               value = -value;
        }
-       else if(td.usec<60000000)
+
+       if(value==0)
+               result += '0';
+       else if(value<1000)
+               result += format("%dµs", value);
+       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";
+               result += format("%d", value/1000);
+               value %= 1000;
+               if(value)
+                       result += format(".%03d", value);
+               result += "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;
+               bool unit = value<60000000LL;
+               result += format_part(value, 86400000000LL,  0,  first);
+               result += format_part(value, 3600000000LL,  '-', first);
+               result += format_part(value, 60000000LL,    ':', first);
+               result += format_part(value, 1000000LL,     ':', first);
+
+               if(value)
                {
-                       ss<<'.';
-                       if(temp%1000)
-                       {
-                               ss.width(6);
-                               ss<<temp;
-                       }
+                       if(value%1000)
+                               result += format(".%06d", value);
                        else
-                       {
-                               ss.width(3);
-                               ss<<temp/1000;
-                       }
+                               result += format(".%03d", value/1000);
                }
-               ss<<"s";
+               if(unit)
+                       result += 's';
        }
 
-       out<<ss.str();
-
-       return out;
+       conv.result(result);
 }
 
+const TimeDelta zero(0);
+const TimeDelta usec(1);
+const TimeDelta msec(1000);
+const TimeDelta sec(1000000);
+const TimeDelta min(60*1000000);
+const TimeDelta hour(3600*1000000LL);
+const TimeDelta day(86400*1000000LL);
+const TimeDelta week(7*86400*1000000LL);
+
 } // namespace Time
 } // namespace Msp