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

Last change on this file since 1627 was 1472, checked in by oabramkina, 6 years ago

Implementing a patch suggested by Rupert Nash in order to bring temporal_filter.cpp in compliance with c++98 norms.

For this, a constructor of CDuration has been added.

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