]> 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 7d3d4bb6d2a2ff706ca898582bc241fb72e1a7e2..a56e0fe8a3944763240b79689a55adc3d69c9ad5 100644 (file)
@@ -1,28 +1,26 @@
-/*
-This file is part of libmspcore
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-#include <sstream>
-#include <iomanip>
+#include <msp/strings/format.h>
 #include "timedelta.h"
-#include "units.h"
 
 using namespace std;
 
 namespace {
 
-void print_part(ostream &out, int64_t &value, int64_t unit, char sep, bool &first)
+using Msp::Time::RawTime;
+
+string format_part(RawTime &value, RawTime unit, char sep, bool &first)
 {
        if(value<unit && first)
-               return;
-       
-       if(!first)
-               out<<sep<<setw(2);
-       
-       out<<value/unit;
-       value%=unit;
-       first=false;
+               return string();
+
+       RawTime v = value/unit;
+       value %= unit;
+       if(first)
+       {
+               first = false;
+               return Msp::format("%d", v);
+       }
+       else
+               return Msp::format("%c%02d", sep, v);
 }
 
 }
@@ -30,54 +28,61 @@ void print_part(ostream &out, int64_t &value, int64_t unit, char sep, bool &firs
 namespace Msp {
 namespace Time {
 
-ostream &operator<<(ostream &out, const TimeDelta &td)
+void operator<<(LexicalConverter &conv, const TimeDelta &td)
 {
-       ostringstream ss;
-       ss.fill('0');
+       string result;
 
-       int64_t value=td.raw();
+       RawTime value = td.raw();
 
        if(value<0)
        {
-               ss<<'-';
-               value=-value;
+               result += '-';
+               value = -value;
        }
 
        if(value==0)
-               ss<<'0';
+               result += '0';
        else if(value<1000)
-               ss<<value<<"µs";
+               result += format("%dµs", value);
        else if(value<1000000)
        {
-               ss<<value/1000;
-               value%=1000;
+               result += format("%d", value/1000);
+               value %= 1000;
                if(value)
-                       ss<<'.'<<setw(3)<<value;
-               ss<<"ms";
+                       result += format(".%03d", value);
+               result += "ms";
        }
        else
        {
-               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);
+               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(value%1000)
-                               ss<<setw(6)<<value;
+                               result += format(".%06d", value);
                        else
-                               ss<<setw(3)<<value/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