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

Last change on this file since 423 was 423, checked in by ymipsl, 11 years ago

Bug fix in calendar

YM

File size: 6.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;
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;
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;
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         return (dur);
44      }
45
46      //-----------------------------------------------------------------
47
48      CDate operator+(const CDate & dt, const CDuration & dr)
49      {
50         CDuration drr (dr);
51         int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
52         const CCalendar & c = dt.getRelCalendar();
53
54         drr.resolve(dt.getRelCalendar());
55
56         // Ajustement des minutes par rapport aux secondes.
57         second += dt.getSecond() + drr.second;
58         if (second <  0) { minute --; second += c.getMinuteLength(); }
59         if (second >= c.getMinuteLength()) { minute ++; second -= c.getMinuteLength(); }
60
61         // Ajustement des heures en fonction des minutes.
62         minute += dt.getMinute() + drr.minute;
63         if (minute < 0) { hour --; minute += c.getHourLength(); }
64         if (minute >= c.getHourLength()) { hour ++; minute -= c.getHourLength(); }
65
66         // Ajustement des jours en fonction des heures.
67         hour += dt.getHour() + drr.hour;
68         if (hour <  0) { drr.day --; hour += c.getDayLength(); }
69         if (hour >= c.getDayLength()) { drr.day ++; hour -= c.getDayLength(); }
70
71         // Ajustement des mois en fonction des jours.
72         CDate dtt(dt); 
73         drr.day+=dtt.getDay()-1 ;
74         dtt.setDay(1) ;         
75
76         if ( drr.day >= 0 )
77         {
78           for(; c.getMonthLength(dtt) <= drr.day; dtt.addMonth (1))
79           { drr.day -=  c.getMonthLength(dtt); drr.month += 1 ; }
80
81           day = drr.day+1 ;
82         }
83         else 
84         {
85           dtt.addMonth(-1) ;
86           for(; c.getMonthLength(dtt) < -drr.day; dtt.addMonth (-1))
87           { drr.day+=c.getMonthLength(dtt) ; drr.month-=1 ; }
88           day=c.getMonthLength(dtt)+drr.day+1 ;
89         }
90           
91/*
92         if (day <  0) { drr.month --; day += c.getMonthLength(dtt); }
93         if (day > c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << ProblÚme ici
94         if (day == 0){ day = c.getMonthLength(dtt); drr.month --; }
95*/         
96         drr.resolve(dt.getRelCalendar());
97
98         // Ajustement des années en fonction des mois.
99         month += dt.getMonth() + drr.month;
100         if (month <  0) { drr.year --; month += c.getYearLength(); }
101         if (month >  c.getYearLength()) { drr.year ++; month -= c.getYearLength(); }
102         if (month == 0){ month = c.getYearLength(); drr.year--; }
103
104         year += dt.getYear() + drr.year;
105
106         return (CDate(dt.getRelCalendar(), year, month, day, hour, minute, second));
107      }
108
109      CDate operator-(const CDate & dt, const CDuration & dr) { return (dt + (-dr)); }
110
111      //-----------------------------------------------------------------
112
113      CDuration operator-(const CDate & dt0, const CDate & dt1)
114      {
115         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
116         CDuration dur =
117         { dt0.getYear() - dt1.getYear(), dt0.getMonth()  - dt1.getMonth() , dt0.getDay()    - dt1.getDay(),
118           dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() };
119         return (dur.resolve(dt0.getRelCalendar()));
120      }
121
122      //-----------------------------------------------------------------
123
124      /// Les opérateurs de comparaison. (Non testés pour le moment)
125      bool operator==(const CDate& dt0, const CDate& dt1)
126      {
127         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
128         return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
129                 (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
130      }
131
132      bool operator< (const CDate& dt0, const CDate& dt1)
133      {
134         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
135         if   (dt0.getYear()  < dt1.getYear())
136         { 
137            return true;
138         }
139         else if (dt0.getYear() == dt1.getYear())
140         { 
141            if   (dt0.getMonth()  < dt1.getMonth())
142            {
143               return true;
144            }
145            else if (dt0.getMonth() == dt1.getMonth())
146            {
147               if   (dt0.getDay()  < dt1.getDay())
148               {
149                   return true;
150               }
151               else if (dt0.getDay() == dt1.getDay())
152               {
153                  if    (dt0.getHour()  < dt1.getHour())
154                  {
155                     return true;
156                  }
157                  else if (dt0.getHour() == dt1.getHour())
158                  { 
159                     if   (dt0.getMinute()  < dt1.getMinute())
160                     {
161                        return true;
162                     }
163                     else if (dt0.getMinute() == dt1.getMinute())
164                     {
165                        if (dt0.getSecond() < dt1.getSecond())
166                           return true;
167                     }
168                  }
169               }
170            }
171         }
172         return false;
173      }
174
175      //-----------------------------------------------------------------
176
177      bool operator!=(const CDate & dt0, const CDate & dt1){ return !(dt1 == dt0); }
178      bool operator> (const CDate & dt0, const CDate & dt1){ return (dt1 < dt0); }
179      bool operator>=(const CDate & dt0, const CDate & dt1){ return ((dt0 > dt1) || (dt1 == dt0)); }
180      bool operator<=(const CDate & dt0, const CDate & dt1){ return ((dt0 < dt1) || (dt1 == dt0)); }
181
182      ///----------------------------------------------------------------
183
184} // namespace xios
185
186
187
188
189
190
Note: See TracBrowser for help on using the repository browser.