Changeset 550 for XIOS/trunk/src/duration.cpp
- Timestamp:
- 02/10/15 14:23:02 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
XIOS/trunk/src/duration.cpp
r539 r550 2 2 #include "date.hpp" 3 3 #include "calendar.hpp" 4 #include "calendar_util.hpp" 4 5 5 6 namespace xios 6 7 { 7 8 /// ////////////////////// Définitions ////////////////////// /// 8 const CDuration Year = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},9 Month = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0},10 Week = {0.0, 0.0, 7.0, 0.0, 0.0, 0.0, 0.0},11 Day = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0},12 Hour = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0},13 Minute = {0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0},14 Second = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0},15 NoneDu = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},16 TimeStep = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};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 }; 17 18 18 19 ///--------------------------------------------------------------- 19 20 20 CDuration & CDuration::operator=(const CDuration& duration)21 CDuration& CDuration::operator=(const CDuration& duration) 21 22 { 22 year = duration.year; month = duration.month ;day = duration.day;23 hour = duration.hour; minute = duration.minute; second = duration.second; timestep=duration.timestep;24 return (*this);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; 25 26 } 26 27 27 StdOStream & operator<<(StdOStream & out, const CDuration& duration)28 StdOStream& operator<<(StdOStream& out, const CDuration& duration) 28 29 { 29 StdOStringStream sout; 30 bool testValue = true; 31 if(duration.year != 0.0) { testValue = false; sout << duration.year << "y " ; } 32 if(duration.month != 0.0) { testValue = false; sout << duration.month << "mo "; } 33 if(duration.day != 0.0) { testValue = false; sout << duration.day << "d " ; } 34 if(duration.hour != 0.0) { testValue = false; sout << duration.hour << "h " ; } 35 if(duration.minute != 0.0) { testValue = false; sout << duration.minute << "mi "; } 36 if(duration.second != 0.0) { testValue = false; sout << duration.second << "s " ; } 37 if(duration.timestep != 0.0 || testValue) { sout << duration.timestep << "ts " ; } 30 StdOStringStream sout; 31 bool forceOutput = true; 38 32 39 // << suppression de l'espace en fin de chaîne. 40 out << (sout.str().substr(0, sout.str().size()-1)); 41 return (out); 33 if (duration.year != 0.0) { forceOutput = false; sout << duration.year << "y "; } 34 if (duration.month != 0.0) { forceOutput = false; sout << duration.month << "mo "; } 35 if (duration.day != 0.0) { forceOutput = false; sout << duration.day << "d "; } 36 if (duration.hour != 0.0) { forceOutput = false; sout << duration.hour << "h "; } 37 if (duration.minute != 0.0) { forceOutput = false; sout << duration.minute << "mi "; } 38 if (duration.second != 0.0) { forceOutput = false; sout << duration.second << "s "; } 39 if (duration.timestep != 0.0 || forceOutput) { sout << duration.timestep << "ts "; } 40 41 // suppression de l'espace en fin de chaîne. 42 StdString strOut = sout.str(); 43 out << strOut.erase(strOut.size() - 1); 44 return out; 42 45 } 43 46 … … 78 81 } 79 82 default: 80 invalidUnit = true; 83 invalidUnit = true; 81 84 break; 82 85 } … … 85 88 ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)", 86 89 << "Bad duration format: invalid unit, unexpected '" << c << "' character."); 87 } while (in.peek() != EOF); // check whether there is a next character to read 90 } while (in.peek() != EOF); // check whether there is a next character to read 88 91 89 92 return in; … … 94 97 bool CDuration::isNone(void) const 95 98 { 96 if ((year == 0) && (month == 0) && (day == 0) && 97 (hour == 0) && (minute == 0) && (second == 0) && (timestep == 0)) 98 return (true); 99 return (false); 99 return (*this == NoneDu); 100 100 } 101 101 102 102 //----------------------------------------------------------------- 103 CDuration & CDuration::solveTimeStep(const CCalendar & c) 103 104 CDuration& CDuration::solveTimeStep(const CCalendar& c) 104 105 { 105 CDuration timeStep=c.getTimeStep() ; 106 second +=timestep*timeStep.second ; 107 minute +=timestep*timeStep.minute ; 108 hour +=timestep*timeStep.hour ; 109 day +=timestep*timeStep.day ; 110 month +=timestep*timeStep.month ; 111 year +=timestep*timeStep.year ; 112 timestep = 0 ; 106 CDuration timeStep = c.getTimeStep(); 107 second += timestep * timeStep.second; 108 minute += timestep * timeStep.minute; 109 hour += timestep * timeStep.hour; 110 day += timestep * timeStep.day; 111 month += timestep * timeStep.month; 112 year += timestep * timeStep.year; 113 timestep = 0.0; 114 } 113 115 114 } 115 116 117 CDuration & CDuration::resolve(const CCalendar & c) 116 CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/) 118 117 { 119 // Simplification de l'écriture des minutes. 120 second += modf(minute, &minute) * (float)c.getMinuteLength(); 121 minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength(); 122 123 // Simplification de l'écriture des heures. 124 minute += modf(hour , &hour) * (float)c.getHourLength(); 125 hour += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength(); 126 127 // Simplification de l'écriture des jours. 128 hour += modf(day, &day) * (float)c.getDayLength(); 129 day += int(hour) /c.getDayLength(); hour = int(hour)%c.getDayLength(); 130 131 // > Aucune équivalence jour - mois fixée par avance. // 132 133 // Simplification de l'écriture des années. 134 month += modf(year, &year) * (float)c.getYearLength(); 135 year += int(month) /c.getYearLength(); month = int(month)%c.getYearLength(); 136 return (*this); 118 return c.resolve(*this, noNegativeTime); 137 119 } 138 120 … … 141 123 StdString CDuration::toString(void) const 142 124 { 143 const CDuration & own = *this; 144 StdOStringStream oss; oss << own; 145 return (oss.str()); 125 StdOStringStream oss; oss << *this; 126 return oss.str(); 146 127 } 147 128 148 CDuration CDuration::FromString(const StdString 129 CDuration CDuration::FromString(const StdString& str) 149 130 { 150 CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};151 152 return (dr);131 CDuration dr = NoneDu; 132 StdIStringStream iss(str); iss >> dr; 133 return dr; 153 134 } 154 155 ///---------------------------------------------------------------156 157 158 135 } // namespace xios 159 136
Note: See TracChangeset
for help on using the changeset viewer.