| 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 | out << duration.toString(); |
|---|
| 30 | return out; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | StdIStream & operator>>(StdIStream & in , CDuration & duration) |
|---|
| 34 | { |
|---|
| 35 | duration.year = duration.month = duration.day = |
|---|
| 36 | duration.hour = duration.minute = duration.second = duration.timestep=0.0; |
|---|
| 37 | double v = 1.0; |
|---|
| 38 | char c = '/'; |
|---|
| 39 | while (!in.eof()) |
|---|
| 40 | { |
|---|
| 41 | if (!(in >> v >> c)) |
|---|
| 42 | { |
|---|
| 43 | //DEBUG("----> Pb StdIStream & operator>>(StdIStream & in , CDuration & duration)"); |
|---|
| 44 | //if (in.eof()) DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)"); |
|---|
| 45 | } |
|---|
| 46 | if (in.eof()) |
|---|
| 47 | { |
|---|
| 48 | //DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)"); |
|---|
| 49 | break; |
|---|
| 50 | } |
|---|
| 51 | switch (c) |
|---|
| 52 | { |
|---|
| 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; |
|---|
| 57 | case 'm': |
|---|
| 58 | { |
|---|
| 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); |
|---|
| 65 | //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée."); |
|---|
| 66 | break; |
|---|
| 67 | } |
|---|
| 68 | break; |
|---|
| 69 | } |
|---|
| 70 | case 't' : |
|---|
| 71 | { |
|---|
| 72 | in >> c; |
|---|
| 73 | if (c=='s') duration.timestep = v; |
|---|
| 74 | break; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | default: |
|---|
| 78 | StdString valc; valc.append(1, c); |
|---|
| 79 | //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée."); |
|---|
| 80 | break; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | return in; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | //----------------------------------------------------------------- |
|---|
| 87 | |
|---|
| 88 | bool CDuration::isNone(void) const |
|---|
| 89 | { |
|---|
| 90 | if ((year == 0) && (month == 0) && (day == 0) && |
|---|
| 91 | (hour == 0) && (minute == 0) && (second == 0) && (timestep == 0)) |
|---|
| 92 | return true; |
|---|
| 93 | return false; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | //----------------------------------------------------------------- |
|---|
| 97 | CDuration & CDuration::solveTimeStep(const CCalendar & c) |
|---|
| 98 | { |
|---|
| 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 | } |
|---|
| 109 | |
|---|
| 110 | CDuration & CDuration::resolve(const CCalendar & c) |
|---|
| 111 | { |
|---|
| 112 | // Simplification de l'écriture des minutes. |
|---|
| 113 | second += modf(minute, &minute) * (float)c.getMinuteLength(); |
|---|
| 114 | minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength(); |
|---|
| 115 | |
|---|
| 116 | // Simplification de l'écriture des heures. |
|---|
| 117 | minute += modf(hour , &hour) * (float)c.getHourLength(); |
|---|
| 118 | hour += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength(); |
|---|
| 119 | |
|---|
| 120 | // Simplification de l'écriture des jours. |
|---|
| 121 | hour += modf(day, &day) * (float)c.getDayLength(); |
|---|
| 122 | day += int(hour) /c.getDayLength(); hour = int(hour)%c.getDayLength(); |
|---|
| 123 | |
|---|
| 124 | // > Aucune équivalence jour - mois fixée par avance. // |
|---|
| 125 | |
|---|
| 126 | // Simplification de l'écriture des années. |
|---|
| 127 | month += modf(year, &year) * (float)c.getYearLength(); |
|---|
| 128 | year += int(month) /c.getYearLength(); month = int(month)%c.getYearLength(); |
|---|
| 129 | return *this; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | //----------------------------------------------------------------- |
|---|
| 133 | |
|---|
| 134 | StdString CDuration::toString(void) const |
|---|
| 135 | { |
|---|
| 136 | StdOStringStream sout; |
|---|
| 137 | bool forceOutput = true; |
|---|
| 138 | |
|---|
| 139 | if (year != 0.0) { forceOutput = false; sout << year << "y "; } |
|---|
| 140 | if (month != 0.0) { forceOutput = false; sout << month << "mo "; } |
|---|
| 141 | if (day != 0.0) { forceOutput = false; sout << day << "d "; } |
|---|
| 142 | if (hour != 0.0) { forceOutput = false; sout << hour << "h "; } |
|---|
| 143 | if (minute != 0.0) { forceOutput = false; sout << minute << "mi "; } |
|---|
| 144 | if (second != 0.0) { forceOutput = false; sout << second << "s "; } |
|---|
| 145 | if (timestep != 0.0 || forceOutput) { sout << timestep << "ts "; } |
|---|
| 146 | |
|---|
| 147 | // Remove the trailing space |
|---|
| 148 | StdString strOut = sout.str(); |
|---|
| 149 | return strOut.erase(strOut.size() - 1); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | StdString CDuration::toStringUDUnits(void) const |
|---|
| 153 | { |
|---|
| 154 | if (timestep != 0.0) |
|---|
| 155 | ERROR("StdString CDuration::toStringUDUnits(void) const", |
|---|
| 156 | "Impossible to convert a duration to string using UDUnits when a timestep is set."); |
|---|
| 157 | |
|---|
| 158 | StdOStringStream sout; |
|---|
| 159 | bool forceOutput = true; |
|---|
| 160 | |
|---|
| 161 | if (year != 0.0) { forceOutput = false; sout << year << " yr "; } |
|---|
| 162 | if (month != 0.0) { forceOutput = false; sout << month << " month "; } |
|---|
| 163 | if (day != 0.0) { forceOutput = false; sout << day << " d "; } |
|---|
| 164 | if (hour != 0.0) { forceOutput = false; sout << hour << " h "; } |
|---|
| 165 | if (minute != 0.0) { forceOutput = false; sout << minute << " min "; } |
|---|
| 166 | if (second != 0.0 || forceOutput) { sout << second << " s "; } |
|---|
| 167 | |
|---|
| 168 | // Remove the trailing space |
|---|
| 169 | StdString strOut = sout.str(); |
|---|
| 170 | return strOut.erase(strOut.size() - 1); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | CDuration CDuration::FromString(const StdString & str) |
|---|
| 174 | { |
|---|
| 175 | CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; |
|---|
| 176 | StdIStringStream iss(str); iss >> dr; |
|---|
| 177 | return dr; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | ///--------------------------------------------------------------- |
|---|
| 181 | } // namespace xios |
|---|
| 182 | |
|---|