1 | #include "duration.hpp" |
---|
2 | #include "date.hpp" |
---|
3 | #include "calendar.hpp" |
---|
4 | |
---|
5 | namespace xios |
---|
6 | { |
---|
7 | /// ////////////////////// Définitions ////////////////////// /// |
---|
8 | const CDuration Year = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
---|
9 | Month = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
---|
10 | Week = {0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0}, |
---|
11 | Day = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, |
---|
12 | Hour = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0}, |
---|
13 | Minute = {0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0}, |
---|
14 | Second = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0}, |
---|
15 | NoneDu = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
---|
16 | TimeStep = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0}; |
---|
17 | |
---|
18 | ///--------------------------------------------------------------- |
---|
19 | |
---|
20 | CDuration & CDuration::operator=(const CDuration & duration) |
---|
21 | { |
---|
22 | year = duration.year; month = duration.month ; day = duration.day; |
---|
23 | hour = duration.hour; minute = duration.minute; second = duration.second; timestep=duration.timestep ; |
---|
24 | return (*this); |
---|
25 | } |
---|
26 | |
---|
27 | StdOStream & operator<<(StdOStream & out, const CDuration & duration) |
---|
28 | { |
---|
29 | StdOStringStream sout; |
---|
30 | bool testValue = true; |
---|
31 | if(duration.year != 0.0) { testValue = false; sout << duration.year << "y " ; } |
---|
32 | if(duration.month != 0.0) { testValue = false; sout << duration.month << "mo "; } |
---|
33 | if(duration.day != 0.0) { testValue = false; sout << duration.day << "d " ; } |
---|
34 | if(duration.hour != 0.0) { testValue = false; sout << duration.hour << "h " ; } |
---|
35 | if(duration.minute != 0.0) { testValue = false; sout << duration.minute << "mi "; } |
---|
36 | if(duration.second != 0.0 || testValue) { sout << duration.second << "s " ; } |
---|
37 | if(duration.timestep != 0.0 || testValue) { sout << duration.timestep << "ts " ; } |
---|
38 | |
---|
39 | // << suppression de l'espace en fin de chaîne. |
---|
40 | out << (sout.str().substr(0, sout.str().size()-1)); |
---|
41 | return (out); |
---|
42 | } |
---|
43 | |
---|
44 | StdIStream & operator>>(StdIStream & in , CDuration & duration) |
---|
45 | { |
---|
46 | duration.year = duration.month = duration.day = |
---|
47 | duration.hour = duration.minute = duration.second = 0.0; |
---|
48 | double v = 1.0; |
---|
49 | char c = '/'; |
---|
50 | while (!in.eof()) |
---|
51 | { |
---|
52 | if (!(in >> v >> c)) |
---|
53 | { |
---|
54 | //DEBUG("----> Pb StdIStream & operator>>(StdIStream & in , CDuration & duration)") ; |
---|
55 | //if (in.eof()) DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ; |
---|
56 | } |
---|
57 | if (in.eof()) |
---|
58 | { |
---|
59 | //DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ; |
---|
60 | break ; |
---|
61 | } |
---|
62 | switch (c) |
---|
63 | { |
---|
64 | case 'y': duration.year = v; break; |
---|
65 | case 'd': duration.day = v; break; |
---|
66 | case 'h': duration.hour = v; break; |
---|
67 | case 's': duration.second = v; break; |
---|
68 | case 'm': |
---|
69 | { |
---|
70 | in >> c; |
---|
71 | if (c == 'i') duration.minute = v; |
---|
72 | else if(c == 'o') duration.month = v; |
---|
73 | else |
---|
74 | { |
---|
75 | StdString valc("m"); valc.append(1, c); |
---|
76 | //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée."); |
---|
77 | break; |
---|
78 | } |
---|
79 | break; |
---|
80 | } |
---|
81 | case 't' : |
---|
82 | { |
---|
83 | in >> c; |
---|
84 | if (c=='s') duration.timestep = v; |
---|
85 | break; |
---|
86 | } |
---|
87 | |
---|
88 | default: |
---|
89 | StdString valc; valc.append(1, c); |
---|
90 | //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée."); |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | return (in); |
---|
95 | } |
---|
96 | |
---|
97 | //----------------------------------------------------------------- |
---|
98 | |
---|
99 | bool CDuration::isNone(void) const |
---|
100 | { |
---|
101 | if ((year == 0) && (month == 0) && (day == 0) && |
---|
102 | (hour == 0) && (minute == 0) && (second == 0) && (timestep == 0)) |
---|
103 | return (true); |
---|
104 | return (false); |
---|
105 | } |
---|
106 | |
---|
107 | //----------------------------------------------------------------- |
---|
108 | |
---|
109 | CDuration & CDuration::resolve(const CCalendar & c) |
---|
110 | { |
---|
111 | // Simplification de l'écriture des minutes. |
---|
112 | second += modf(minute, &minute) * (float)c.getMinuteLength(); |
---|
113 | minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength(); |
---|
114 | |
---|
115 | // Simplification de l'écriture des heures. |
---|
116 | minute += modf(hour , &hour) * (float)c.getHourLength(); |
---|
117 | hour += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength(); |
---|
118 | |
---|
119 | // Simplification de l'écriture des jours. |
---|
120 | hour += modf(day, &day) * (float)c.getDayLength(); |
---|
121 | day += int(hour) /c.getDayLength(); hour = int(hour)%c.getDayLength(); |
---|
122 | |
---|
123 | // > Aucune équivalence jour - mois fixée par avance. // |
---|
124 | |
---|
125 | // Simplification de l'écriture des années. |
---|
126 | month += modf(year, &year) * (float)c.getYearLength(); |
---|
127 | year += int(month) /c.getYearLength(); month = int(month)%c.getYearLength(); |
---|
128 | return (*this); |
---|
129 | } |
---|
130 | |
---|
131 | //----------------------------------------------------------------- |
---|
132 | |
---|
133 | StdString CDuration::toString(void) const |
---|
134 | { |
---|
135 | const CDuration & own = *this; |
---|
136 | StdOStringStream oss; oss << own; |
---|
137 | return (oss.str()); |
---|
138 | } |
---|
139 | |
---|
140 | CDuration CDuration::FromString(const StdString & str) |
---|
141 | { |
---|
142 | CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; |
---|
143 | StdIStringStream iss(str); iss >> dr; |
---|
144 | return (dr); |
---|
145 | } |
---|
146 | |
---|
147 | ///--------------------------------------------------------------- |
---|
148 | |
---|
149 | |
---|
150 | } // namespace xios |
---|
151 | |
---|