[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 | { |
---|
[612] | 30 | out << duration.toString(); |
---|
| 31 | return out; |
---|
[219] | 32 | } |
---|
| 33 | |
---|
[539] | 34 | StdIStream& operator>>(StdIStream& in , CDuration& duration) |
---|
[219] | 35 | { |
---|
[539] | 36 | duration = NoneDu; |
---|
| 37 | double v = 1.0; |
---|
| 38 | char c = '/'; |
---|
| 39 | bool invalidUnit = false; |
---|
| 40 | |
---|
| 41 | do |
---|
| 42 | { |
---|
| 43 | in >> v >> c; |
---|
| 44 | if (in.fail()) |
---|
| 45 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
| 46 | << "Bad duration format: impossible to read a pair (value, unit)."); |
---|
| 47 | |
---|
| 48 | switch (c) |
---|
| 49 | { |
---|
| 50 | case 'y': duration.year = v; break; |
---|
| 51 | case 'd': duration.day = v; break; |
---|
| 52 | case 'h': duration.hour = v; break; |
---|
| 53 | case 's': duration.second = v; break; |
---|
| 54 | case 'm': |
---|
| 55 | { |
---|
| 56 | in >> c; |
---|
| 57 | if (c == 'i') duration.minute = v; |
---|
| 58 | else if (c == 'o') duration.month = v; |
---|
| 59 | else invalidUnit = true; |
---|
| 60 | break; |
---|
[219] | 61 | } |
---|
[539] | 62 | case 't': |
---|
| 63 | { |
---|
| 64 | in >> c; |
---|
| 65 | if (c == 's') duration.timestep = v; |
---|
| 66 | else invalidUnit = true; |
---|
| 67 | break; |
---|
| 68 | } |
---|
| 69 | default: |
---|
[550] | 70 | invalidUnit = true; |
---|
[539] | 71 | break; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | if (invalidUnit) |
---|
| 75 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
| 76 | << "Bad duration format: invalid unit, unexpected '" << c << "' character."); |
---|
[550] | 77 | } while (in.peek() != EOF); // check whether there is a next character to read |
---|
[539] | 78 | |
---|
| 79 | return in; |
---|
[219] | 80 | } |
---|
| 81 | |
---|
| 82 | //----------------------------------------------------------------- |
---|
| 83 | |
---|
| 84 | bool CDuration::isNone(void) const |
---|
| 85 | { |
---|
[550] | 86 | return (*this == NoneDu); |
---|
[219] | 87 | } |
---|
| 88 | |
---|
| 89 | //----------------------------------------------------------------- |
---|
[550] | 90 | |
---|
| 91 | CDuration& CDuration::solveTimeStep(const CCalendar& c) |
---|
[436] | 92 | { |
---|
[550] | 93 | CDuration timeStep = c.getTimeStep(); |
---|
| 94 | second += timestep * timeStep.second; |
---|
| 95 | minute += timestep * timeStep.minute; |
---|
| 96 | hour += timestep * timeStep.hour; |
---|
| 97 | day += timestep * timeStep.day; |
---|
| 98 | month += timestep * timeStep.month; |
---|
| 99 | year += timestep * timeStep.year; |
---|
| 100 | timestep = 0.0; |
---|
[579] | 101 | return *this; |
---|
[550] | 102 | } |
---|
[448] | 103 | |
---|
[550] | 104 | CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/) |
---|
[219] | 105 | { |
---|
[550] | 106 | return c.resolve(*this, noNegativeTime); |
---|
[219] | 107 | } |
---|
| 108 | |
---|
| 109 | //----------------------------------------------------------------- |
---|
| 110 | |
---|
| 111 | StdString CDuration::toString(void) const |
---|
| 112 | { |
---|
[612] | 113 | StdOStringStream sout; |
---|
| 114 | bool forceOutput = true; |
---|
| 115 | |
---|
| 116 | if (year != 0.0) { forceOutput = false; sout << year << "y "; } |
---|
| 117 | if (month != 0.0) { forceOutput = false; sout << month << "mo "; } |
---|
| 118 | if (day != 0.0) { forceOutput = false; sout << day << "d "; } |
---|
| 119 | if (hour != 0.0) { forceOutput = false; sout << hour << "h "; } |
---|
| 120 | if (minute != 0.0) { forceOutput = false; sout << minute << "mi "; } |
---|
| 121 | if (second != 0.0) { forceOutput = false; sout << second << "s "; } |
---|
| 122 | if (timestep != 0.0 || forceOutput) { sout << timestep << "ts "; } |
---|
| 123 | |
---|
| 124 | // Remove the trailing space |
---|
| 125 | StdString strOut = sout.str(); |
---|
| 126 | return strOut.erase(strOut.size() - 1); |
---|
[219] | 127 | } |
---|
| 128 | |
---|
[612] | 129 | StdString CDuration::toStringUDUnits(void) const |
---|
| 130 | { |
---|
| 131 | if (timestep != 0.0) |
---|
| 132 | ERROR("StdString CDuration::toStringUDUnits(void) const", |
---|
| 133 | "Impossible to convert a duration to string using UDUnits when a timestep is set."); |
---|
| 134 | |
---|
| 135 | StdOStringStream sout; |
---|
| 136 | bool forceOutput = true; |
---|
| 137 | |
---|
| 138 | if (year != 0.0) { forceOutput = false; sout << year << " yr "; } |
---|
| 139 | if (month != 0.0) { forceOutput = false; sout << month << " month "; } |
---|
| 140 | if (day != 0.0) { forceOutput = false; sout << day << " d "; } |
---|
| 141 | if (hour != 0.0) { forceOutput = false; sout << hour << " h "; } |
---|
| 142 | if (minute != 0.0) { forceOutput = false; sout << minute << " min "; } |
---|
| 143 | if (second != 0.0 || forceOutput) { sout << second << " s "; } |
---|
| 144 | |
---|
| 145 | // Remove the trailing space |
---|
| 146 | StdString strOut = sout.str(); |
---|
| 147 | return strOut.erase(strOut.size() - 1); |
---|
| 148 | } |
---|
| 149 | |
---|
[550] | 150 | CDuration CDuration::FromString(const StdString& str) |
---|
[219] | 151 | { |
---|
[550] | 152 | CDuration dr = NoneDu; |
---|
| 153 | StdIStringStream iss(str); iss >> dr; |
---|
| 154 | return dr; |
---|
[219] | 155 | } |
---|
[335] | 156 | } // namespace xios |
---|
[219] | 157 | |
---|