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