]> git.tdb.fi Git - libs/core.git/blob - source/time/timedelta.cpp
MSVC compatibility fixes
[libs/core.git] / source / time / timedelta.cpp
1 /*
2 This file is part of libmspcore
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #include <sstream>
7 #include <iomanip>
8 #include "timedelta.h"
9 #include "units.h"
10
11 using namespace std;
12
13 namespace {
14
15 using Msp::Time::RawTime;
16
17 void print_part(ostream &out, RawTime &value, RawTime unit, char sep, bool &first)
18 {
19         if(value<unit && first)
20                 return;
21         
22         if(!first)
23                 out<<sep<<setw(2);
24         
25         out<<value/unit;
26         value%=unit;
27         first=false;
28 }
29
30 }
31
32 namespace Msp {
33 namespace Time {
34
35 ostream &operator<<(ostream &out, const TimeDelta &td)
36 {
37         ostringstream ss;
38         ss.fill('0');
39
40         RawTime value=td.raw();
41
42         if(value<0)
43         {
44                 ss<<'-';
45                 value=-value;
46         }
47
48         if(value==0)
49                 ss<<'0';
50         else if(value<1000)
51                 ss<<value<<"µs";
52         else if(value<1000000)
53         {
54                 ss<<value/1000;
55                 value%=1000;
56                 if(value)
57                         ss<<'.'<<setw(3)<<value;
58                 ss<<"ms";
59         }
60         else
61         {
62                 bool first=true;
63                 print_part(ss, value, 86400000000LL,  0,  first);
64                 print_part(ss, value, 3600000000LL,  '-', first);
65                 print_part(ss, value, 60000000LL,    ':', first);
66                 print_part(ss, value, 1000000LL,     ':', first);
67
68                 if(value)
69                 {
70                         ss<<'.';
71                         if(value%1000)
72                                 ss<<setw(6)<<value;
73                         else
74                                 ss<<setw(3)<<value/1000;
75                 }
76                 ss<<"s";
77         }
78
79         out<<ss.str();
80
81         return out;
82 }
83
84 } // namespace Time
85 } // namespace Msp