source: XIOS/dev/XIOS_DEV_CMIP6/src/date.cpp @ 1478

Last change on this file since 1478 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: 11.4 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        this->month += value;
238        if (this->month == 13) { year++; this->month = 1; }
239        if (this->month == 0)  { year--; this->month = 12; }
240      }
241
242      //----------------------------------------------------------------
243
244      bool CDate::setRelCalendar(const CCalendar& relCalendar)
245      {
246        this->relCalendar = &relCalendar;
247        return this->checkDate();
248      }
249
250      //----------------------------------------------------------------
251
252      CDate CDate::FromString(const StdString& str, const CCalendar& calendar)
253      {
254        CDate dt(calendar);
255        StdIStringStream iss(str);
256        iss >> dt;
257        return dt;
258      }
259
260      //----------------------------------------------------------------
261
262      StdString CDate::getStryyyymmdd(void) const
263      {
264        std::streamsize s;
265        char c;
266
267        ostringstream oss;
268
269        s = oss.width(4); c = oss.fill('0'); oss << year;
270        s = oss.width(2); c = oss.fill('0'); oss << month;
271        s = oss.width(2); c = oss.fill('0'); oss << day;
272
273        return oss.str();
274      }
275
276      string CDate::getStr(const string& str) const
277      {
278        ostringstream oss;
279        int level;
280
281        level=0;
282        for(string::const_iterator it=str.begin();it!=str.end();++it)
283        {
284          if (level==0)
285          {
286            if (*it=='%') level++;
287            else oss<<*it;
288          }
289          else if (level==1)
290          {
291            switch (*it)
292            {
293              case 'y' :
294                oss.width(4); oss.fill('0'); oss << year;
295                level=0;
296                break;
297              case 'm' : // month or minute
298                level++;
299                break;
300              case 'd' :
301                oss.width(2); oss.fill('0'); oss << day;
302                level=0;
303                break;
304              case 'h' :
305                oss.width(2); oss.fill('0'); oss << hour;
306                level=0;
307                break;
308              case 's' :
309                oss.width(2); oss.fill('0'); oss << second;
310                level=0;
311                break;
312              case 'S' : // seconds since time origin
313                oss.width(0); oss << Time(*this);
314                level=0;
315                break;
316              case 'D' : // days since time origin
317                oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds();
318                level=0;
319                break;
320              default :
321                oss<<'%'<<*it;
322                level=0;
323            }
324          }
325          else if (level==2)
326          {
327            switch (*it)
328            {
329              case 'o' : // month
330                oss.width(2); oss.fill('0'); oss << month;
331                level=0;
332                break;
333              case 'i' : //minute
334                oss.width(2); oss.fill('0'); oss << minute;
335                level=0;
336                break;
337              default :
338                oss<<"%m"<<*it;
339                level=0;
340            }
341          }
342        }
343        return oss.str();
344      }
345
346      StdString CDate::toString(void) const
347      {
348        StdOStringStream oss;
349        oss << *this;
350        return oss.str();
351      }
352
353      ///---------------------------------------------------------------
354
355} // namespace xios
Note: See TracBrowser for help on using the repository browser.