Distributed under the LGPL
*/
-#include <sstream>
-#include <iomanip>
+#include <msp/strings/format.h>
#include "timedelta.h"
#include "units.h"
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;
+ return string();
+
+ RawTime v = value/unit;
value %= unit;
- first = false;
+ 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();
if(value<0)
{
- ss<<'-';
+ 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;
+ 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 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);
}
} // namespace Time