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

Last change on this file since 335 was 335, checked in by ymipsl, 12 years ago

Change namespace xmlioserver -> xios

YM

File size: 6.7 KB
Line 
1#include "calendar_util.hpp"
2
3namespace xios
4{
5   namespace date
6   {
7      /// ////////////////////// Définitions ////////////////////// ///
8
9      CDuration operator*(const double & scal, const CDuration & ddr)
10      { return (ddr * scal); }
11
12      CDuration operator-(const CDuration & ddr , const CDuration & dr)
13      {
14         CDuration dur(ddr);
15         dur.year -= dr.year;  dur.month  -= dr.month ; dur.day    -= dr.day;
16         dur.hour -= dr.hour;  dur.minute -= dr.minute; dur.second -= dr.second;
17         return (dur);
18      }
19
20      CDuration operator+(const CDuration & ddr , const CDuration & dr)
21      {
22         CDuration dur(ddr);
23         dur.year += dr.year;  dur.month  += dr.month ; dur.day    += dr.day;
24         dur.hour += dr.hour;  dur.minute += dr.minute; dur.second += dr.second;
25         return (dur);
26      }
27
28      CDuration operator*(const CDuration & ddr , const double & scal)
29      {
30         CDuration dur(ddr);
31         dur.year *= scal;  dur.month  *= scal; dur.day    *= scal;
32         dur.hour *= scal;  dur.minute *= scal; dur.second *= scal;
33         return (dur);
34      }
35
36      CDuration operator-(const CDuration & ddr)
37      {
38         CDuration dur(ddr);
39         dur.year   = -dur.year;
40         dur.month  = -dur.month;
41         dur.day    = -dur.day;
42         dur.hour   = -dur.hour;
43         dur.minute = -dur.minute;
44         dur.second = -dur.second;
45         return (dur);
46      }
47
48      //-----------------------------------------------------------------
49
50      CDate operator+(const CDate & dt, const CDuration & dr)
51      {
52         CDuration drr (dr);
53         int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
54         const CCalendar & c = dt.getRelCalendar();
55
56         drr.resolve(dt.getRelCalendar());
57
58         // Ajustement des minutes par rapport aux secondes.
59         second += dt.getSecond() + drr.second;
60         if (second <  0) { minute --; second += c.getMinuteLength(); }
61         if (second >= c.getMinuteLength()) { minute ++; second -= c.getMinuteLength(); }
62
63         // Ajustement des heures en fonction des minutes.
64         minute += dt.getMinute() + drr.minute;
65         if (minute < 0) { hour --; minute += c.getHourLength(); }
66         if (minute >= c.getHourLength()) { hour ++; minute -= c.getHourLength(); }
67
68         // Ajustement des jours en fonction des heures.
69         hour += dt.getHour() + drr.hour;
70         if (hour <  0) { drr.day --; hour += c.getDayLength(); }
71         if (hour >= c.getDayLength()) { drr.day ++; hour -= c.getDayLength(); }
72
73         // Ajustement des mois en fonction des jours.
74         int signVal = (drr.day != 0) ? drr.day/fabs(drr.day) : 0;
75         CDate dtt(dt); 
76         if (signVal < 0) dtt.addMonth (signVal);
77
78         for(; c.getMonthLength(dtt) < fabs(drr.day); dtt.addMonth (signVal))
79         { drr.day -= signVal * c.getMonthLength(dtt); drr.month += signVal; }
80
81         day += dt.getDay() + drr.day;
82         if (day <  0) { drr.month --; day += c.getMonthLength(dtt); }
83         if (day >= c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << ProblÚme ici
84         if (day == 0){ day = c.getMonthLength(dtt); drr.month --; }
85         
86         drr.resolve(dt.getRelCalendar());
87
88         // Ajustement des années en fonction des mois.
89         month += dt.getMonth() + drr.month;
90         if (month <  0) { drr.year --; month += c.getYearLength(); }
91         if (month >  c.getYearLength()) { drr.year ++; month -= c.getYearLength(); }
92         if (month == 0){ month = c.getYearLength(); drr.year--; }
93
94         year += dt.getYear() + drr.year;
95
96         return (CDate(dt.getRelCalendar(), year, month, day, hour, minute, second));
97      }
98
99      CDate operator-(const CDate & dt, const CDuration & dr) { return (dt + (-dr)); }
100
101      //-----------------------------------------------------------------
102
103      CDuration operator-(const CDate & dt0, const CDate & dt1)
104      {
105         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
106         CDuration dur =
107         { dt0.getYear() - dt1.getYear(), dt0.getMonth()  - dt1.getMonth() , dt0.getDay()    - dt1.getDay(),
108           dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() };
109         return (dur.resolve(dt0.getRelCalendar()));
110      }
111
112      //-----------------------------------------------------------------
113
114      /// Les opérateurs de comparaison. (Non testés pour le moment)
115      bool operator==(const CDate& dt0, const CDate& dt1)
116      {
117         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
118         return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
119                 (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
120      }
121
122      bool operator< (const CDate& dt0, const CDate& dt1)
123      {
124         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
125         if   (dt0.getYear()  < dt1.getYear())
126         { 
127            return true;
128         }
129         else if (dt0.getYear() == dt1.getYear())
130         { 
131            if   (dt0.getMonth()  < dt1.getMonth())
132            {
133               return true;
134            }
135            else if (dt0.getMonth() == dt1.getMonth())
136            {
137               if   (dt0.getDay()  < dt1.getDay())
138               {
139                   return true;
140               }
141               else if (dt0.getDay() == dt1.getDay())
142               {
143                  if    (dt0.getHour()  < dt1.getHour())
144                  {
145                     return true;
146                  }
147                  else if (dt0.getHour() == dt1.getHour())
148                  { 
149                     if   (dt0.getMinute()  < dt1.getMinute())
150                     {
151                        return true;
152                     }
153                     else if (dt0.getMinute() == dt1.getMinute())
154                     {
155                        if (dt0.getSecond() < dt1.getSecond())
156                           return true;
157                     }
158                  }
159               }
160            }
161         }
162         return false;
163      }
164
165      //-----------------------------------------------------------------
166
167      bool operator!=(const CDate & dt0, const CDate & dt1){ return !(dt1 == dt0); }
168      bool operator> (const CDate & dt0, const CDate & dt1){ return (dt1 < dt0); }
169      bool operator>=(const CDate & dt0, const CDate & dt1){ return ((dt0 > dt1) || (dt1 == dt0)); }
170      bool operator<=(const CDate & dt0, const CDate & dt1){ return ((dt0 < dt1) || (dt1 == dt0)); }
171
172      ///----------------------------------------------------------------
173
174   } // namespace date
175} // namespace xios
176
177
178
179
180
181
Note: See TracBrowser for help on using the repository browser.