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