source: XIOS/trunk/src/duration.cpp @ 436

Last change on this file since 436 was 436, checked in by ymipsl, 11 years ago

bug fix :

  • freq_op is now by default of 1 timestep (1ts)
  • once operation is now working as required

YM

File size: 6.4 KB
RevLine 
[219]1#include "duration.hpp"
2#include "date.hpp"
3#include "calendar.hpp"
4
[335]5namespace xios
[219]6{
7      /// ////////////////////// Définitions ////////////////////// ///
[424]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};
[219]17
18      ///---------------------------------------------------------------
19
20      CDuration & CDuration::operator=(const CDuration & duration)
21      {
22         year = duration.year;  month  = duration.month ; day    = duration.day;
[424]23         hour = duration.hour;  minute = duration.minute; second = duration.second; timestep=duration.timestep ;
[219]24         return (*this);
25      }
26
27      StdOStream & operator<<(StdOStream & out, const CDuration & duration)
28      {
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 "; }
[436]36         if(duration.second != 0.0) { testValue = false; sout << duration.second << "s " ; }
[424]37         if(duration.timestep != 0.0 || testValue)       { sout << duration.timestep << "ts " ; }
[219]38
39         // << suppression de l'espace en fin de chaîne.
40         out << (sout.str().substr(0, sout.str().size()-1));
41         return (out);
42      }
43
44      StdIStream & operator>>(StdIStream & in , CDuration & duration)
45      {
46         duration.year = duration.month  = duration.day    =
47         duration.hour = duration.minute = duration.second = 0.0;
48         double v = 1.0;
49         char   c = '/';
50         while (!in.eof())
51         {
[266]52               if (!(in >> v >> c)) 
53               {
54                 //DEBUG("----> Pb StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
55                 //if (in.eof())  DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
56               }
57               if (in.eof()) 
58               {
59                 //DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
60                 break ;
61               }
[219]62               switch (c)
63               {
[266]64                  case 'y': duration.year   = v; break;
65                  case 'd': duration.day    = v; break;
66                  case 'h': duration.hour   = v; break;
67                  case 's': duration.second = v; break;
[219]68                  case 'm':
[266]69                  {
[219]70                     in >> c;
71                     if     (c == 'i') duration.minute = v;
72                     else if(c == 'o') duration.month  = v;
73                     else
74                     {
75                        StdString valc("m"); valc.append(1, c);
[266]76                        //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
77                        break;
[219]78                     }
[266]79                     break;
80                  }
[424]81                  case 't' :
82                  {
83                    in >> c;
84                    if (c=='s') duration.timestep = v; 
85                    break;
86                  }
87                 
[219]88                  default:
89                     StdString valc; valc.append(1, c);
[266]90                     //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
91                     break;
[219]92               }
93            }
94            return (in);
95      }
96
97      //-----------------------------------------------------------------
98
99      bool CDuration::isNone(void) const
100      {
101         if ((year == 0) && (month  == 0) && (day    == 0) &&
[424]102             (hour == 0) && (minute == 0) && (second == 0) && (timestep == 0))
[219]103            return (true);
104         return (false);
105      }
106
107      //-----------------------------------------------------------------
[436]108      CDuration & CDuration::solveTimeStep(const CCalendar & c)
109      {
110        CDuration timeStep=c.getTimeStep() ;
111        second +=timeStep.second ; 
112        minute +=timeStep.minute ; 
113        hour +=timeStep.hour ; 
114        day +=timeStep.day ; 
115        month +=timeStep.month ; 
116        year +=timeStep.year ; 
117        timestep = 0 ;
118        return (*this);
119      }
120               
121       
[219]122      CDuration & CDuration::resolve(const CCalendar & c)
123      {
124         // Simplification de l'écriture des minutes.
125         second += modf(minute, &minute) * (float)c.getMinuteLength();
126         minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength();
127
128         // Simplification de l'écriture des heures.
129         minute += modf(hour , &hour) * (float)c.getHourLength();
130         hour   += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength();
131
132         // Simplification de l'écriture des jours.
133         hour   += modf(day, &day) * (float)c.getDayLength();
134         day    += int(hour)  /c.getDayLength(); hour   = int(hour)%c.getDayLength();
135
136         // > Aucune équivalence jour - mois fixée par avance. //
137
138         // Simplification de l'écriture des années.
139         month  += modf(year, &year) * (float)c.getYearLength();
140         year   += int(month) /c.getYearLength(); month  = int(month)%c.getYearLength();
141         return (*this);
142      }
143
144      //-----------------------------------------------------------------
145
146      StdString CDuration::toString(void) const
147      {
148         const  CDuration & own = *this;
149         StdOStringStream oss; oss << own;
150         return (oss.str());
151      }
152
153      CDuration CDuration::FromString(const StdString & str)
154      {
[424]155         CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
[219]156         StdIStringStream iss(str); iss >> dr;
157         return (dr);
158      }
159
160      ///---------------------------------------------------------------
161
162
[335]163} // namespace xios
[219]164
Note: See TracBrowser for help on using the repository browser.