1 | #include "date.hpp" |
---|
2 | #include "calendar.hpp" |
---|
3 | #include "calendar_type.hpp" |
---|
4 | #include "calendar_util.hpp" |
---|
5 | #include <boost/date_time/gregorian/gregorian.hpp> |
---|
6 | #include <boost/date_time/posix_time/posix_time.hpp> |
---|
7 | |
---|
8 | using namespace boost::posix_time; |
---|
9 | using namespace boost::gregorian; |
---|
10 | |
---|
11 | namespace xios |
---|
12 | { |
---|
13 | /// ////////////////////// Définitions ////////////////////// /// |
---|
14 | CDate::CDate(void) |
---|
15 | : relCalendar(NULL) |
---|
16 | , year(0), month(1), day(1) |
---|
17 | , hour(0), minute(0), second(0) |
---|
18 | {} |
---|
19 | |
---|
20 | CDate::CDate(const CCalendar& calendar) |
---|
21 | : relCalendar(&calendar) |
---|
22 | , year(0), month(1), day(1) |
---|
23 | , hour(0), minute(0), second(0) |
---|
24 | {} |
---|
25 | |
---|
26 | CDate::CDate(const CCalendar& calendar, |
---|
27 | int yr, int mth, int d, |
---|
28 | int hr, int min, int sec) |
---|
29 | : relCalendar(&calendar) |
---|
30 | , year(yr), month(mth), day(d) |
---|
31 | , hour(hr), minute(min), second(sec) |
---|
32 | { |
---|
33 | if (!this->checkDate()) |
---|
34 | { |
---|
35 | DEBUG(<< "La date initialisée a été modifiée " |
---|
36 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | CDate::CDate(const CDate& date) |
---|
41 | : relCalendar(date.relCalendar) |
---|
42 | , year(date.year), month(date.month), day(date.day) |
---|
43 | , hour(date.hour), minute(date.minute), second(date.second) |
---|
44 | { |
---|
45 | // Delay the verification until we get a calendar we can compare the date to |
---|
46 | if (relCalendar && !checkDate()) |
---|
47 | { |
---|
48 | DEBUG(<< "La date initialisée a été modifiée " |
---|
49 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | CDate::~CDate(void) |
---|
54 | { /* Ne rien faire de plus */ } |
---|
55 | |
---|
56 | ///--------------------------------------------------------------- |
---|
57 | |
---|
58 | CDate& CDate::operator=(const CDate& date) |
---|
59 | { |
---|
60 | relCalendar = date.relCalendar; |
---|
61 | year = date.year; month = date.month; day = date.day; |
---|
62 | hour = date.hour; minute = date.minute; second = date.second; |
---|
63 | return (*this); |
---|
64 | } |
---|
65 | |
---|
66 | StdOStream& operator<<(StdOStream& out, const CDate& date) |
---|
67 | { |
---|
68 | std::streamsize s; |
---|
69 | char c; |
---|
70 | |
---|
71 | int width=4; |
---|
72 | double maxSize=10000; |
---|
73 | while (date.year>=maxSize) |
---|
74 | { |
---|
75 | maxSize*=10; |
---|
76 | width++; |
---|
77 | } |
---|
78 | s = out.width(width); c = out.fill('0'); out << date.year << '-'; |
---|
79 | |
---|
80 | s = out.width(2); c = out.fill('0'); out << date.month << '-'; |
---|
81 | s = out.width(2); c = out.fill('0'); out << date.day << ' '; |
---|
82 | s = out.width(2); c = out.fill('0'); out << date.hour << ':'; |
---|
83 | s = out.width(2); c = out.fill('0'); out << date.minute << ':'; |
---|
84 | s = out.width(2); c = out.fill('0'); out << date.second; |
---|
85 | |
---|
86 | return out; |
---|
87 | } |
---|
88 | |
---|
89 | StdIStream& operator>>(StdIStream& in, CDate& date) |
---|
90 | { |
---|
91 | if (date.relCalendar) |
---|
92 | date.relCalendar->parseDate(in, date); |
---|
93 | else |
---|
94 | CCalendar::parseDateDefault(in, date); |
---|
95 | |
---|
96 | return in; |
---|
97 | } |
---|
98 | |
---|
99 | CDate::operator Time(void) const // Non vérifiée, pas optimisée ... |
---|
100 | { |
---|
101 | // This will check that a calendar was correctly associated to the date |
---|
102 | const CCalendar& c = getRelCalendar(); |
---|
103 | |
---|
104 | // Todo : Tester si la date courante est supérieure à la date initiale. |
---|
105 | Time retvalue = getSecondOfYear() - c.getTimeOrigin().getSecondOfYear(); |
---|
106 | |
---|
107 | if (c.hasLeapYear()) |
---|
108 | { |
---|
109 | for (CDate _d(c.getTimeOrigin()); _d.getYear() < getYear(); _d.setYear(_d.getYear() + 1)) |
---|
110 | retvalue += c.getYearTotalLength(_d); |
---|
111 | } |
---|
112 | else |
---|
113 | retvalue += (getYear() - c.getTimeOrigin().getYear()) * c.getYearTotalLength(*this); |
---|
114 | |
---|
115 | |
---|
116 | return retvalue; |
---|
117 | } |
---|
118 | |
---|
119 | //---------------------------------------------------------------- |
---|
120 | |
---|
121 | bool CDate::checkDate(void) |
---|
122 | { |
---|
123 | // This will also check that a calendar was correctly associated to the date |
---|
124 | return getRelCalendar().checkDate(*this); |
---|
125 | } |
---|
126 | |
---|
127 | //---------------------------------------------------------------- |
---|
128 | |
---|
129 | int CDate::getYear (void) const { return (this->year ); } |
---|
130 | int CDate::getMonth (void) const { return (this->month ); } |
---|
131 | int CDate::getDay (void) const { return (this->day ); } |
---|
132 | int CDate::getHour (void) const { return (this->hour ); } |
---|
133 | int CDate::getMinute(void) const { return (this->minute); } |
---|
134 | int CDate::getSecond(void) const { return (this->second); } |
---|
135 | |
---|
136 | //---------------------------------------------------------------- |
---|
137 | |
---|
138 | const CCalendar& CDate::getRelCalendar(void) const |
---|
139 | { |
---|
140 | if (!this->relCalendar) |
---|
141 | ERROR("const CCalendar& CDate::getRelCalendar(void) const", |
---|
142 | "Invalid state: The date is not associated with any calendar."); |
---|
143 | return (*this->relCalendar); |
---|
144 | } |
---|
145 | |
---|
146 | bool CDate::hasRelCalendar(void) const |
---|
147 | { return (this->relCalendar != NULL); } |
---|
148 | |
---|
149 | //---------------------------------------------------------------- |
---|
150 | |
---|
151 | /*! |
---|
152 | Get the number of seconds since the beginning of the year. |
---|
153 | \return the number of seconds since the beginning of the year. |
---|
154 | */ |
---|
155 | int CDate::getSecondOfYear() const |
---|
156 | { |
---|
157 | CDate yearStart(*this); |
---|
158 | const CCalendar& c = getRelCalendar(); |
---|
159 | int nbDay = 0; |
---|
160 | |
---|
161 | for (yearStart.setMonth(1); yearStart.getMonth() < getMonth(); yearStart.setMonth(yearStart.getMonth() + 1)) |
---|
162 | nbDay += c.getMonthLength(yearStart); |
---|
163 | |
---|
164 | // We need to use getDayLengthInSeconds instead of getDayLength since we might |
---|
165 | // have a non-integral number of hours per day for user defined calendars |
---|
166 | return ((nbDay + getDay() - 1) * c.getDayLengthInSeconds() |
---|
167 | + (getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond()); |
---|
168 | } |
---|
169 | |
---|
170 | /*! |
---|
171 | Get the number of days (expressed as a real number) since the beginning of the year. |
---|
172 | \return the number of days (expressed as a real number) since the beginning of the year. |
---|
173 | */ |
---|
174 | double CDate::getDayOfYear() const |
---|
175 | { |
---|
176 | return double(getSecondOfYear()) / getRelCalendar().getDayLengthInSeconds(); |
---|
177 | } |
---|
178 | |
---|
179 | /*! |
---|
180 | Get the fraction of the current year as a real number between 0 and 1. |
---|
181 | \return the fraction of the current year. |
---|
182 | */ |
---|
183 | double CDate::getFractionOfYear() const |
---|
184 | { |
---|
185 | return double(getSecondOfYear()) / getRelCalendar().getYearTotalLength(*this); |
---|
186 | } |
---|
187 | |
---|
188 | /*! |
---|
189 | Get the number of seconds since the beginning of the day. |
---|
190 | \return the number of seconds since the beginning of the day. |
---|
191 | */ |
---|
192 | int CDate::getSecondOfDay() const |
---|
193 | { |
---|
194 | const CCalendar& c = getRelCalendar(); |
---|
195 | return ((getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond()); |
---|
196 | } |
---|
197 | |
---|
198 | /*! |
---|
199 | Get the fraction of the current day as a real number between 0 and 1. |
---|
200 | \return the fraction of the current day. |
---|
201 | */ |
---|
202 | double CDate::getFractionOfDay() const |
---|
203 | { |
---|
204 | return double(getSecondOfDay()) / getRelCalendar().getDayLengthInSeconds(); |
---|
205 | } |
---|
206 | |
---|
207 | //---------------------------------------------------------------- |
---|
208 | |
---|
209 | void CDate::setYear (int newyear) { this->year = newyear; } |
---|
210 | void CDate::setMonth (int newmonth) { this->month = newmonth; } |
---|
211 | void CDate::setDay (int newday) { this->day = newday; } |
---|
212 | void CDate::setHour (int newhour) { this->hour = newhour; } |
---|
213 | void CDate::setMinute(int newminute) { this->minute = newminute; } |
---|
214 | void CDate::setSecond(int newsecond) { this->second = newsecond; } |
---|
215 | |
---|
216 | void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec) |
---|
217 | { |
---|
218 | this->year = yr; |
---|
219 | this->month = mth; |
---|
220 | this->day = d; |
---|
221 | this->hour = hr; |
---|
222 | this->minute = min; |
---|
223 | this->second = sec; |
---|
224 | } |
---|
225 | |
---|
226 | //---------------------------------------------------------------- |
---|
227 | |
---|
228 | void CDate::addMonth(int value) |
---|
229 | {// Value doit être égale à 1 ou -1. |
---|
230 | this->month += value; |
---|
231 | if (this->month == 13) { year++; this->month = 1; } |
---|
232 | if (this->month == 0) { year--; this->month = 12; } |
---|
233 | } |
---|
234 | |
---|
235 | //---------------------------------------------------------------- |
---|
236 | |
---|
237 | bool CDate::setRelCalendar(const CCalendar& relCalendar) |
---|
238 | { |
---|
239 | this->relCalendar = &relCalendar; |
---|
240 | return this->checkDate(); |
---|
241 | } |
---|
242 | |
---|
243 | //---------------------------------------------------------------- |
---|
244 | |
---|
245 | CDate CDate::FromString(const StdString& str, const CCalendar& calendar) |
---|
246 | { |
---|
247 | CDate dt(calendar); |
---|
248 | StdIStringStream iss(str); |
---|
249 | iss >> dt; |
---|
250 | return dt; |
---|
251 | } |
---|
252 | |
---|
253 | //---------------------------------------------------------------- |
---|
254 | |
---|
255 | StdString CDate::getStryyyymmdd(void) const |
---|
256 | { |
---|
257 | std::streamsize s; |
---|
258 | char c; |
---|
259 | |
---|
260 | ostringstream oss; |
---|
261 | |
---|
262 | s = oss.width(4); c = oss.fill('0'); oss << year; |
---|
263 | s = oss.width(2); c = oss.fill('0'); oss << month; |
---|
264 | s = oss.width(2); c = oss.fill('0'); oss << day; |
---|
265 | |
---|
266 | return oss.str(); |
---|
267 | } |
---|
268 | |
---|
269 | string CDate::getStr(const string& str) const |
---|
270 | { |
---|
271 | ostringstream oss; |
---|
272 | int level; |
---|
273 | |
---|
274 | level=0; |
---|
275 | for(string::const_iterator it=str.begin();it!=str.end();++it) |
---|
276 | { |
---|
277 | if (level==0) |
---|
278 | { |
---|
279 | if (*it=='%') level++; |
---|
280 | else oss<<*it; |
---|
281 | } |
---|
282 | else if (level==1) |
---|
283 | { |
---|
284 | switch (*it) |
---|
285 | { |
---|
286 | case 'y' : |
---|
287 | oss.width(4); oss.fill('0'); oss << year; |
---|
288 | level=0; |
---|
289 | break; |
---|
290 | case 'm' : // month or minute |
---|
291 | level++; |
---|
292 | break; |
---|
293 | case 'd' : |
---|
294 | oss.width(2); oss.fill('0'); oss << day; |
---|
295 | level=0; |
---|
296 | break; |
---|
297 | case 'h' : |
---|
298 | oss.width(2); oss.fill('0'); oss << hour; |
---|
299 | level=0; |
---|
300 | break; |
---|
301 | case 's' : |
---|
302 | oss.width(2); oss.fill('0'); oss << second; |
---|
303 | level=0; |
---|
304 | break; |
---|
305 | case 'S' : // seconds since time origin |
---|
306 | oss.width(0); oss << Time(*this); |
---|
307 | level=0; |
---|
308 | break; |
---|
309 | case 'D' : // days since time origin |
---|
310 | oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds(); |
---|
311 | level=0; |
---|
312 | break; |
---|
313 | default : |
---|
314 | oss<<'%'<<*it; |
---|
315 | level=0; |
---|
316 | } |
---|
317 | } |
---|
318 | else if (level==2) |
---|
319 | { |
---|
320 | switch (*it) |
---|
321 | { |
---|
322 | case 'o' : // month |
---|
323 | oss.width(2); oss.fill('0'); oss << month; |
---|
324 | level=0; |
---|
325 | break; |
---|
326 | case 'i' : //minute |
---|
327 | oss.width(2); oss.fill('0'); oss << minute; |
---|
328 | level=0; |
---|
329 | break; |
---|
330 | default : |
---|
331 | oss<<"%m"<<*it; |
---|
332 | level=0; |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | return oss.str(); |
---|
337 | } |
---|
338 | |
---|
339 | StdString CDate::toString(void) const |
---|
340 | { |
---|
341 | StdOStringStream oss; |
---|
342 | oss << *this; |
---|
343 | return oss.str(); |
---|
344 | } |
---|
345 | |
---|
346 | ///--------------------------------------------------------------- |
---|
347 | |
---|
348 | } // namespace xios |
---|