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

Last change on this file since 579 was 579, checked in by rlacroix, 9 years ago

Add a missing return statement to the CDuration::solveTimeStep method.

Cosmetic only, the return value was never used.

  • 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: 4.8 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      StdOStream& operator<<(StdOStream& out, const CDuration& duration)
29      {
30        StdOStringStream sout;
31        bool forceOutput = true;
32
33        if (duration.year   != 0.0) { forceOutput = false; sout << duration.year   << "y "; }
34        if (duration.month  != 0.0) { forceOutput = false; sout << duration.month  << "mo "; }
35        if (duration.day    != 0.0) { forceOutput = false; sout << duration.day    << "d "; }
36        if (duration.hour   != 0.0) { forceOutput = false; sout << duration.hour   << "h "; }
37        if (duration.minute != 0.0) { forceOutput = false; sout << duration.minute << "mi "; }
38        if (duration.second != 0.0) { forceOutput = false; sout << duration.second << "s "; }
39        if (duration.timestep != 0.0 || forceOutput)     { sout << duration.timestep << "ts "; }
40
41        // suppression de l'espace en fin de chaîne.
42        StdString strOut = sout.str();
43        out << strOut.erase(strOut.size() - 1);
44        return out;
45      }
46
47      StdIStream& operator>>(StdIStream& in , CDuration& duration)
48      {
49        duration = NoneDu;
50        double v = 1.0;
51        char   c = '/';
52        bool   invalidUnit = false;
53
54        do
55        {
56          in >> v >> c;
57          if (in.fail())
58            ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)",
59                  << "Bad duration format: impossible to read a pair (value, unit).");
60
61          switch (c)
62          {
63            case 'y': duration.year   = v; break;
64            case 'd': duration.day    = v; break;
65            case 'h': duration.hour   = v; break;
66            case 's': duration.second = v; break;
67            case 'm':
68            {
69              in >> c;
70              if      (c == 'i') duration.minute = v;
71              else if (c == 'o') duration.month  = v;
72              else invalidUnit = true;
73              break;
74            }
75            case 't':
76            {
77              in >> c;
78              if (c == 's') duration.timestep = v;
79              else invalidUnit = true;
80              break;
81            }
82            default:
83              invalidUnit = true;
84              break;
85          }
86
87          if (invalidUnit)
88            ERROR("StdIStream& operator>>(StdIStream& in , CDuration& duration)",
89                  << "Bad duration format: invalid unit, unexpected '" << c << "' character.");
90        } while (in.peek() != EOF); // check whether there is a next character to read
91
92        return in;
93      }
94
95      //-----------------------------------------------------------------
96
97      bool CDuration::isNone(void) const
98      {
99        return (*this == NoneDu);
100      }
101
102      //-----------------------------------------------------------------
103
104      CDuration& CDuration::solveTimeStep(const CCalendar& c)
105      {
106        CDuration timeStep = c.getTimeStep();
107        second += timestep * timeStep.second;
108        minute += timestep * timeStep.minute;
109        hour   += timestep * timeStep.hour;
110        day    += timestep * timeStep.day;
111        month  += timestep * timeStep.month;
112        year   += timestep * timeStep.year;
113        timestep = 0.0;
114        return *this;
115      }
116
117      CDuration& CDuration::resolve(const CCalendar& c, bool noNegativeTime /*= false*/)
118      {
119        return c.resolve(*this, noNegativeTime);
120      }
121
122      //-----------------------------------------------------------------
123
124      StdString CDuration::toString(void) const
125      {
126        StdOStringStream oss; oss << *this;
127        return oss.str();
128      }
129
130      CDuration CDuration::FromString(const StdString& str)
131      {
132        CDuration dr = NoneDu;
133        StdIStringStream iss(str); iss >> dr;
134        return dr;
135      }
136} // namespace xios
137
Note: See TracBrowser for help on using the repository browser.