[219] | 1 | #include "duration.hpp" |
---|
| 2 | #include "date.hpp" |
---|
| 3 | #include "calendar.hpp" |
---|
[550] | 4 | #include "calendar_util.hpp" |
---|
[219] | 5 | |
---|
[335] | 6 | namespace xios |
---|
[219] | 7 | { |
---|
| 8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
[550] | 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 }; |
---|
[219] | 18 | |
---|
| 19 | ///--------------------------------------------------------------- |
---|
| 20 | |
---|
[550] | 21 | CDuration& CDuration::operator=(const CDuration& duration) |
---|
[219] | 22 | { |
---|
[550] | 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; |
---|
[219] | 26 | } |
---|
| 27 | |
---|
[550] | 28 | StdOStream& operator<<(StdOStream& out, const CDuration& duration) |
---|
[219] | 29 | { |
---|
[550] | 30 | StdOStringStream sout; |
---|
| 31 | bool forceOutput = true; |
---|
[219] | 32 | |
---|
[550] | 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; |
---|
[219] | 45 | } |
---|
| 46 | |
---|
[539] | 47 | StdIStream& operator>>(StdIStream& in , CDuration& duration) |
---|
[219] | 48 | { |
---|
[539] | 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; |
---|
[219] | 74 | } |
---|
[539] | 75 | case 't': |
---|
| 76 | { |
---|
| 77 | in >> c; |
---|
| 78 | if (c == 's') duration.timestep = v; |
---|
| 79 | else invalidUnit = true; |
---|
| 80 | break; |
---|
| 81 | } |
---|
| 82 | default: |
---|
[550] | 83 | invalidUnit = true; |
---|
[539] | 84 | break; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if (invalidUnit) |
---|
| 88 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
| 89 | << "Bad duration format: invalid unit, unexpected '" << c << "' character."); |
---|
[550] | 90 | } while (in.peek() != EOF); // check whether there is a next character to read |
---|
[539] | 91 | |
---|
| 92 | return in; |
---|
[219] | 93 | } |
---|
| 94 | |
---|
| 95 | //----------------------------------------------------------------- |
---|
| 96 | |
---|
| 97 | bool CDuration::isNone(void) const |
---|
| 98 | { |
---|
[550] | 99 | return (*this == NoneDu); |
---|
[219] | 100 | } |
---|
| 101 | |
---|
| 102 | //----------------------------------------------------------------- |
---|
[550] | 103 | |
---|
| 104 | CDuration& CDuration::solveTimeStep(const CCalendar& c) |
---|
[436] | 105 | { |
---|
[550] | 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 | } |
---|
[448] | 115 | |
---|
[550] | 116 | CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/) |
---|
[219] | 117 | { |
---|
[550] | 118 | return c.resolve(*this, noNegativeTime); |
---|
[219] | 119 | } |
---|
| 120 | |
---|
| 121 | //----------------------------------------------------------------- |
---|
| 122 | |
---|
| 123 | StdString CDuration::toString(void) const |
---|
| 124 | { |
---|
[550] | 125 | StdOStringStream oss; oss << *this; |
---|
| 126 | return oss.str(); |
---|
[219] | 127 | } |
---|
| 128 | |
---|
[550] | 129 | CDuration CDuration::FromString(const StdString& str) |
---|
[219] | 130 | { |
---|
[550] | 131 | CDuration dr = NoneDu; |
---|
| 132 | StdIStringStream iss(str); iss >> dr; |
---|
| 133 | return dr; |
---|
[219] | 134 | } |
---|
[335] | 135 | } // namespace xios |
---|
[219] | 136 | |
---|