source: XIOS/dev/common/src/calendar_util.cpp @ 1512

Last change on this file since 1512 was 266, checked in by hozdoba, 13 years ago

Corrections après tests sur titane

File size: 6.7 KB
Line 
1#include "calendar_util.hpp"
2
3namespace xmlioserver
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); dtt.addMonth (signVal);
76
77         for(; c.getMonthLength(dtt) < fabs(drr.day); dtt.addMonth (signVal))
78         { drr.day -= signVal * c.getMonthLength(dtt); drr.month += signVal; }
79
80         day += dt.getDay() + drr.day;
81         if (day <  0) { drr.month --; day += c.getMonthLength(dtt); }
82         if (day >= c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << ProblÚme ici
83         if (day == 0){ day = c.getMonthLength(dtt); drr.month --; }
84         
85         drr.resolve(dt.getRelCalendar());
86
87         // Ajustement des années en fonction des mois.
88         month += dt.getMonth() + drr.month;
89         if (month <  0) { drr.year --; month += c.getYearLength(); }
90         if (month >  c.getYearLength()) { drr.year ++; month -= c.getYearLength(); }
91         if (month == 0){ month = c.getYearLength(); drr.year--; }
92
93         year += dt.getYear() + drr.year;
94
95         return (CDate(dt.getRelCalendar(), year, month, day, hour, minute, second));
96      }
97
98      CDate operator-(const CDate & dt, const CDuration & dr) { return (dt + (-dr)); }
99
100      //-----------------------------------------------------------------
101
102      CDuration operator-(const CDate & dt0, const CDate & dt1)
103      {
104         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
105         CDuration dur =
106         { dt0.getYear() - dt1.getYear(), dt0.getMonth()  - dt1.getMonth() , dt0.getDay()    - dt1.getDay(),
107           dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() };
108         return (dur.resolve(dt0.getRelCalendar()));
109      }
110
111      //-----------------------------------------------------------------
112
113      /// Les opérateurs de comparaison. (Non testés pour le moment)
114      bool operator==(const CDate& dt0, const CDate& dt1)
115      {
116         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
117         return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
118                 (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
119      }
120
121      bool operator< (const CDate& dt0, const CDate& dt1)
122      {
123         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
124         if   (dt0.getYear()  < dt1.getYear())
125         { 
126            return true;
127         }
128         else if (dt0.getYear() == dt1.getYear())
129         { 
130            if   (dt0.getMonth()  < dt1.getMonth())
131            {
132               return true;
133            }
134            else if (dt0.getMonth() == dt1.getMonth())
135            {
136               if   (dt0.getDay()  < dt1.getDay())
137               {
138                   return true;
139               }
140               else if (dt0.getDay() == dt1.getDay())
141               {
142                  if    (dt0.getHour()  < dt1.getHour())
143                  {
144                     return true;
145                  }
146                  else if (dt0.getHour() == dt1.getHour())
147                  { 
148                     if   (dt0.getMinute()  < dt1.getMinute())
149                     {
150                        return true;
151                     }
152                     else if (dt0.getMinute() == dt1.getMinute())
153                     {
154                        if (dt0.getSecond() < dt1.getSecond())
155                           return true;
156                     }
157                  }
158               }
159            }
160         }
161         return false;
162      }
163
164      //-----------------------------------------------------------------
165
166      bool operator!=(const CDate & dt0, const CDate & dt1){ return !(dt1 == dt0); }
167      bool operator> (const CDate & dt0, const CDate & dt1){ return (dt1 < dt0); }
168      bool operator>=(const CDate & dt0, const CDate & dt1){ return ((dt0 > dt1) || (dt1 == dt0)); }
169      bool operator<=(const CDate & dt0, const CDate & dt1){ return ((dt0 < dt1) || (dt1 == dt0)); }
170
171      ///----------------------------------------------------------------
172
173   } // namespace date
174} // namespace xmlioserver
175
176
177
178
179
180
Note: See TracBrowser for help on using the repository browser.