-/* $Id$
-
-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;
using Msp::Time::RawTime;
-void print_part(ostream &out, RawTime &value, RawTime unit, char sep, bool &first)
+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);
}
}
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;
- RawTime 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