[248] | 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 = false; 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 = duration.timestep=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 | CDuration & CDuration::solveTimeStep(const CCalendar & c) |
---|
| 109 | { |
---|
| 110 | CDuration timeStep=c.getTimeStep() ; |
---|
| 111 | second +=timestep*timeStep.second ; |
---|
| 112 | minute +=timestep*timeStep.minute ; |
---|
| 113 | hour +=timestep*timeStep.hour ; |
---|
| 114 | day +=timestep*timeStep.day ; |
---|
| 115 | month +=timestep*timeStep.month ; |
---|
| 116 | year +=timestep*timeStep.year ; |
---|
| 117 | timestep = 0 ; |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | CDuration & CDuration::resolve(const CCalendar & c) |
---|
| 123 | { |
---|
| 124 | // Simplification de l'écriture des minutes. |
---|
| 125 | second += modf(minute, &minute) * (float)c.getMinuteLength(); |
---|
| 126 | minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength(); |
---|
| 127 | |
---|
| 128 | // Simplification de l'écriture des heures. |
---|
| 129 | minute += modf(hour , &hour) * (float)c.getHourLength(); |
---|
| 130 | hour += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength(); |
---|
| 131 | |
---|
| 132 | // Simplification de l'écriture des jours. |
---|
| 133 | hour += modf(day, &day) * (float)c.getDayLength(); |
---|
| 134 | day += int(hour) /c.getDayLength(); hour = int(hour)%c.getDayLength(); |
---|
| 135 | |
---|
| 136 | // > Aucune équivalence jour - mois fixée par avance. // |
---|
| 137 | |
---|
| 138 | // Simplification de l'écriture des années. |
---|
| 139 | month += modf(year, &year) * (float)c.getYearLength(); |
---|
| 140 | year += int(month) /c.getYearLength(); month = int(month)%c.getYearLength(); |
---|
| 141 | return (*this); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | //----------------------------------------------------------------- |
---|
| 145 | |
---|
| 146 | StdString CDuration::toString(void) const |
---|
| 147 | { |
---|
| 148 | const CDuration & own = *this; |
---|
| 149 | StdOStringStream oss; oss << own; |
---|
| 150 | return (oss.str()); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | CDuration CDuration::FromString(const StdString & str) |
---|
| 154 | { |
---|
| 155 | CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; |
---|
| 156 | StdIStringStream iss(str); iss >> dr; |
---|
| 157 | return (dr); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | ///--------------------------------------------------------------- |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | } // namespace xios |
---|
| 164 | |
---|