--- /dev/null
+/*
+This file is part of libmspframework
+Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+#include <sstream>
+#include "timedelta.h"
+#include "units.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Time {
+
+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;
+ }
+}
+
+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)
+ {
+ ss<<td.usec/1000;
+ if(td.usec%1000)
+ {
+ ss<<'.';
+ ss.width(3);
+ ss<<td.usec%1000;
+ }
+ ss<<"ms";
+ }
+ else if(td.usec<60000000)
+ {
+ 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";
+ }
+ 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)
+ {
+ ss<<'.';
+ if(temp%1000)
+ {
+ ss.width(6);
+ ss<<temp;
+ }
+ else
+ {
+ ss.width(3);
+ ss<<temp/1000;
+ }
+ }
+ ss<<"s";
+ }
+
+ out<<ss.str();
+
+ return out;
+}
+
+} // namespace Time
+} // namespace Msp