1 | #include "duration.hpp" |
---|
2 | #include "date.hpp" |
---|
3 | #include "calendar.hpp" |
---|
4 | #include "calendar_util.hpp" |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
9 | const CDuration Year = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, |
---|
10 | Month = { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, |
---|
11 | Week = { 0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0 }, |
---|
12 | Day = { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, |
---|
13 | Hour = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 }, |
---|
14 | Minute = { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, |
---|
15 | Second = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 }, |
---|
16 | TimeStep = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }, |
---|
17 | NoneDu = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
---|
18 | |
---|
19 | ///--------------------------------------------------------------- |
---|
20 | |
---|
21 | CDuration& CDuration::operator=(const CDuration& duration) |
---|
22 | { |
---|
23 | year = duration.year; month = duration.month; day = duration.day; |
---|
24 | hour = duration.hour; minute = duration.minute; second = duration.second; timestep = duration.timestep; |
---|
25 | return *this; |
---|
26 | } |
---|
27 | |
---|
28 | StdOStream& operator<<(StdOStream& out, const CDuration& duration) |
---|
29 | { |
---|
30 | StdOStringStream sout; |
---|
31 | bool forceOutput = true; |
---|
32 | |
---|
33 | if (duration.year != 0.0) { forceOutput = false; sout << duration.year << "y "; } |
---|
34 | if (duration.month != 0.0) { forceOutput = false; sout << duration.month << "mo "; } |
---|
35 | if (duration.day != 0.0) { forceOutput = false; sout << duration.day << "d "; } |
---|
36 | if (duration.hour != 0.0) { forceOutput = false; sout << duration.hour << "h "; } |
---|
37 | if (duration.minute != 0.0) { forceOutput = false; sout << duration.minute << "mi "; } |
---|
38 | if (duration.second != 0.0) { forceOutput = false; sout << duration.second << "s "; } |
---|
39 | if (duration.timestep != 0.0 || forceOutput) { sout << duration.timestep << "ts "; } |
---|
40 | |
---|
41 | // suppression de l'espace en fin de chaîne. |
---|
42 | StdString strOut = sout.str(); |
---|
43 | out << strOut.erase(strOut.size() - 1); |
---|
44 | return out; |
---|
45 | } |
---|
46 | |
---|
47 | StdIStream& operator>>(StdIStream& in , CDuration& duration) |
---|
48 | { |
---|
49 | duration = NoneDu; |
---|
50 | double v = 1.0; |
---|
51 | char c = '/'; |
---|
52 | bool invalidUnit = false; |
---|
53 | |
---|
54 | do |
---|
55 | { |
---|
56 | in >> v >> c; |
---|
57 | if (in.fail()) |
---|
58 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
59 | << "Bad duration format: impossible to read a pair (value, unit)."); |
---|
60 | |
---|
61 | switch (c) |
---|
62 | { |
---|
63 | case 'y': duration.year = v; break; |
---|
64 | case 'd': duration.day = v; break; |
---|
65 | case 'h': duration.hour = v; break; |
---|
66 | case 's': duration.second = v; break; |
---|
67 | case 'm': |
---|
68 | { |
---|
69 | in >> c; |
---|
70 | if (c == 'i') duration.minute = v; |
---|
71 | else if (c == 'o') duration.month = v; |
---|
72 | else invalidUnit = true; |
---|
73 | break; |
---|
74 | } |
---|
75 | case 't': |
---|
76 | { |
---|
77 | in >> c; |
---|
78 | if (c == 's') duration.timestep = v; |
---|
79 | else invalidUnit = true; |
---|
80 | break; |
---|
81 | } |
---|
82 | default: |
---|
83 | invalidUnit = true; |
---|
84 | break; |
---|
85 | } |
---|
86 | |
---|
87 | if (invalidUnit) |
---|
88 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
89 | << "Bad duration format: invalid unit, unexpected '" << c << "' character."); |
---|
90 | } while (in.peek() != EOF); // check whether there is a next character to read |
---|
91 | |
---|
92 | return in; |
---|
93 | } |
---|
94 | |
---|
95 | //----------------------------------------------------------------- |
---|
96 | |
---|
97 | bool CDuration::isNone(void) const |
---|
98 | { |
---|
99 | return (*this == NoneDu); |
---|
100 | } |
---|
101 | |
---|
102 | //----------------------------------------------------------------- |
---|
103 | |
---|
104 | CDuration& CDuration::solveTimeStep(const CCalendar& c) |
---|
105 | { |
---|
106 | CDuration timeStep = c.getTimeStep(); |
---|
107 | second += timestep * timeStep.second; |
---|
108 | minute += timestep * timeStep.minute; |
---|
109 | hour += timestep * timeStep.hour; |
---|
110 | day += timestep * timeStep.day; |
---|
111 | month += timestep * timeStep.month; |
---|
112 | year += timestep * timeStep.year; |
---|
113 | timestep = 0.0; |
---|
114 | } |
---|
115 | |
---|
116 | CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/) |
---|
117 | { |
---|
118 | return c.resolve(*this, noNegativeTime); |
---|
119 | } |
---|
120 | |
---|
121 | //----------------------------------------------------------------- |
---|
122 | |
---|
123 | StdString CDuration::toString(void) const |
---|
124 | { |
---|
125 | StdOStringStream oss; oss << *this; |
---|
126 | return oss.str(); |
---|
127 | } |
---|
128 | |
---|
129 | CDuration CDuration::FromString(const StdString& str) |
---|
130 | { |
---|
131 | CDuration dr = NoneDu; |
---|
132 | StdIStringStream iss(str); iss >> dr; |
---|
133 | return dr; |
---|
134 | } |
---|
135 | } // namespace xios |
---|
136 | |
---|