source: XIOS/dev/branch_openmp/src/duration.cpp @ 1482

Last change on this file since 1482 was 1482, checked in by yushan, 6 years ago

Branch EP merged with Dev_cmip6 @r1481

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 6.9 KB
Line 
1#include "duration.hpp"
2#include "date.hpp"
3#include "calendar.hpp"
4#include "calendar_util.hpp"
5
6namespace xios
7{
8      /// ////////////////////// Définitions ////////////////////// ///
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 );
19
20      ///---------------------------------------------------------------
21
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
28      CDuration& CDuration::operator=(const CDuration& duration)
29      {
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;
33      }
34
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
44      StdOStream& operator<<(StdOStream& out, const CDuration& duration)
45      {
46         out << duration.toString();
47         return out;
48      }
49
50      StdIStream& operator>>(StdIStream& in , CDuration& duration)
51      {
52        duration = NoneDu;
53        double v = 1.0;
54        char   c = '/';
55        bool   invalidUnit = false;
56        CDuration sentinel = NoneDu;
57
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
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          {
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;
81            case 'm':
82            {
83              in >> c;
84              if      (c == 'i') setDuration(minute, v)
85              else if (c == 'o') setDuration(month, v)
86              else invalidUnit = true;
87              break;
88            }
89            case 't':
90            {
91              in >> c;
92              if (c == 's') setDuration(timestep, v)
93              else invalidUnit = true;
94              break;
95            }
96            default:
97              invalidUnit = true;
98              break;
99          }
100
101          if (invalidUnit)
102            ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)",
103                  << "Bad duration format: invalid unit, unexpected '" << c << "' character.");
104        } while (in.peek() != EOF); // check whether there is a next character to read
105
106#undef setDuration
107
108        return in;
109      }
110
111      //-----------------------------------------------------------------
112
113      bool CDuration::isNone(void) const
114      {
115        return (*this == NoneDu);
116      }
117
118      //-----------------------------------------------------------------
119
120      CDuration& CDuration::solveTimeStep(const CCalendar& c)
121      {
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;
130        return *this;
131      }
132
133      CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/)
134      {
135        return c.resolve(*this, noNegativeTime);
136      }
137
138      //-----------------------------------------------------------------
139
140      StdString CDuration::toString(void) const
141      {
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);
156      }
157
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
179      CDuration CDuration::FromString(const StdString& str)
180      {
181        CDuration dr = NoneDu;
182        StdIStringStream iss(str); iss >> dr;
183        return dr;
184      }
185} // namespace xios
186
Note: See TracBrowser for help on using the repository browser.