[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 ////////////////////// /// |
---|
| 9 | |
---|
[1472] | 10 | const CDuration Year ( 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ); |
---|
| 11 | const CDuration Month ( 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 ); |
---|
| 12 | const CDuration Week ( 0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0 ); |
---|
| 13 | const CDuration Day ( 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 ); |
---|
| 14 | const CDuration Hour ( 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 ); |
---|
| 15 | const CDuration Minute ( 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 ); |
---|
| 16 | const CDuration Second ( 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); |
---|
| 17 | const CDuration TimeStep ( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 ); |
---|
| 18 | const CDuration NoneDu ( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ); |
---|
| 19 | |
---|
[219] | 20 | ///--------------------------------------------------------------- |
---|
| 21 | |
---|
[1472] | 22 | CDuration::CDuration(double y, double m, double d, double h, double min, double s, double ts) : |
---|
| 23 | year(y), month(m), day(d), hour(h), minute(min), second(s), timestep(ts) |
---|
| 24 | { |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | ///--------------------------------------------------------------- |
---|
| 28 | |
---|
[550] | 29 | CDuration& CDuration::operator=(const CDuration& duration) |
---|
[219] | 30 | { |
---|
[550] | 31 | year = duration.year; month = duration.month; day = duration.day; |
---|
| 32 | hour = duration.hour; minute = duration.minute; second = duration.second; timestep = duration.timestep; |
---|
| 33 | return *this; |
---|
[219] | 34 | } |
---|
| 35 | |
---|
[1158] | 36 | /*! |
---|
| 37 | This operation may not serve much, it's here because of the need of operator== from generic class CType<T> |
---|
| 38 | */ |
---|
| 39 | bool CDuration::operator==(const CDuration& duration) |
---|
| 40 | { |
---|
| 41 | return (year == duration.year && month == duration.month && day == duration.day && |
---|
| 42 | hour == duration.hour && minute == duration.minute && second == duration.second && timestep == duration.timestep); |
---|
| 43 | } |
---|
| 44 | |
---|
[550] | 45 | StdOStream& operator<<(StdOStream& out, const CDuration& duration) |
---|
[219] | 46 | { |
---|
[612] | 47 | out << duration.toString(); |
---|
| 48 | return out; |
---|
[219] | 49 | } |
---|
| 50 | |
---|
[539] | 51 | StdIStream& operator>>(StdIStream& in , CDuration& duration) |
---|
[219] | 52 | { |
---|
[539] | 53 | duration = NoneDu; |
---|
| 54 | double v = 1.0; |
---|
| 55 | char c = '/'; |
---|
| 56 | bool invalidUnit = false; |
---|
[1158] | 57 | CDuration sentinel = NoneDu; |
---|
[539] | 58 | |
---|
[1158] | 59 | #define setDuration(unit, value) \ |
---|
| 60 | { \ |
---|
| 61 | if (sentinel.unit) \ |
---|
| 62 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", \ |
---|
| 63 | << "Bad duration format: " #unit " has already been set."); \ |
---|
| 64 | \ |
---|
| 65 | duration.unit = value; \ |
---|
| 66 | sentinel.unit = 1.0; \ |
---|
| 67 | } |
---|
| 68 | |
---|
[539] | 69 | do |
---|
| 70 | { |
---|
| 71 | in >> v >> c; |
---|
| 72 | if (in.fail()) |
---|
| 73 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
| 74 | << "Bad duration format: impossible to read a pair (value, unit)."); |
---|
| 75 | |
---|
| 76 | switch (c) |
---|
| 77 | { |
---|
[1158] | 78 | case 'y': setDuration(year, v) break; |
---|
| 79 | case 'd': setDuration(day, v) break; |
---|
| 80 | case 'h': setDuration(hour, v) break; |
---|
| 81 | case 's': setDuration(second, v) break; |
---|
[539] | 82 | case 'm': |
---|
| 83 | { |
---|
| 84 | in >> c; |
---|
[1158] | 85 | if (c == 'i') setDuration(minute, v) |
---|
| 86 | else if (c == 'o') setDuration(month, v) |
---|
[539] | 87 | else invalidUnit = true; |
---|
| 88 | break; |
---|
[219] | 89 | } |
---|
[539] | 90 | case 't': |
---|
| 91 | { |
---|
| 92 | in >> c; |
---|
[1158] | 93 | if (c == 's') setDuration(timestep, v) |
---|
[539] | 94 | else invalidUnit = true; |
---|
| 95 | break; |
---|
| 96 | } |
---|
| 97 | default: |
---|
[550] | 98 | invalidUnit = true; |
---|
[539] | 99 | break; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if (invalidUnit) |
---|
| 103 | ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", |
---|
| 104 | << "Bad duration format: invalid unit, unexpected '" << c << "' character."); |
---|
[550] | 105 | } while (in.peek() != EOF); // check whether there is a next character to read |
---|
[539] | 106 | |
---|
[1158] | 107 | #undef setDuration |
---|
| 108 | |
---|
[539] | 109 | return in; |
---|
[219] | 110 | } |
---|
| 111 | |
---|
| 112 | //----------------------------------------------------------------- |
---|
| 113 | |
---|
| 114 | bool CDuration::isNone(void) const |
---|
| 115 | { |
---|
[550] | 116 | return (*this == NoneDu); |
---|
[219] | 117 | } |
---|
| 118 | |
---|
| 119 | //----------------------------------------------------------------- |
---|
[550] | 120 | |
---|
| 121 | CDuration& CDuration::solveTimeStep(const CCalendar& c) |
---|
[436] | 122 | { |
---|
[550] | 123 | CDuration timeStep = c.getTimeStep(); |
---|
| 124 | second += timestep * timeStep.second; |
---|
| 125 | minute += timestep * timeStep.minute; |
---|
| 126 | hour += timestep * timeStep.hour; |
---|
| 127 | day += timestep * timeStep.day; |
---|
| 128 | month += timestep * timeStep.month; |
---|
| 129 | year += timestep * timeStep.year; |
---|
| 130 | timestep = 0.0; |
---|
[579] | 131 | return *this; |
---|
[550] | 132 | } |
---|
[448] | 133 | |
---|
[550] | 134 | CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/) |
---|
[219] | 135 | { |
---|
[550] | 136 | return c.resolve(*this, noNegativeTime); |
---|
[219] | 137 | } |
---|
| 138 | |
---|
| 139 | //----------------------------------------------------------------- |
---|
| 140 | |
---|
| 141 | StdString CDuration::toString(void) const |
---|
| 142 | { |
---|
[612] | 143 | StdOStringStream sout; |
---|
| 144 | bool forceOutput = true; |
---|
| 145 | |
---|
| 146 | if (year != 0.0) { forceOutput = false; sout << year << "y "; } |
---|
| 147 | if (month != 0.0) { forceOutput = false; sout << month << "mo "; } |
---|
| 148 | if (day != 0.0) { forceOutput = false; sout << day << "d "; } |
---|
| 149 | if (hour != 0.0) { forceOutput = false; sout << hour << "h "; } |
---|
| 150 | if (minute != 0.0) { forceOutput = false; sout << minute << "mi "; } |
---|
| 151 | if (second != 0.0) { forceOutput = false; sout << second << "s "; } |
---|
| 152 | if (timestep != 0.0 || forceOutput) { sout << timestep << "ts "; } |
---|
| 153 | |
---|
| 154 | // Remove the trailing space |
---|
| 155 | StdString strOut = sout.str(); |
---|
| 156 | return strOut.erase(strOut.size() - 1); |
---|
[219] | 157 | } |
---|
| 158 | |
---|
[612] | 159 | StdString CDuration::toStringUDUnits(void) const |
---|
| 160 | { |
---|
| 161 | if (timestep != 0.0) |
---|
| 162 | ERROR("StdString CDuration::toStringUDUnits(void) const", |
---|
| 163 | "Impossible to convert a duration to string using UDUnits when a timestep is set."); |
---|
| 164 | |
---|
| 165 | StdOStringStream sout; |
---|
| 166 | bool forceOutput = true; |
---|
| 167 | |
---|
| 168 | if (year != 0.0) { forceOutput = false; sout << year << " yr "; } |
---|
| 169 | if (month != 0.0) { forceOutput = false; sout << month << " month "; } |
---|
| 170 | if (day != 0.0) { forceOutput = false; sout << day << " d "; } |
---|
| 171 | if (hour != 0.0) { forceOutput = false; sout << hour << " h "; } |
---|
| 172 | if (minute != 0.0) { forceOutput = false; sout << minute << " min "; } |
---|
| 173 | if (second != 0.0 || forceOutput) { sout << second << " s "; } |
---|
| 174 | |
---|
| 175 | // Remove the trailing space |
---|
| 176 | StdString strOut = sout.str(); |
---|
| 177 | return strOut.erase(strOut.size() - 1); |
---|
| 178 | } |
---|
| 179 | |
---|
[550] | 180 | CDuration CDuration::FromString(const StdString& str) |
---|
[219] | 181 | { |
---|
[550] | 182 | CDuration dr = NoneDu; |
---|
| 183 | StdIStringStream iss(str); iss >> dr; |
---|
| 184 | return dr; |
---|
[219] | 185 | } |
---|
[335] | 186 | } // namespace xios |
---|
[219] | 187 | |
---|