[219] | 1 | #include "duration.hpp" |
---|
| 2 | #include "date.hpp" |
---|
| 3 | #include "calendar.hpp" |
---|
| 4 | |
---|
[335] | 5 | namespace xios |
---|
[219] | 6 | { |
---|
| 7 | /// ////////////////////// Définitions ////////////////////// /// |
---|
[424] | 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}; |
---|
[219] | 17 | |
---|
| 18 | ///--------------------------------------------------------------- |
---|
| 19 | |
---|
| 20 | CDuration & CDuration::operator=(const CDuration & duration) |
---|
| 21 | { |
---|
[579] | 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; |
---|
[219] | 25 | } |
---|
| 26 | |
---|
| 27 | StdOStream & operator<<(StdOStream & out, const CDuration & duration) |
---|
| 28 | { |
---|
[612] | 29 | out << duration.toString(); |
---|
[579] | 30 | return out; |
---|
[219] | 31 | } |
---|
| 32 | |
---|
| 33 | StdIStream & operator>>(StdIStream & in , CDuration & duration) |
---|
| 34 | { |
---|
| 35 | duration.year = duration.month = duration.day = |
---|
[448] | 36 | duration.hour = duration.minute = duration.second = duration.timestep=0.0; |
---|
[219] | 37 | double v = 1.0; |
---|
| 38 | char c = '/'; |
---|
| 39 | while (!in.eof()) |
---|
| 40 | { |
---|
[579] | 41 | if (!(in >> v >> c)) |
---|
[266] | 42 | { |
---|
[579] | 43 | //DEBUG("----> Pb StdIStream & operator>>(StdIStream & in , CDuration & duration)"); |
---|
| 44 | //if (in.eof()) DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)"); |
---|
[266] | 45 | } |
---|
[579] | 46 | if (in.eof()) |
---|
[266] | 47 | { |
---|
[579] | 48 | //DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)"); |
---|
| 49 | break; |
---|
[266] | 50 | } |
---|
[219] | 51 | switch (c) |
---|
| 52 | { |
---|
[266] | 53 | case 'y': duration.year = v; break; |
---|
| 54 | case 'd': duration.day = v; break; |
---|
| 55 | case 'h': duration.hour = v; break; |
---|
| 56 | case 's': duration.second = v; break; |
---|
[219] | 57 | case 'm': |
---|
[266] | 58 | { |
---|
[219] | 59 | in >> c; |
---|
| 60 | if (c == 'i') duration.minute = v; |
---|
| 61 | else if(c == 'o') duration.month = v; |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | StdString valc("m"); valc.append(1, c); |
---|
[266] | 65 | //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée."); |
---|
| 66 | break; |
---|
[219] | 67 | } |
---|
[266] | 68 | break; |
---|
| 69 | } |
---|
[424] | 70 | case 't' : |
---|
| 71 | { |
---|
| 72 | in >> c; |
---|
[579] | 73 | if (c=='s') duration.timestep = v; |
---|
[424] | 74 | break; |
---|
| 75 | } |
---|
[579] | 76 | |
---|
[219] | 77 | default: |
---|
| 78 | StdString valc; valc.append(1, c); |
---|
[266] | 79 | //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée."); |
---|
| 80 | break; |
---|
[219] | 81 | } |
---|
| 82 | } |
---|
[579] | 83 | return in; |
---|
[219] | 84 | } |
---|
| 85 | |
---|
| 86 | //----------------------------------------------------------------- |
---|
| 87 | |
---|
| 88 | bool CDuration::isNone(void) const |
---|
| 89 | { |
---|
| 90 | if ((year == 0) && (month == 0) && (day == 0) && |
---|
[424] | 91 | (hour == 0) && (minute == 0) && (second == 0) && (timestep == 0)) |
---|
[579] | 92 | return true; |
---|
| 93 | return false; |
---|
[219] | 94 | } |
---|
| 95 | |
---|
| 96 | //----------------------------------------------------------------- |
---|
[436] | 97 | CDuration & CDuration::solveTimeStep(const CCalendar & c) |
---|
| 98 | { |
---|
[579] | 99 | CDuration timeStep=c.getTimeStep(); |
---|
| 100 | second +=timestep*timeStep.second; |
---|
| 101 | minute +=timestep*timeStep.minute; |
---|
| 102 | hour +=timestep*timeStep.hour; |
---|
| 103 | day +=timestep*timeStep.day; |
---|
| 104 | month +=timestep*timeStep.month; |
---|
| 105 | year +=timestep*timeStep.year; |
---|
| 106 | timestep = 0; |
---|
| 107 | return *this; |
---|
| 108 | } |
---|
[448] | 109 | |
---|
[219] | 110 | CDuration & CDuration::resolve(const CCalendar & c) |
---|
| 111 | { |
---|
[648] | 112 | // Convert year, day, hour and minute to integer |
---|
| 113 | month += modf(year, &year) * c.getYearLength(); |
---|
| 114 | hour += modf(day, &day) * c.getDayLength(); |
---|
| 115 | minute += modf(hour , &hour) * c.getHourLength(); |
---|
| 116 | second += modf(minute, &minute) * c.getMinuteLength(); |
---|
[219] | 117 | |
---|
[648] | 118 | // Simplify second, minute, day and year |
---|
| 119 | double remain; |
---|
| 120 | remain = fmod(second, c.getMinuteLength()); |
---|
| 121 | minute += (second - remain) / c.getMinuteLength(); |
---|
| 122 | second = remain; |
---|
[219] | 123 | |
---|
[648] | 124 | remain = fmod(minute, c.getHourLength()); |
---|
| 125 | hour += (minute - remain) / c.getHourLength(); |
---|
| 126 | minute = remain; |
---|
[219] | 127 | |
---|
[648] | 128 | remain = fmod(hour, c.getDayLength()); |
---|
| 129 | day += (hour - remain) / c.getDayLength(); |
---|
| 130 | hour = remain; |
---|
[219] | 131 | |
---|
[648] | 132 | remain = fmod(month, c.getYearLength()); |
---|
| 133 | year += (month - remain) / c.getYearLength(); |
---|
| 134 | month = remain; |
---|
| 135 | |
---|
| 136 | return *this; |
---|
[219] | 137 | } |
---|
| 138 | |
---|
| 139 | //----------------------------------------------------------------- |
---|
| 140 | |
---|
| 141 | StdString CDuration::toString(void) const |
---|
| 142 | { |
---|
[612] | 143 | StdOStringStream sout; |
---|
| 144 | bool forceOutput = true; |
---|
| 145 | |
---|
| 146 | if (year != 0.0) { forceOutput = false; sout << year << "y "; } |
---|
| 147 | if (month != 0.0) { forceOutput = false; sout << month << "mo "; } |
---|
| 148 | if (day != 0.0) { forceOutput = false; sout << day << "d "; } |
---|
| 149 | if (hour != 0.0) { forceOutput = false; sout << hour << "h "; } |
---|
| 150 | if (minute != 0.0) { forceOutput = false; sout << minute << "mi "; } |
---|
| 151 | if (second != 0.0) { forceOutput = false; sout << second << "s "; } |
---|
| 152 | if (timestep != 0.0 || forceOutput) { sout << timestep << "ts "; } |
---|
| 153 | |
---|
| 154 | // Remove the trailing space |
---|
| 155 | StdString strOut = sout.str(); |
---|
| 156 | return strOut.erase(strOut.size() - 1); |
---|
[219] | 157 | } |
---|
| 158 | |
---|
[612] | 159 | StdString CDuration::toStringUDUnits(void) const |
---|
| 160 | { |
---|
| 161 | if (timestep != 0.0) |
---|
| 162 | ERROR("StdString CDuration::toStringUDUnits(void) const", |
---|
| 163 | "Impossible to convert a duration to string using UDUnits when a timestep is set."); |
---|
| 164 | |
---|
| 165 | StdOStringStream sout; |
---|
| 166 | bool forceOutput = true; |
---|
| 167 | |
---|
| 168 | if (year != 0.0) { forceOutput = false; sout << year << " yr "; } |
---|
| 169 | if (month != 0.0) { forceOutput = false; sout << month << " month "; } |
---|
| 170 | if (day != 0.0) { forceOutput = false; sout << day << " d "; } |
---|
| 171 | if (hour != 0.0) { forceOutput = false; sout << hour << " h "; } |
---|
| 172 | if (minute != 0.0) { forceOutput = false; sout << minute << " min "; } |
---|
| 173 | if (second != 0.0 || forceOutput) { sout << second << " s "; } |
---|
| 174 | |
---|
| 175 | // Remove the trailing space |
---|
| 176 | StdString strOut = sout.str(); |
---|
| 177 | return strOut.erase(strOut.size() - 1); |
---|
| 178 | } |
---|
| 179 | |
---|
[219] | 180 | CDuration CDuration::FromString(const StdString & str) |
---|
| 181 | { |
---|
[424] | 182 | CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; |
---|
[219] | 183 | StdIStringStream iss(str); iss >> dr; |
---|
[579] | 184 | return dr; |
---|
[219] | 185 | } |
---|
| 186 | |
---|
| 187 | ///--------------------------------------------------------------- |
---|
[335] | 188 | } // namespace xios |
---|
[219] | 189 | |
---|