1 | #ifndef __XMLIO_CALENDAR_UTILS__ |
---|
2 | #define __XMLIO_CALENDAR_UTILS__ |
---|
3 | |
---|
4 | namespace XMLIOSERVER |
---|
5 | { |
---|
6 | Date Date::FromString(const string& str, const AbstractCalendar& cal) |
---|
7 | { Date dt(cal); std::istringstream iss(str); iss >> dt; return dt; } |
---|
8 | |
---|
9 | bool Date::checkDate(void) |
---|
10 | { |
---|
11 | bool retValue = true; |
---|
12 | |
---|
13 | // Vérificatio de la valeur du mois. |
---|
14 | if (month < 1) { retValue = false; month = 1; } |
---|
15 | if (month > relCalendar.getYearLength()) |
---|
16 | { retValue = false; month = relCalendar.getYearLength(); } |
---|
17 | |
---|
18 | // Vérification de la valeur du jour. |
---|
19 | if (day < 1) { retValue = false; month = 1; } |
---|
20 | if (day > relCalendar.getMonthLength(*this)) |
---|
21 | { retValue = false; day = relCalendar.getMonthLength(*this); } |
---|
22 | |
---|
23 | // Vérification de la valeur de l'heure. |
---|
24 | if (hour < 0) { retValue = false; hour = 0; } |
---|
25 | if (hour >= relCalendar.getDayLength()) |
---|
26 | { retValue = false; hour = relCalendar.getDayLength()-1; } |
---|
27 | |
---|
28 | // Vérification de la valeur des minutes. |
---|
29 | if (minute < 0) { retValue = false; minute = 0; } |
---|
30 | if (minute >= relCalendar.getHourLength()) |
---|
31 | { retValue = false; minute = relCalendar.getHourLength()-1; } |
---|
32 | |
---|
33 | // Vérification de la valeur des secondes. |
---|
34 | if (second < 0) { retValue = false; month = 0; } |
---|
35 | if (second >= relCalendar.getMinuteLength()) |
---|
36 | { retValue = false; second = relCalendar.getMinuteLength()-1; } |
---|
37 | |
---|
38 | return retValue; |
---|
39 | } |
---|
40 | |
---|
41 | Duration& Duration::resolve(const AbstractCalendar& c) |
---|
42 | { |
---|
43 | // Simplification de l'écriture des minutes. |
---|
44 | second += modf(minute, &minute) * (float)c.getMinuteLength(); |
---|
45 | minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength(); |
---|
46 | |
---|
47 | // Simplification de l'écriture des heures. |
---|
48 | minute += modf(hour , &hour) * (float)c.getHourLength(); |
---|
49 | hour += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength(); |
---|
50 | |
---|
51 | // Simplification de l'écriture des jours. |
---|
52 | hour += modf(day, &day) * (float)c.getDayLength(); |
---|
53 | day += int(hour) /c.getDayLength(); hour = int(hour)%c.getDayLength(); |
---|
54 | |
---|
55 | // > Aucune équivalence jour - mois fixée par avance. // |
---|
56 | |
---|
57 | // Simplification de l'écriture des années. |
---|
58 | month += modf(year, &year) * (float)c.getYearLength(); |
---|
59 | year += int(month) /c.getYearLength(); month = int(month)%c.getYearLength(); |
---|
60 | return (*this); |
---|
61 | } |
---|
62 | |
---|
63 | Duration operator*(const Duration& ddr, const double& scal) |
---|
64 | { |
---|
65 | Duration dur(ddr); |
---|
66 | dur.year *= scal; dur.month *= scal; dur.day *= scal; |
---|
67 | dur.hour *= scal; dur.minute *= scal; dur.second *= scal; |
---|
68 | return (dur); |
---|
69 | } |
---|
70 | |
---|
71 | Duration operator*(const double& scal, const Duration& ddr) |
---|
72 | { return (ddr * scal); } |
---|
73 | |
---|
74 | Duration operator-(const Duration& ddr, const Duration& dr) |
---|
75 | { |
---|
76 | Duration dur(ddr); |
---|
77 | dur.year -= dr.year; dur.month -= dr.month ; dur.day -= dr.day; |
---|
78 | dur.hour -= dr.hour; dur.minute -= dr.minute; dur.second -= dr.second; |
---|
79 | return (dur); |
---|
80 | } |
---|
81 | |
---|
82 | Duration operator-(const Duration& ddr) |
---|
83 | { |
---|
84 | Duration dur(ddr); |
---|
85 | dur.year = -dur.year; dur.month = -dur.month ; dur.day = -dur.day; |
---|
86 | dur.hour = -dur.hour; dur.minute = -dur.minute; dur.second = -dur.second; |
---|
87 | return (dur); |
---|
88 | } |
---|
89 | |
---|
90 | Duration operator+(const Duration& ddr, const Duration& dr) |
---|
91 | { |
---|
92 | Duration dur(ddr); |
---|
93 | dur.year += dr.year; dur.month += dr.month ; dur.day += dr.day; |
---|
94 | dur.hour += dr.hour; dur.minute += dr.minute; dur.second += dr.second; |
---|
95 | return (dur); |
---|
96 | } |
---|
97 | |
---|
98 | Date::operator Time(void) // Non vérifiée, pas optimisée ... |
---|
99 | { |
---|
100 | // Todo : Tester si la date courante est supérieure à la date initiale. |
---|
101 | Time retvalue = - relCalendar.getNbSecond(relCalendar.getInitDate()) |
---|
102 | + relCalendar.getNbSecond(*this); |
---|
103 | |
---|
104 | if ((relCalendar.getId().compare("D360") == 0) || |
---|
105 | (relCalendar.getId().compare("AllLeap") == 0) || |
---|
106 | (relCalendar.getId().compare("NoLeap") == 0)) |
---|
107 | return (retvalue + (getYear() - relCalendar.getInitDate().getYear()) * relCalendar.getYearTotalLength(*this)); |
---|
108 | |
---|
109 | for(Date _d(relCalendar.getInitDate()); _d.getYear() < getYear(); _d.setYear(_d.getYear()+1)) |
---|
110 | retvalue += relCalendar.getYearTotalLength(_d); |
---|
111 | return (retvalue); |
---|
112 | } |
---|
113 | |
---|
114 | Date operator+(const Date& dt, const Duration& dr) // Non testée. |
---|
115 | { |
---|
116 | Duration drr (dr); |
---|
117 | int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; |
---|
118 | const AbstractCalendar& c = dt.getRelCalendar(); |
---|
119 | |
---|
120 | drr.resolve(dt.getRelCalendar()); |
---|
121 | |
---|
122 | // Ajustement des minutes par rapport aux secondes. |
---|
123 | second += dt.getSecond() + drr.second; |
---|
124 | if (second < 0) { minute --; second += c.getMinuteLength(); } |
---|
125 | if (second >= c.getMinuteLength()) { minute ++; second -= c.getMinuteLength(); } |
---|
126 | |
---|
127 | // Ajustement des heures en fonction des minutes. |
---|
128 | minute += dt.getMinute() + drr.minute; |
---|
129 | if (minute < 0) { hour --; minute += c.getHourLength(); } |
---|
130 | if (minute >= c.getHourLength()) { hour ++; minute -= c.getHourLength(); } |
---|
131 | |
---|
132 | // Ajustement des jours en fonction des heures. |
---|
133 | hour += dt.getHour() + drr.hour; |
---|
134 | if (hour < 0) { drr.day --; hour += c.getDayLength(); } |
---|
135 | if (hour >= c.getDayLength()) { drr.day ++; hour -= c.getDayLength(); } |
---|
136 | |
---|
137 | // Ajustement des mois en fonction des jours. |
---|
138 | int signVal = drr.day/fabs(drr.day); |
---|
139 | Date dtt(dt); dtt.addMonth (signVal); |
---|
140 | |
---|
141 | for(; c.getMonthLength(dtt) < fabs(drr.day); dtt.addMonth (signVal)) |
---|
142 | { drr.day -= signVal * c.getMonthLength(dtt); drr.month += signVal; } |
---|
143 | |
---|
144 | day += dt.getDay() + drr.day; |
---|
145 | if (day < 0) { drr.month --; day += c.getMonthLength(dtt); } |
---|
146 | if (day >= c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << ProblÚme ici |
---|
147 | if (day == 0) day = c.getMonthLength(dtt); |
---|
148 | |
---|
149 | drr.resolve(dt.getRelCalendar()); |
---|
150 | |
---|
151 | // Ajustement des années en fonction des mois. |
---|
152 | month += dt.getMonth() + drr.month; |
---|
153 | if (month < 0) { year --; month += c.getYearLength(); } |
---|
154 | if (month >= c.getYearLength()) { year ++; month -= c.getYearLength(); } |
---|
155 | if (month == 0) month = c.getYearLength(); |
---|
156 | |
---|
157 | year += dt.getYear() + drr.year; |
---|
158 | |
---|
159 | return (Date(dt.getRelCalendar(), year, month, day, hour, minute, second)); |
---|
160 | } |
---|
161 | |
---|
162 | Date operator-(const Date& dt, const Duration& dr) { return (dt + (-dr)); } |
---|
163 | |
---|
164 | Duration operator-(const Date& dt0, const Date& dt1) |
---|
165 | { |
---|
166 | // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. |
---|
167 | Duration dur = |
---|
168 | { dt0.getYear() - dt1.getYear(), dt0.getMonth() - dt1.getMonth() , dt0.getDay() - dt1.getDay(), |
---|
169 | dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() }; |
---|
170 | return (dur.resolve(dt0.getRelCalendar())); |
---|
171 | } |
---|
172 | |
---|
173 | /// Les opérateurs de comparaison. (Non testés pour le moment) |
---|
174 | bool operator==(const Date& dt0, const Date& dt1) |
---|
175 | { |
---|
176 | // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. |
---|
177 | return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth() == dt1.getMonth()) && (dt1.getDay() == dt0.getDay()) && |
---|
178 | (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond())); |
---|
179 | } |
---|
180 | |
---|
181 | bool operator<(const Date& dt0, const Date& dt1) |
---|
182 | { |
---|
183 | // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. |
---|
184 | if (dt0.getYear() < dt1.getYear()) return true; if (dt0.getMonth() < dt1.getMonth()) return true; |
---|
185 | if (dt0.getDay() < dt1.getDay()) return true; if (dt0.getHour() < dt1.getHour()) return true; |
---|
186 | if (dt0.getMinute() < dt1.getMinute()) return true; if (dt0.getSecond() < dt1.getSecond()) return true; |
---|
187 | return false; |
---|
188 | } |
---|
189 | |
---|
190 | bool operator!=(const Date& dt0, const Date& dt1) { return !(dt1 == dt0); } |
---|
191 | bool operator> (const Date& dt0, const Date& dt1) { return (dt1 < dt0); } |
---|
192 | bool operator>=(const Date& dt0, const Date& dt1) { return ((dt0 > dt1) || (dt1 == dt0)); } |
---|
193 | bool operator<=(const Date& dt0, const Date& dt1) { return ((dt0 < dt1) || (dt1 == dt0)); } |
---|
194 | |
---|
195 | Date& AbstractCalendar::update(void) |
---|
196 | { return (getCurrentDate() = getCurrentDate() + tempo); } |
---|
197 | |
---|
198 | } // namespace XMLIOSERVER |
---|
199 | |
---|
200 | #endif //__XMLIO_CALENDAR_UTILS__ |
---|