1 | #include "date.hpp" |
---|
2 | #include "calendar.hpp" |
---|
3 | #include "calendar_type.hpp" |
---|
4 | #include <boost/date_time/gregorian/gregorian.hpp> |
---|
5 | #include <boost/date_time/posix_time/posix_time.hpp> |
---|
6 | |
---|
7 | using namespace boost::posix_time ; |
---|
8 | using namespace boost::gregorian ; |
---|
9 | |
---|
10 | namespace xios |
---|
11 | { |
---|
12 | /// ////////////////////// Définitions ////////////////////// /// |
---|
13 | CDate::CDate(const CCalendar& calendar) |
---|
14 | : relCalendar(calendar) |
---|
15 | , year(0), month(1) , day(1) |
---|
16 | , hour(0), minute(0), second(0) |
---|
17 | { } |
---|
18 | |
---|
19 | CDate::CDate(const CCalendar& calendar, |
---|
20 | int yr, int mth, int d, |
---|
21 | int hr, int min, int sec) |
---|
22 | : relCalendar(calendar) |
---|
23 | , year(yr), month(mth) , day(d) |
---|
24 | , hour(hr), minute(min), second(sec) |
---|
25 | { |
---|
26 | if(!this->checkDate()) |
---|
27 | { |
---|
28 | DEBUG(<< "La date initialisée a été modifiée " |
---|
29 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | CDate::CDate(const CDate & date) |
---|
34 | : relCalendar(date.getRelCalendar()), |
---|
35 | year(date.year), month(date.month) , day(date.day), |
---|
36 | hour(date.hour), minute(date.minute), second(date.second) |
---|
37 | { |
---|
38 | if(!this->checkDate()) |
---|
39 | { |
---|
40 | DEBUG(<< "La date initialisée a été modifiée " |
---|
41 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | CDate::~CDate(void) |
---|
46 | { /* Ne rien faire de plus */ } |
---|
47 | |
---|
48 | ///--------------------------------------------------------------- |
---|
49 | |
---|
50 | CDate & CDate::operator=(const CDate & date) |
---|
51 | { |
---|
52 | // relCalendar = d.getRelCalendar(); << inutile si fonction bien utilisée |
---|
53 | year = date.year; month = date.month ; day = date.day; |
---|
54 | hour = date.hour; minute = date.minute; second = date.second; |
---|
55 | return (*this); |
---|
56 | } |
---|
57 | |
---|
58 | StdOStream & operator<<(StdOStream & out, const CDate & date) |
---|
59 | { |
---|
60 | std::streamsize s ; |
---|
61 | char c ; |
---|
62 | |
---|
63 | s = out.width (4); c = out.fill ('0') ; out << date.year << '-'; |
---|
64 | s = out.width (2); c = out.fill ('0') ; out << date.month << '-'; |
---|
65 | s = out.width (2); c = out.fill ('0') ; out << date.day << ' '; |
---|
66 | s = out.width (2); c = out.fill ('0') ; out << date.hour << ':'; |
---|
67 | s = out.width (2); c = out.fill ('0') ; out << date.minute << ':'; |
---|
68 | s = out.width (2); c = out.fill ('0') ; out << date.second ; |
---|
69 | |
---|
70 | return (out); |
---|
71 | } |
---|
72 | |
---|
73 | StdIStream & operator>>(StdIStream & in, CDate & date) // Non testée. |
---|
74 | { |
---|
75 | char sep = '-'; // Le caractÚre c est utilisé pour "recueillir" les séparateurs "/" et ":". |
---|
76 | char c ; |
---|
77 | |
---|
78 | in >> date.year >> c ; |
---|
79 | if (c==sep) |
---|
80 | { |
---|
81 | in >> date.month >> c ; |
---|
82 | if (c==sep) |
---|
83 | { |
---|
84 | in >> date.day ; |
---|
85 | c=in.get(); |
---|
86 | sep=' ' ; |
---|
87 | if (c==sep) |
---|
88 | { |
---|
89 | in >> date.hour >> c ; |
---|
90 | sep=':' ; |
---|
91 | if (c==sep) |
---|
92 | { |
---|
93 | in>>date.minute >> c; |
---|
94 | if (c==sep) |
---|
95 | { |
---|
96 | in>>date.second ; |
---|
97 | if(!date.checkDate()) |
---|
98 | ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)",<<"Bad date format or not conform to calendar" ); |
---|
99 | return (in); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)",<<"Bad date format or not conform to calendar" ); |
---|
106 | return (in); |
---|
107 | } |
---|
108 | |
---|
109 | CDate::operator Time(void) const // Non vérifiée, pas optimisée ... |
---|
110 | { |
---|
111 | // Todo : Tester si la date courante est supérieure à la date initiale. |
---|
112 | Time retvalue = - relCalendar.getNbSecond(relCalendar.getInitDate()) |
---|
113 | + relCalendar.getNbSecond(*this); |
---|
114 | |
---|
115 | if ((relCalendar.getId().compare("D360") == 0) || |
---|
116 | (relCalendar.getId().compare("AllLeap") == 0) || |
---|
117 | (relCalendar.getId().compare("NoLeap") == 0)) |
---|
118 | return (retvalue + (getYear() - relCalendar.getTimeOrigin().getYear()) |
---|
119 | * relCalendar.getYearTotalLength(*this)); |
---|
120 | |
---|
121 | for(CDate _d(relCalendar.getTimeOrigin()); |
---|
122 | _d.getYear() < getYear(); _d.setYear(_d.getYear()+1)) |
---|
123 | retvalue += relCalendar.getYearTotalLength(_d); |
---|
124 | return (retvalue); |
---|
125 | } |
---|
126 | |
---|
127 | //---------------------------------------------------------------- |
---|
128 | |
---|
129 | bool CDate::checkDate(void) |
---|
130 | { |
---|
131 | bool retValue = true; |
---|
132 | |
---|
133 | // Vérificatio de la valeur du mois. |
---|
134 | if (month < 1) { retValue = false; month = 1; } |
---|
135 | if (month > relCalendar.getYearLength()) |
---|
136 | { retValue = false; month = relCalendar.getYearLength(); } |
---|
137 | |
---|
138 | // Vérification de la valeur du jour. |
---|
139 | if (day < 1) { retValue = false; month = 1; } |
---|
140 | if (day > (&relCalendar)->getMonthLength(*this)) |
---|
141 | { retValue = false; day = (&relCalendar)->getMonthLength(*this); } |
---|
142 | |
---|
143 | // Vérification de la valeur de l'heure. |
---|
144 | if (hour < 0) { retValue = false; hour = 0; } |
---|
145 | if (hour >= relCalendar.getDayLength()) |
---|
146 | { retValue = false; hour = relCalendar.getDayLength()-1; } |
---|
147 | |
---|
148 | // Vérification de la valeur des minutes. |
---|
149 | if (minute < 0) { retValue = false; minute = 0; } |
---|
150 | if (minute >= relCalendar.getHourLength()) |
---|
151 | { retValue = false; minute = relCalendar.getHourLength()-1; } |
---|
152 | |
---|
153 | // Vérification de la valeur des secondes. |
---|
154 | if (second < 0) { retValue = false; month = 0; } |
---|
155 | if (second >= relCalendar.getMinuteLength()) |
---|
156 | { retValue = false; second = relCalendar.getMinuteLength()-1; } |
---|
157 | |
---|
158 | return retValue; |
---|
159 | } |
---|
160 | |
---|
161 | //---------------------------------------------------------------- |
---|
162 | |
---|
163 | int CDate::getYear (void) const { return (this->year ); } |
---|
164 | int CDate::getMonth (void) const { return (this->month ); } |
---|
165 | int CDate::getDay (void) const { return (this->day ); } |
---|
166 | int CDate::getHour (void) const { return (this->hour ); } |
---|
167 | int CDate::getMinute(void) const { return (this->minute); } |
---|
168 | int CDate::getSecond(void) const { return (this->second); } |
---|
169 | |
---|
170 | //---------------------------------------------------------------- |
---|
171 | |
---|
172 | const CCalendar & CDate::getRelCalendar(void) const |
---|
173 | { return (this->relCalendar); } |
---|
174 | |
---|
175 | //---------------------------------------------------------------- |
---|
176 | |
---|
177 | void CDate::setYear (int newyear) { this->year = newyear; } |
---|
178 | void CDate::setMonth (int newmonth) { this->month = newmonth; } |
---|
179 | void CDate::setDay (int newday) { this->day = newday; } |
---|
180 | |
---|
181 | //---------------------------------------------------------------- |
---|
182 | |
---|
183 | void CDate::addMonth (int value) |
---|
184 | {// Value doit être égale à 1 ou -1. |
---|
185 | this->month += value; |
---|
186 | if (this->month == 13) { year++; this->month = 1 ; } |
---|
187 | if (this->month == 0 ) { year--; this->month = 12; } |
---|
188 | } |
---|
189 | |
---|
190 | //---------------------------------------------------------------- |
---|
191 | |
---|
192 | CDate CDate::FromString(const StdString & str, const CCalendar & calendar) |
---|
193 | { |
---|
194 | CDate dt(calendar); |
---|
195 | StdIStringStream iss(str); |
---|
196 | iss >> dt; |
---|
197 | return dt; |
---|
198 | } |
---|
199 | |
---|
200 | //---------------------------------------------------------------- |
---|
201 | |
---|
202 | StdString CDate::getStryyyymmdd(void) const |
---|
203 | { |
---|
204 | std::streamsize s ; |
---|
205 | char c ; |
---|
206 | |
---|
207 | ostringstream oss ; |
---|
208 | |
---|
209 | s = oss.width (4); c = oss.fill ('0') ; oss << year ; |
---|
210 | s = oss.width (2); c = oss.fill ('0') ; oss << month; |
---|
211 | s = oss.width (2); c = oss.fill ('0') ; oss << day ; |
---|
212 | |
---|
213 | return oss.str(); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | string CDate::getStr(const string& str) const |
---|
218 | { |
---|
219 | ostringstream oss ; |
---|
220 | int level; |
---|
221 | |
---|
222 | level=0 ; |
---|
223 | for(string::const_iterator it=str.begin();it!=str.end();++it) |
---|
224 | { |
---|
225 | if (level==0) |
---|
226 | { |
---|
227 | if (*it=='%') level++ ; |
---|
228 | else oss<<*it ; |
---|
229 | } |
---|
230 | else if (level==1) |
---|
231 | { |
---|
232 | switch (*it) |
---|
233 | { |
---|
234 | case 'y' : |
---|
235 | oss.width (4); oss.fill ('0') ; oss << year ; |
---|
236 | level=0 ; |
---|
237 | break ; |
---|
238 | case 'm' : // month or minute |
---|
239 | level++ ; |
---|
240 | break ; |
---|
241 | case 'd' : |
---|
242 | oss.width (2); oss.fill ('0') ; oss << day ; |
---|
243 | level=0; |
---|
244 | break ; |
---|
245 | case 'h' : |
---|
246 | oss.width (2); oss.fill ('0') ; oss << hour ; |
---|
247 | level=0; |
---|
248 | break ; |
---|
249 | case 's' : |
---|
250 | oss.width (2); oss.fill ('0') ; oss << second ; |
---|
251 | level=0 ; |
---|
252 | break; |
---|
253 | default : |
---|
254 | oss<<'%'<<*it ; |
---|
255 | level=0 ; |
---|
256 | } |
---|
257 | } |
---|
258 | else if (level==2) |
---|
259 | { |
---|
260 | switch (*it) |
---|
261 | { |
---|
262 | case 'o' : // month |
---|
263 | oss.width (2); oss.fill ('0') ; oss << month ; |
---|
264 | level=0 ; |
---|
265 | break ; |
---|
266 | case 'i' : //minute |
---|
267 | oss.width (2); oss.fill ('0') ; oss << minute ; |
---|
268 | level=0 ; |
---|
269 | break ; |
---|
270 | default : |
---|
271 | oss<<"%m"<<*it ; |
---|
272 | level=0 ; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | return oss.str(); |
---|
277 | } |
---|
278 | |
---|
279 | StdString CDate::toString(void) const |
---|
280 | { |
---|
281 | StdOStringStream oss; |
---|
282 | oss << (*this); |
---|
283 | return (oss.str()); |
---|
284 | } |
---|
285 | |
---|
286 | ///--------------------------------------------------------------- |
---|
287 | |
---|
288 | } // namespace xios |
---|