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