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

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

Implementing a patch suggested by Rupert Nash in order to bring temporal_filter.cpp in compliance with c++98 norms.

For this, a constructor of CDuration has been added.

  • 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: 7.9 KB
Line 
1#include "calendar_util.hpp"
2
3namespace xios
4{
5    /// ////////////////////// Définitions ////////////////////// ///
6
7    CDuration operator*(const double& scal, const CDuration& ddr)
8    { return (ddr * scal); }
9
10    CDuration operator-(const CDuration& ddr, const CDuration& dr)
11    {
12      CDuration dur(ddr);
13      dur.year -= dr.year; dur.month  -= dr.month;  dur.day    -= dr.day;
14      dur.hour -= dr.hour; dur.minute -= dr.minute; dur.second -= dr.second; dur.timestep -= dr.timestep;
15      return dur;
16    }
17
18    CDuration operator+(const CDuration& ddr, const CDuration& dr)
19    {
20      CDuration dur(ddr);
21      dur.year += dr.year; dur.month  += dr.month;  dur.day    += dr.day;
22      dur.hour += dr.hour; dur.minute += dr.minute; dur.second += dr.second; dur.timestep += dr.timestep;
23      return dur;
24    }
25
26    CDuration operator*(const CDuration& ddr, const double& scal)
27    {
28      CDuration dur(ddr);
29      dur.year *= scal; dur.month  *= scal; dur.day    *= scal;
30      dur.hour *= scal; dur.minute *= scal; dur.second *= scal; dur.timestep *= scal;
31      return dur;
32    }
33
34    CDuration operator-(const CDuration& ddr)
35    {
36      CDuration dur(ddr);
37      dur.year     = -dur.year;
38      dur.month    = -dur.month;
39      dur.day      = -dur.day;
40      dur.hour     = -dur.hour;
41      dur.minute   = -dur.minute;
42      dur.second   = -dur.second;
43      dur.timestep = -dur.timestep;
44      return dur;
45    }
46
47    //-----------------------------------------------------------------
48
49    CDate operator+(const CDate& dt, const CDuration& dr)
50    {
51      CDuration drr(dr);
52      int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
53      const CCalendar& c = dt.getRelCalendar();
54      const bool calendarHasMonths = (c.getYearLength() > 0);
55
56      drr.timestep = 0;
57      if (dr.timestep)
58      {
59        if (c.getTimeStep() == NoneDu)
60          ERROR("operator+(const CDate& dt, const CDuration& dr)",
61                << "Impossible to use the timestep before it is set.");
62        drr = drr + dr.timestep * c.getTimeStep();
63      }
64
65      // Handle the time part of the date
66      drr.second += dt.getSecond();
67      drr.minute += dt.getMinute();
68      drr.hour   += dt.getHour();
69
70      if (!calendarHasMonths) // Handle the day and year immediately if there is no months
71      {
72        drr.day  += dt.getDay() - 1;
73        drr.year += dt.getYear();
74      }
75
76      drr.resolve(c, true); // Force the time to be positive
77
78      second = drr.second;
79      minute = drr.minute;
80      hour   = drr.hour;
81
82      if (calendarHasMonths)
83      {
84        // Ajustement des mois en fonction des jours.
85        CDate dtt(dt);
86        drr.day += dtt.getDay() - 1;
87        dtt.setDay(1);
88
89        if (drr.day >= 0)
90        {
91          for(; c.getMonthLength(dtt) <= drr.day; dtt.addMonth(1))
92          { drr.day -= c.getMonthLength(dtt); drr.month += 1; }
93
94          day = drr.day + 1;
95        }
96        else
97        {
98          dtt.addMonth(-1);
99          drr.month -= 1;
100          for(; c.getMonthLength(dtt) < -drr.day; dtt.addMonth(-1))
101          { drr.day += c.getMonthLength(dtt); drr.month -= 1; }
102          day = c.getMonthLength(dtt) + drr.day + 1;
103        }
104
105        drr.resolve(c);
106
107        // Ajustement des années en fonction des mois.
108        month += dt.getMonth() + drr.month;
109        if (month < 0) { drr.year--; month += c.getYearLength(); }
110        if (month > c.getYearLength()) { drr.year++; month -= c.getYearLength(); }
111        if (month == 0){ month = c.getYearLength(); drr.year--; }
112
113        year += dt.getYear() + drr.year;
114      }
115      else // if (!calendarHasMonths)
116      {
117        day   = drr.day + 1;
118        month = 1;
119        year  = drr.year;
120      }
121
122      return (CDate(c, year, month, day, hour, minute, second));
123    }
124
125    CDate operator-(const CDate& dt, const CDuration& dr) { return (dt + (-dr)); }
126
127    //-----------------------------------------------------------------
128
129    CDuration operator-(const CDate& dt0, const CDate& dt1)
130    {
131      // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
132      CDuration dur( dt0.getYear() - dt1.getYear(), dt0.getMonth()  - dt1.getMonth() , dt0.getDay()   - dt1.getDay(),
133      dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() );
134      return (dur.resolve(dt0.getRelCalendar()));
135    }
136
137    //-----------------------------------------------------------------
138
139    /// Les opérateurs de comparaison. (Non testés pour le moment)
140
141    bool operator==(const CDuration& ddr, const CDuration& dr)
142    {
143      return ((ddr.year == dr.year) && (ddr.month  == dr.month)  && (dr.day    == ddr.day) &&
144              (ddr.hour == dr.hour) && (ddr.minute == dr.minute) && (dr.second == ddr.second) &&
145              (ddr.timestep == dr.timestep));
146    }
147
148    bool operator!=(const CDuration& ddr, const CDuration& dr)
149    {
150      return !(ddr == dr);
151    }
152
153    bool operator==(const CDate& dt0, const CDate& dt1)
154    {
155      // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
156      return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
157              (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
158    }
159
160    bool operator<(const CDate& dt0, const CDate& dt1)
161    {
162      // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
163      if (dt0.getYear() < dt1.getYear())
164      {
165        return true;
166      }
167      else if (dt0.getYear() == dt1.getYear())
168      {
169        if (dt0.getMonth() < dt1.getMonth())
170        {
171          return true;
172        }
173        else if (dt0.getMonth() == dt1.getMonth())
174        {
175          if (dt0.getDay() < dt1.getDay())
176          {
177             return true;
178          }
179          else if (dt0.getDay() == dt1.getDay())
180          {
181            if (dt0.getHour() < dt1.getHour())
182            {
183              return true;
184            }
185            else if (dt0.getHour() == dt1.getHour())
186            {
187              if (dt0.getMinute() < dt1.getMinute())
188              {
189                return true;
190              }
191              else if (dt0.getMinute() == dt1.getMinute())
192              {
193                if (dt0.getSecond() < dt1.getSecond())
194                  return true;
195              }
196            }
197          }
198        }
199      }
200      return false;
201    }
202
203    //-----------------------------------------------------------------
204
205    bool operator!=(const CDate& dt0, const CDate& dt1) { return !(dt1 == dt0); }
206    bool operator> (const CDate& dt0, const CDate& dt1) { return (dt1 < dt0); }
207    bool operator>=(const CDate& dt0, const CDate& dt1) { return (dt0 > dt1 || dt1 == dt0); }
208    bool operator<=(const CDate& dt0, const CDate& dt1) { return (dt0 < dt1 || dt1 == dt0); }
209
210    ///----------------------------------------------------------------
211
212    bool DurationFakeLessComparator::operator()(const CDuration& dur1, const CDuration& dur2) const
213    {
214      if (dur1.year < dur2.year)
215        return true;
216      else if (dur1.year == dur2.year)
217      {
218        if (dur1.month < dur2.month)
219          return true;
220        else if (dur1.month == dur2.month)
221        {
222          if (dur1.day < dur2.day)
223            return true;
224          else if (dur1.day == dur2.day)
225          {
226            if (dur1.hour < dur2.hour)
227              return true;
228            else if (dur1.hour == dur2.hour)
229            {
230              if (dur1.minute < dur2.minute)
231                return true;
232              else if (dur1.minute == dur2.minute)
233              {
234                if (dur1.second < dur2.second)
235                  return true;
236                else if (dur1.second == dur2.second)
237                  return (dur1.timestep < dur2.timestep);
238              }
239            }
240          }
241        }
242      }
243      return false;
244    }
245
246    ///----------------------------------------------------------------
247
248} // namespace xios
249
250
251
252
253
254
Note: See TracBrowser for help on using the repository browser.