source: XIOS/dev/dev_cmip6_omp/src/date.cpp @ 1606

Last change on this file since 1606 was 1496, checked in by oabramkina, 6 years ago

Trunk and CMIP6: bugfix in addition of a month in case of a user-defined calendar.

  • 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: 11.5 KB
Line 
1#include "date.hpp"
2#include "calendar.hpp"
3#include "calendar_type.hpp"
4#include "calendar_util.hpp"
5#include <boost/date_time/gregorian/gregorian.hpp>
6#include <boost/date_time/posix_time/posix_time.hpp>
7
8using namespace boost::posix_time;
9using namespace boost::gregorian;
10
11namespace xios
12{
13      /// ////////////////////// Définitions ////////////////////// ///
14      CDate::CDate(void)
15        : relCalendar(NULL)
16        , year(0), month(1),  day(1)
17        , hour(0), minute(0), second(0)
18      {}
19
20      CDate::CDate(const CCalendar& calendar)
21         : relCalendar(&calendar)
22         , year(0), month(1),  day(1)
23         , hour(0), minute(0), second(0)
24      {}
25
26      CDate::CDate(const CCalendar& calendar,
27                   int yr, int mth, int d,
28                   int hr, int min, int sec)
29         : relCalendar(&calendar)
30         , year(yr), month(mth),  day(d)
31         , hour(hr), minute(min), second(sec)
32      {
33         if (!this->checkDate())
34         {
35            DEBUG(<< "La date initialisée a été modifiée "
36                  << "car elle était incorrecte par rapport au calendrier souhaité.");
37         }
38      }
39
40      CDate::CDate(const CDate& date)
41        : relCalendar(date.relCalendar)
42        , year(date.year), month(date.month),   day(date.day)
43        , hour(date.hour), minute(date.minute), second(date.second)
44      {
45        // Delay the verification until we get a calendar we can compare the date to
46        if (relCalendar && !checkDate())
47        {
48          DEBUG(<< "La date initialisée a été modifiée "
49                << "car elle était incorrecte par rapport au calendrier souhaité.");
50        }
51      }
52
53      CDate::~CDate(void)
54      { /* Ne rien faire de plus */ }
55
56      ///---------------------------------------------------------------
57
58      CDate& CDate::operator=(const CDate& date)
59      {
60         relCalendar = date.relCalendar;
61         year = date.year; month  = date.month; day    = date.day;
62         hour = date.hour; minute = date.minute; second = date.second;
63         return (*this);
64      }
65
66      bool CDate::operator==(const CDate& date)
67      {         
68         return (&(*relCalendar) == &(*date.relCalendar) &&
69                 year == date.year && month  == date.month  && day == date.day &&
70                 hour == date.hour && minute == date.minute && second == date.second);
71         
72      }
73
74      StdOStream& operator<<(StdOStream& out, const CDate& date)
75      {
76        std::streamsize s;
77        char c;
78
79        int width=4;
80        double maxSize=10000;
81        while (date.year>=maxSize)
82        {
83          maxSize*=10;
84          width++;
85        }
86        s = out.width(width); c = out.fill('0'); out << date.year << '-';
87
88        s = out.width(2); c = out.fill('0'); out << date.month << '-';
89        s = out.width(2); c = out.fill('0'); out << date.day << ' ';
90        s = out.width(2); c = out.fill('0'); out << date.hour << ':';
91        s = out.width(2); c = out.fill('0'); out << date.minute << ':';
92        s = out.width(2); c = out.fill('0'); out << date.second;
93
94        return out;
95      }
96
97      StdIStream& operator>>(StdIStream& in, CDate& date)
98      {
99        if (date.relCalendar)
100          date.relCalendar->parseDate(in, date);
101        else
102          CCalendar::parseDateDefault(in, date);
103
104        return in;
105      }
106
107      CDate::operator Time(void) const // Non vérifiée, pas optimisée ...
108      {
109        // This will check that a calendar was correctly associated to the date
110        const CCalendar& c = getRelCalendar();
111
112        // Todo : Tester si la date courante est supérieure à la date initiale.
113        Time t = getSecondOfYear() - c.getTimeOrigin().getSecondOfYear();
114
115        if (c.hasLeapYear())
116        {
117          for (CDate d(c.getTimeOrigin()); d.getYear() < getYear(); d.setYear(d.getYear() + 1))
118            t += c.getYearTotalLength(d);
119        }
120        else
121          t += Time(getYear() - c.getTimeOrigin().getYear()) * c.getYearTotalLength(*this);
122
123        return t;
124      }
125
126      //----------------------------------------------------------------
127
128      bool CDate::checkDate(void)
129      {
130        // This will also check that a calendar was correctly associated to the date
131        return getRelCalendar().checkDate(*this);
132      }
133
134      //----------------------------------------------------------------
135
136      int CDate::getYear  (void) const { return (this->year  ); }
137      int CDate::getMonth (void) const { return (this->month ); }
138      int CDate::getDay   (void) const { return (this->day   ); }
139      int CDate::getHour  (void) const { return (this->hour  ); }
140      int CDate::getMinute(void) const { return (this->minute); }
141      int CDate::getSecond(void) const { return (this->second); }
142
143      //----------------------------------------------------------------
144
145      const CCalendar& CDate::getRelCalendar(void) const
146      {
147        if (!this->relCalendar)
148          ERROR("const CCalendar& CDate::getRelCalendar(void) const",
149                "Invalid state: The date is not associated with any calendar.");
150        return (*this->relCalendar);
151      }
152
153      bool CDate::hasRelCalendar(void) const
154      { return (this->relCalendar != NULL); }
155
156      //----------------------------------------------------------------
157
158      /*!
159        Get the number of seconds since the beginning of the year.
160        \return the number of seconds since the beginning of the year.
161      */
162      int CDate::getSecondOfYear() const
163      {
164        CDate yearStart(*this);
165        const CCalendar& c = getRelCalendar();
166        int nbDay = 0;
167
168        for (yearStart.setMonth(1); yearStart.getMonth() < getMonth(); yearStart.setMonth(yearStart.getMonth() + 1))
169          nbDay += c.getMonthLength(yearStart);
170
171        // We need to use getDayLengthInSeconds instead of getDayLength since we might
172        // have a non-integral number of hours per day for user defined calendars
173        return ((nbDay + getDay() - 1) * c.getDayLengthInSeconds()
174                  + (getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond());
175      }
176
177      /*!
178        Get the number of days (expressed as a real number) since the beginning of the year.
179        \return the number of days (expressed as a real number) since the beginning of the year.
180      */
181      double CDate::getDayOfYear() const
182      {
183        return double(getSecondOfYear()) / getRelCalendar().getDayLengthInSeconds();
184      }
185
186      /*!
187        Get the fraction of the current year as a real number between 0 and 1.
188        \return the fraction of the current year.
189      */
190      double CDate::getFractionOfYear() const
191      {
192        return double(getSecondOfYear()) / getRelCalendar().getYearTotalLength(*this);
193      }
194
195      /*!
196        Get the number of seconds since the beginning of the day.
197        \return the number of seconds since the beginning of the day.
198      */
199      int CDate::getSecondOfDay() const
200      {
201        const CCalendar& c = getRelCalendar();
202        return ((getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond());
203      }
204
205      /*!
206        Get the fraction of the current day as a real number between 0 and 1.
207        \return the fraction of the current day.
208      */
209      double CDate::getFractionOfDay() const
210      {
211        return double(getSecondOfDay()) / getRelCalendar().getDayLengthInSeconds();
212      }
213
214      //----------------------------------------------------------------
215
216      void CDate::setYear  (int newyear)   { this->year   = newyear; }
217      void CDate::setMonth (int newmonth)  { this->month  = newmonth; }
218      void CDate::setDay   (int newday)    { this->day    = newday; }
219      void CDate::setHour  (int newhour)   { this->hour   = newhour; }
220      void CDate::setMinute(int newminute) { this->minute = newminute; }
221      void CDate::setSecond(int newsecond) { this->second = newsecond; }
222
223      void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec)
224      {
225        this->year   = yr;
226        this->month  = mth;
227        this->day    = d;
228        this->hour   = hr;
229        this->minute = min;
230        this->second = sec;
231      }
232
233      //----------------------------------------------------------------
234
235      void CDate::addMonth(int value)
236      {// Value doit être égale à 1 ou -1.
237
238        const CCalendar& c = getRelCalendar();
239        int nbMonthsPerYear = c.getYearLength();
240
241        this->month += value;
242
243        if (this->month == nbMonthsPerYear + 1) { year++; this->month = 1; }
244        if (this->month == 0)  { year--; this->month = nbMonthsPerYear; }
245      }
246
247      //----------------------------------------------------------------
248
249      bool CDate::setRelCalendar(const CCalendar& relCalendar)
250      {
251        this->relCalendar = &relCalendar;
252        return this->checkDate();
253      }
254
255      //----------------------------------------------------------------
256
257      CDate CDate::FromString(const StdString& str, const CCalendar& calendar)
258      {
259        CDate dt(calendar);
260        StdIStringStream iss(str);
261        iss >> dt;
262        return dt;
263      }
264
265      //----------------------------------------------------------------
266
267      StdString CDate::getStryyyymmdd(void) const
268      {
269        std::streamsize s;
270        char c;
271
272        ostringstream oss;
273
274        s = oss.width(4); c = oss.fill('0'); oss << year;
275        s = oss.width(2); c = oss.fill('0'); oss << month;
276        s = oss.width(2); c = oss.fill('0'); oss << day;
277
278        return oss.str();
279      }
280
281      string CDate::getStr(const string& str) const
282      {
283        ostringstream oss;
284        int level;
285
286        level=0;
287        for(string::const_iterator it=str.begin();it!=str.end();++it)
288        {
289          if (level==0)
290          {
291            if (*it=='%') level++;
292            else oss<<*it;
293          }
294          else if (level==1)
295          {
296            switch (*it)
297            {
298              case 'y' :
299                oss.width(4); oss.fill('0'); oss << year;
300                level=0;
301                break;
302              case 'm' : // month or minute
303                level++;
304                break;
305              case 'd' :
306                oss.width(2); oss.fill('0'); oss << day;
307                level=0;
308                break;
309              case 'h' :
310                oss.width(2); oss.fill('0'); oss << hour;
311                level=0;
312                break;
313              case 's' :
314                oss.width(2); oss.fill('0'); oss << second;
315                level=0;
316                break;
317              case 'S' : // seconds since time origin
318                oss.width(0); oss << Time(*this);
319                level=0;
320                break;
321              case 'D' : // days since time origin
322                oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds();
323                level=0;
324                break;
325              default :
326                oss<<'%'<<*it;
327                level=0;
328            }
329          }
330          else if (level==2)
331          {
332            switch (*it)
333            {
334              case 'o' : // month
335                oss.width(2); oss.fill('0'); oss << month;
336                level=0;
337                break;
338              case 'i' : //minute
339                oss.width(2); oss.fill('0'); oss << minute;
340                level=0;
341                break;
342              default :
343                oss<<"%m"<<*it;
344                level=0;
345            }
346          }
347        }
348        return oss.str();
349      }
350
351      StdString CDate::toString(void) const
352      {
353        StdOStringStream oss;
354        oss << *this;
355        return oss.str();
356      }
357
358      ///---------------------------------------------------------------
359
360} // namespace xios
Note: See TracBrowser for help on using the repository browser.