source: XIOS/dev/dev_olga/src/duration.cpp @ 1168

Last change on this file since 1168 was 1158, checked in by oabramkina, 7 years ago

Two server levels: merging with trunk r1137.
There are bugs.

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