New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
duration.cpp in vendors/XIOS/current/src – NEMO

source: vendors/XIOS/current/src/duration.cpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1#include "duration.hpp"
2#include "date.hpp"
3#include "calendar.hpp"
4
5namespace xios
6{
7      /// ////////////////////// Définitions ////////////////////// ///
8      const CDuration Year   = {1.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},
10                      Week   = {0.0, 0.0, 7.0, 0.0, 0.0, 0.0},
11                      Day    = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0},
12                      Hour   = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0},
13                      Minute = {0.0, 0.0, 0.0, 0.0, 1.0, 0.0},
14                      Second = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0},
15                      NoneDu = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
16
17      ///---------------------------------------------------------------
18
19      CDuration & CDuration::operator=(const CDuration & duration)
20      {
21         year = duration.year;  month  = duration.month ; day    = duration.day;
22         hour = duration.hour;  minute = duration.minute; second = duration.second;
23         return (*this);
24      }
25
26      StdOStream & operator<<(StdOStream & out, const CDuration & duration)
27      {
28         StdOStringStream sout;
29         bool testValue = true;
30         if(duration.year   != 0.0) { testValue = false; sout << duration.year   << "y " ; }
31         if(duration.month  != 0.0) { testValue = false; sout << duration.month  << "mo "; }
32         if(duration.day    != 0.0) { testValue = false; sout << duration.day    << "d " ; }
33         if(duration.hour   != 0.0) { testValue = false; sout << duration.hour   << "h " ; }
34         if(duration.minute != 0.0) { testValue = false; sout << duration.minute << "mi "; }
35         if(duration.second != 0.0 || testValue)       { sout << duration.second << "s " ; }
36
37         // << suppression de l'espace en fin de chaîne.
38         out << (sout.str().substr(0, sout.str().size()-1));
39         return (out);
40      }
41
42      StdIStream & operator>>(StdIStream & in , CDuration & duration)
43      {
44         duration.year = duration.month  = duration.day    =
45         duration.hour = duration.minute = duration.second = 0.0;
46         double v = 1.0;
47         char   c = '/';
48         while (!in.eof())
49         {
50               if (!(in >> v >> c)) 
51               {
52                 //DEBUG("----> Pb StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
53                 //if (in.eof())  DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
54               }
55               if (in.eof()) 
56               {
57                 //DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
58                 break ;
59               }
60               switch (c)
61               {
62                  case 'y': duration.year   = v; break;
63                  case 'd': duration.day    = v; break;
64                  case 'h': duration.hour   = v; break;
65                  case 's': duration.second = v; break;
66                  case 'm':
67                  {
68                     in >> c;
69                     if     (c == 'i') duration.minute = v;
70                     else if(c == 'o') duration.month  = v;
71                     else
72                     {
73                        StdString valc("m"); valc.append(1, c);
74                        //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
75                        break;
76                     }
77                     break;
78                  }
79                  default:
80                     StdString valc; valc.append(1, c);
81                     //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
82                     break;
83               }
84            }
85            return (in);
86      }
87
88      //-----------------------------------------------------------------
89
90      bool CDuration::isNone(void) const
91      {
92         if ((year == 0) && (month  == 0) && (day    == 0) &&
93             (hour == 0) && (minute == 0) && (second == 0))
94            return (true);
95         return (false);
96      }
97
98      //-----------------------------------------------------------------
99
100      CDuration & CDuration::resolve(const CCalendar & c)
101      {
102         // Simplification de l'écriture des minutes.
103         second += modf(minute, &minute) * (float)c.getMinuteLength();
104         minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength();
105
106         // Simplification de l'écriture des heures.
107         minute += modf(hour , &hour) * (float)c.getHourLength();
108         hour   += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength();
109
110         // Simplification de l'écriture des jours.
111         hour   += modf(day, &day) * (float)c.getDayLength();
112         day    += int(hour)  /c.getDayLength(); hour   = int(hour)%c.getDayLength();
113
114         // > Aucune équivalence jour - mois fixée par avance. //
115
116         // Simplification de l'écriture des années.
117         month  += modf(year, &year) * (float)c.getYearLength();
118         year   += int(month) /c.getYearLength(); month  = int(month)%c.getYearLength();
119         return (*this);
120      }
121
122      //-----------------------------------------------------------------
123
124      StdString CDuration::toString(void) const
125      {
126         const  CDuration & own = *this;
127         StdOStringStream oss; oss << own;
128         return (oss.str());
129      }
130
131      CDuration CDuration::FromString(const StdString & str)
132      {
133         CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
134         StdIStringStream iss(str); iss >> dr;
135         return (dr);
136      }
137
138      ///---------------------------------------------------------------
139
140
141} // namespace xios
142
Note: See TracBrowser for help on using the repository browser.