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

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

Improve CF compliance: Write the "cell_methods" metadata.

Also try to use the time units defined by the UDUnits specification.

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