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