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