source: XIOS/trunk/src/calendar_util.cpp @ 1983

Last change on this file since 1983 was 1920, checked in by yushan, 4 years ago

trunk : raise error if freq_op > output_freq

  • 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: 9.7 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    bool operator<(const CDuration& dur1, const CDuration& dur2)
48    {
49      if (dur1.year < dur2.year)
50        return true;
51      else if (dur1.year == dur2.year)
52      {
53        if (dur1.month < dur2.month)
54          return true;
55        else if (dur1.month == dur2.month)
56        {
57          if (dur1.day < dur2.day)
58            return true;
59          else if (dur1.day == dur2.day)
60          {
61            if (dur1.hour < dur2.hour)
62              return true;
63            else if (dur1.hour == dur2.hour)
64            {
65              if (dur1.minute < dur2.minute)
66                return true;
67              else if (dur1.minute == dur2.minute)
68              {
69                if (dur1.second < dur2.second)
70                  return true;
71                else if (dur1.second == dur2.second)
72                  return (dur1.timestep < dur2.timestep);
73              }
74            }
75          }
76        }
77      }
78      return false;
79    }
80
81
82    bool operator>(const CDuration& dur1, const CDuration& dur2)
83    {
84      if (dur1.year > dur2.year)
85        return true;
86      else if (dur1.year == dur2.year)
87      {
88        if (dur1.month > dur2.month)
89          return true;
90        else if (dur1.month == dur2.month)
91        {
92          if (dur1.day > dur2.day)
93            return true;
94          else if (dur1.day == dur2.day)
95          {
96            if (dur1.hour > dur2.hour)
97              return true;
98            else if (dur1.hour == dur2.hour)
99            {
100              if (dur1.minute > dur2.minute)
101                return true;
102              else if (dur1.minute == dur2.minute)
103              {
104                if (dur1.second > dur2.second)
105                  return true;
106                else if (dur1.second == dur2.second)
107                  return (dur1.timestep > dur2.timestep);
108              }
109            }
110          }
111        }
112      }
113      return false;
114    }
115
116
117
118    //-----------------------------------------------------------------
119
120    CDate operator+(const CDate& dt, const CDuration& dr)
121    {
122      CDuration drr(dr);
123      int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
124      const CCalendar& c = dt.getRelCalendar();
125      const bool calendarHasMonths = (c.getYearLength() > 0);
126
127      drr.timestep = 0;
128      if (dr.timestep)
129      {
130        if (c.getTimeStep() == NoneDu)
131          ERROR("operator+(const CDate& dt, const CDuration& dr)",
132                << "Impossible to use the timestep before it is set.");
133        drr = drr + dr.timestep * c.getTimeStep();
134      }
135
136      // Handle the time part of the date
137      drr.second += dt.getSecond();
138      drr.minute += dt.getMinute();
139      drr.hour   += dt.getHour();
140
141      if (!calendarHasMonths) // Handle the day and year immediately if there is no months
142      {
143        drr.day  += dt.getDay() - 1;
144        drr.year += dt.getYear();
145      }
146
147      drr.resolve(c, true); // Force the time to be positive
148
149      second = drr.second;
150      minute = drr.minute;
151      hour   = drr.hour;
152
153      if (calendarHasMonths)
154      {
155        // Ajustement des mois en fonction des jours.
156        CDate dtt(dt);
157        drr.day += dtt.getDay() - 1;
158        dtt.setDay(1);
159
160        if (drr.day >= 0)
161        {
162          for(; c.getMonthLength(dtt) <= drr.day; dtt.addMonth(1))
163          { drr.day -= c.getMonthLength(dtt); drr.month += 1; }
164
165          day = drr.day + 1;
166        }
167        else
168        {
169          dtt.addMonth(-1);
170          drr.month -= 1;
171          for(; c.getMonthLength(dtt) < -drr.day; dtt.addMonth(-1))
172          { drr.day += c.getMonthLength(dtt); drr.month -= 1; }
173          day = c.getMonthLength(dtt) + drr.day + 1;
174        }
175
176        drr.resolve(c);
177
178        // Ajustement des années en fonction des mois.
179        month += dt.getMonth() + drr.month;
180        if (month < 0) { drr.year--; month += c.getYearLength(); }
181        if (month > c.getYearLength()) { drr.year++; month -= c.getYearLength(); }
182        if (month == 0){ month = c.getYearLength(); drr.year--; }
183
184        year += dt.getYear() + drr.year;
185      }
186      else // if (!calendarHasMonths)
187      {
188        day   = drr.day + 1;
189        month = 1;
190        year  = drr.year;
191      }
192
193      return (CDate(c, year, month, day, hour, minute, second));
194    }
195
196    CDate operator-(const CDate& dt, const CDuration& dr) { return (dt + (-dr)); }
197
198    //-----------------------------------------------------------------
199
200    CDuration operator-(const CDate& dt0, const CDate& dt1)
201    {
202      // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
203      CDuration dur( dt0.getYear() - dt1.getYear(), dt0.getMonth()  - dt1.getMonth() , dt0.getDay()   - dt1.getDay(),
204      dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() );
205      return (dur.resolve(dt0.getRelCalendar()));
206    }
207
208    //-----------------------------------------------------------------
209
210    /// Les opérateurs de comparaison. (Non testés pour le moment)
211
212    bool operator==(const CDuration& ddr, const CDuration& dr)
213    {
214      return ((ddr.year == dr.year) && (ddr.month  == dr.month)  && (dr.day    == ddr.day) &&
215              (ddr.hour == dr.hour) && (ddr.minute == dr.minute) && (dr.second == ddr.second) &&
216              (ddr.timestep == dr.timestep));
217    }
218
219    bool operator!=(const CDuration& ddr, const CDuration& dr)
220    {
221      return !(ddr == dr);
222    }
223
224    bool operator==(const CDate& dt0, const CDate& dt1)
225    {
226      // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
227      return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
228              (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
229    }
230
231    bool operator<(const CDate& dt0, const CDate& dt1)
232    {
233      // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
234      if (dt0.getYear() < dt1.getYear())
235      {
236        return true;
237      }
238      else if (dt0.getYear() == dt1.getYear())
239      {
240        if (dt0.getMonth() < dt1.getMonth())
241        {
242          return true;
243        }
244        else if (dt0.getMonth() == dt1.getMonth())
245        {
246          if (dt0.getDay() < dt1.getDay())
247          {
248             return true;
249          }
250          else if (dt0.getDay() == dt1.getDay())
251          {
252            if (dt0.getHour() < dt1.getHour())
253            {
254              return true;
255            }
256            else if (dt0.getHour() == dt1.getHour())
257            {
258              if (dt0.getMinute() < dt1.getMinute())
259              {
260                return true;
261              }
262              else if (dt0.getMinute() == dt1.getMinute())
263              {
264                if (dt0.getSecond() < dt1.getSecond())
265                  return true;
266              }
267            }
268          }
269        }
270      }
271      return false;
272    }
273
274    //-----------------------------------------------------------------
275
276    bool operator!=(const CDate& dt0, const CDate& dt1) { return !(dt1 == dt0); }
277    bool operator> (const CDate& dt0, const CDate& dt1) { return (dt1 < dt0); }
278    bool operator>=(const CDate& dt0, const CDate& dt1) { return (dt0 > dt1 || dt1 == dt0); }
279    bool operator<=(const CDate& dt0, const CDate& dt1) { return (dt0 < dt1 || dt1 == dt0); }
280
281    ///----------------------------------------------------------------
282
283    bool DurationFakeLessComparator::operator()(const CDuration& dur1, const CDuration& dur2) const
284    {
285      if (dur1.year < dur2.year)
286        return true;
287      else if (dur1.year == dur2.year)
288      {
289        if (dur1.month < dur2.month)
290          return true;
291        else if (dur1.month == dur2.month)
292        {
293          if (dur1.day < dur2.day)
294            return true;
295          else if (dur1.day == dur2.day)
296          {
297            if (dur1.hour < dur2.hour)
298              return true;
299            else if (dur1.hour == dur2.hour)
300            {
301              if (dur1.minute < dur2.minute)
302                return true;
303              else if (dur1.minute == dur2.minute)
304              {
305                if (dur1.second < dur2.second)
306                  return true;
307                else if (dur1.second == dur2.second)
308                  return (dur1.timestep < dur2.timestep);
309              }
310            }
311          }
312        }
313      }
314      return false;
315    }
316
317    ///----------------------------------------------------------------
318
319} // namespace xios
320
321
322
323
324
325
Note: See TracBrowser for help on using the repository browser.