[219] | 1 | #include "date.hpp" |
---|
| 2 | #include "calendar.hpp" |
---|
[334] | 3 | #include "calendar_type.hpp" |
---|
[549] | 4 | #include "calendar_util.hpp" |
---|
[316] | 5 | #include <boost/date_time/gregorian/gregorian.hpp> |
---|
| 6 | #include <boost/date_time/posix_time/posix_time.hpp> |
---|
[219] | 7 | |
---|
[549] | 8 | using namespace boost::posix_time; |
---|
| 9 | using namespace boost::gregorian; |
---|
[316] | 10 | |
---|
[335] | 11 | namespace xios |
---|
[219] | 12 | { |
---|
| 13 | /// ////////////////////// Définitions ////////////////////// /// |
---|
[532] | 14 | CDate::CDate(void) |
---|
[549] | 15 | : relCalendar(NULL) |
---|
| 16 | , year(0), month(1), day(1) |
---|
| 17 | , hour(0), minute(0), second(0) |
---|
[532] | 18 | {} |
---|
| 19 | |
---|
[334] | 20 | CDate::CDate(const CCalendar& calendar) |
---|
[532] | 21 | : relCalendar(&calendar) |
---|
[549] | 22 | , year(0), month(1), day(1) |
---|
[334] | 23 | , hour(0), minute(0), second(0) |
---|
[532] | 24 | {} |
---|
[549] | 25 | |
---|
[219] | 26 | CDate::CDate(const CCalendar& calendar, |
---|
| 27 | int yr, int mth, int d, |
---|
| 28 | int hr, int min, int sec) |
---|
[532] | 29 | : relCalendar(&calendar) |
---|
[549] | 30 | , year(yr), month(mth), day(d) |
---|
[219] | 31 | , hour(hr), minute(min), second(sec) |
---|
| 32 | { |
---|
[549] | 33 | if (!this->checkDate()) |
---|
[231] | 34 | { |
---|
[219] | 35 | DEBUG(<< "La date initialisée a été modifiée " |
---|
| 36 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
[231] | 37 | } |
---|
[219] | 38 | } |
---|
| 39 | |
---|
[532] | 40 | CDate::CDate(const CDate& date) |
---|
[549] | 41 | : relCalendar(date.relCalendar) |
---|
| 42 | , year(date.year), month(date.month), day(date.day) |
---|
| 43 | , hour(date.hour), minute(date.minute), second(date.second) |
---|
[219] | 44 | { |
---|
[532] | 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 | } |
---|
[219] | 51 | } |
---|
| 52 | |
---|
| 53 | CDate::~CDate(void) |
---|
| 54 | { /* Ne rien faire de plus */ } |
---|
| 55 | |
---|
| 56 | ///--------------------------------------------------------------- |
---|
| 57 | |
---|
[549] | 58 | CDate& CDate::operator=(const CDate& date) |
---|
[219] | 59 | { |
---|
[532] | 60 | relCalendar = date.relCalendar; |
---|
[549] | 61 | year = date.year; month = date.month; day = date.day; |
---|
[219] | 62 | hour = date.hour; minute = date.minute; second = date.second; |
---|
| 63 | return (*this); |
---|
| 64 | } |
---|
| 65 | |
---|
[549] | 66 | StdOStream& operator<<(StdOStream& out, const CDate& date) |
---|
[219] | 67 | { |
---|
[549] | 68 | std::streamsize s; |
---|
| 69 | char c; |
---|
[494] | 70 | |
---|
[549] | 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 << '-'; |
---|
[494] | 79 | |
---|
[549] | 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; |
---|
[494] | 85 | |
---|
[549] | 86 | return out; |
---|
[219] | 87 | } |
---|
| 88 | |
---|
[549] | 89 | StdIStream& operator>>(StdIStream& in, CDate& date) // Non testée. |
---|
[219] | 90 | { |
---|
[316] | 91 | char sep = '-'; // Le caractÚre c est utilisé pour "recueillir" les séparateurs "/" et ":". |
---|
[549] | 92 | char c; |
---|
| 93 | |
---|
| 94 | // Default initialize the date |
---|
| 95 | date.year = 0; |
---|
| 96 | date.month = 1; |
---|
| 97 | date.day = 1; |
---|
| 98 | date.hour = 0; |
---|
| 99 | date.minute = 0; |
---|
| 100 | date.second = 0; |
---|
| 101 | |
---|
| 102 | in >> date.year >> c; |
---|
| 103 | if (c == sep) |
---|
[316] | 104 | { |
---|
[549] | 105 | in >> date.month >> c; |
---|
| 106 | if (c == sep) |
---|
[316] | 107 | { |
---|
[549] | 108 | in >> date.day; |
---|
| 109 | c = in.get(); |
---|
| 110 | sep = ' '; |
---|
| 111 | if (c == sep) |
---|
[316] | 112 | { |
---|
[549] | 113 | in >> date.hour >> c; |
---|
| 114 | sep = ':'; |
---|
| 115 | if (c == sep) |
---|
[316] | 116 | { |
---|
| 117 | in>>date.minute >> c; |
---|
[549] | 118 | if (c == sep) |
---|
[316] | 119 | { |
---|
[532] | 120 | in >> date.second; |
---|
[549] | 121 | in >> c; |
---|
[316] | 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | } |
---|
[549] | 127 | |
---|
| 128 | // Delay the verification until we get a calendar we can compare the date to |
---|
| 129 | if (date.relCalendar && !date.checkDate()) |
---|
| 130 | ERROR("StdIStream& operator >> (StdIStream& in, CDate& date)", |
---|
| 131 | << "Bad date format or not conform to calendar"); |
---|
| 132 | |
---|
| 133 | if (c == '+') // We will be adding a duration to the date |
---|
| 134 | { |
---|
| 135 | CDuration dur; |
---|
| 136 | in >> dur; |
---|
| 137 | date = date + dur; |
---|
| 138 | } |
---|
| 139 | else if (!in.eof()) |
---|
| 140 | ERROR("StdIStream& operator >> (StdIStream& in, CDate& date)", |
---|
| 141 | << "Invalid date format: unexpected trailing character(s)"); |
---|
| 142 | |
---|
| 143 | return in; |
---|
[219] | 144 | } |
---|
| 145 | |
---|
[334] | 146 | CDate::operator Time(void) const // Non vérifiée, pas optimisée ... |
---|
[219] | 147 | { |
---|
[549] | 148 | // This will check that a calendar was correctly associated to the date |
---|
| 149 | const CCalendar& c = getRelCalendar(); |
---|
[532] | 150 | |
---|
[549] | 151 | // Todo : Tester si la date courante est supérieure à la date initiale. |
---|
| 152 | Time retvalue = getSecondOfYear() - c.getTimeOrigin().getSecondOfYear(); |
---|
[219] | 153 | |
---|
[549] | 154 | if ((c.getId().compare("D360") == 0) || |
---|
| 155 | (c.getId().compare("AllLeap") == 0) || |
---|
| 156 | (c.getId().compare("NoLeap") == 0)) |
---|
| 157 | return (retvalue + (getYear() - c.getTimeOrigin().getYear()) |
---|
| 158 | * c.getYearTotalLength(*this)); |
---|
[219] | 159 | |
---|
[549] | 160 | for (CDate _d(c.getTimeOrigin()); _d.getYear() < getYear(); _d.setYear(_d.getYear() + 1)) |
---|
| 161 | retvalue += c.getYearTotalLength(_d); |
---|
| 162 | |
---|
| 163 | return retvalue; |
---|
[219] | 164 | } |
---|
| 165 | |
---|
| 166 | //---------------------------------------------------------------- |
---|
| 167 | |
---|
| 168 | bool CDate::checkDate(void) |
---|
| 169 | { |
---|
[549] | 170 | bool retValue = true; |
---|
[219] | 171 | |
---|
[549] | 172 | // This will check that a calendar was correctly associated to the date |
---|
| 173 | const CCalendar& c = getRelCalendar(); |
---|
[532] | 174 | |
---|
[549] | 175 | // Vérification de la valeur du mois. |
---|
| 176 | if (month < 1) { retValue = false; month = 1; } |
---|
| 177 | if (month > c.getYearLength()) |
---|
| 178 | { retValue = false; month = c.getYearLength(); } |
---|
[219] | 179 | |
---|
[549] | 180 | // Vérification de la valeur du jour. |
---|
| 181 | if (day < 1) { retValue = false; day = 1; } |
---|
| 182 | if (day > c.getMonthLength(*this)) |
---|
| 183 | { retValue = false; day = c.getMonthLength(*this); } |
---|
[219] | 184 | |
---|
[549] | 185 | // Vérification de la valeur de l'heure. |
---|
| 186 | if (hour < 0) { retValue = false; hour = 0; } |
---|
| 187 | if (hour >= c.getDayLength()) |
---|
| 188 | { retValue = false; hour = c.getDayLength() - 1; } |
---|
[219] | 189 | |
---|
[549] | 190 | // Vérification de la valeur des minutes. |
---|
| 191 | if (minute < 0) { retValue = false; minute = 0; } |
---|
| 192 | if (minute >= c.getHourLength()) |
---|
| 193 | { retValue = false; minute = c.getHourLength() - 1; } |
---|
[219] | 194 | |
---|
[549] | 195 | // Vérification de la valeur des secondes. |
---|
| 196 | if (second < 0) { retValue = false; second = 0; } |
---|
| 197 | if (second >= c.getMinuteLength()) |
---|
| 198 | { retValue = false; second = c.getMinuteLength() - 1; } |
---|
[219] | 199 | |
---|
[549] | 200 | return retValue; |
---|
[219] | 201 | } |
---|
| 202 | |
---|
| 203 | //---------------------------------------------------------------- |
---|
| 204 | |
---|
| 205 | int CDate::getYear (void) const { return (this->year ); } |
---|
| 206 | int CDate::getMonth (void) const { return (this->month ); } |
---|
| 207 | int CDate::getDay (void) const { return (this->day ); } |
---|
| 208 | int CDate::getHour (void) const { return (this->hour ); } |
---|
| 209 | int CDate::getMinute(void) const { return (this->minute); } |
---|
| 210 | int CDate::getSecond(void) const { return (this->second); } |
---|
| 211 | |
---|
| 212 | //---------------------------------------------------------------- |
---|
| 213 | |
---|
[532] | 214 | const CCalendar& CDate::getRelCalendar(void) const |
---|
| 215 | { |
---|
| 216 | if (!this->relCalendar) |
---|
| 217 | ERROR("const CCalendar& CDate::getRelCalendar(void) const", |
---|
| 218 | "Invalid state: The date is not associated with any calendar."); |
---|
| 219 | return (*this->relCalendar); |
---|
| 220 | } |
---|
[219] | 221 | |
---|
[532] | 222 | bool CDate::hasRelCalendar(void) const |
---|
| 223 | { return (this->relCalendar != NULL); } |
---|
| 224 | |
---|
[219] | 225 | //---------------------------------------------------------------- |
---|
| 226 | |
---|
[549] | 227 | /*! |
---|
| 228 | Get the number of seconds since the beginning of the year. |
---|
| 229 | \return the number of seconds since the beginning of the year. |
---|
| 230 | */ |
---|
| 231 | int CDate::getSecondOfYear() const |
---|
| 232 | { |
---|
| 233 | CDate yearStart(*this); |
---|
| 234 | const CCalendar& c = getRelCalendar(); |
---|
| 235 | int nbDay = 0; |
---|
| 236 | |
---|
| 237 | for (yearStart.setMonth(1); yearStart.getMonth() < getMonth(); yearStart.setMonth(yearStart.getMonth() + 1)) |
---|
| 238 | nbDay += c.getMonthLength(yearStart); |
---|
| 239 | |
---|
| 240 | return ((((nbDay + getDay() - 1) * c.getDayLength() + getHour()) * c.getHourLength() |
---|
| 241 | + getMinute()) * c.getMinuteLength() + getSecond()); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /*! |
---|
| 245 | Get the number of days (expressed as a real number) since the beginning of the year. |
---|
| 246 | \return the number of days (expressed as a real number) since the beginning of the year. |
---|
| 247 | */ |
---|
| 248 | double CDate::getDayOfYear() const |
---|
| 249 | { |
---|
| 250 | return double(getSecondOfYear()) / getRelCalendar().getDayLengthInSeconds(); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /*! |
---|
| 254 | Get the fraction of the current year as a real number between 0 and 1. |
---|
| 255 | \return the fraction of the current year. |
---|
| 256 | */ |
---|
| 257 | double CDate::getFractionOfYear() const |
---|
| 258 | { |
---|
| 259 | return double(getSecondOfYear()) / getRelCalendar().getYearTotalLength(*this); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /*! |
---|
| 263 | Get the number of seconds since the beginning of the day. |
---|
| 264 | \return the number of seconds since the beginning of the day. |
---|
| 265 | */ |
---|
| 266 | int CDate::getSecondOfDay() const |
---|
| 267 | { |
---|
| 268 | const CCalendar& c = getRelCalendar(); |
---|
| 269 | return ((getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond()); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | /*! |
---|
| 273 | Get the fraction of the current day as a real number between 0 and 1. |
---|
| 274 | \return the fraction of the current day. |
---|
| 275 | */ |
---|
| 276 | double CDate::getFractionOfDay() const |
---|
| 277 | { |
---|
| 278 | return double(getSecondOfDay()) / getRelCalendar().getDayLengthInSeconds(); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | //---------------------------------------------------------------- |
---|
| 282 | |
---|
[532] | 283 | void CDate::setYear (int newyear) { this->year = newyear; } |
---|
| 284 | void CDate::setMonth (int newmonth) { this->month = newmonth; } |
---|
| 285 | void CDate::setDay (int newday) { this->day = newday; } |
---|
| 286 | void CDate::setHour (int newhour) { this->hour = newhour; } |
---|
| 287 | void CDate::setMinute(int newminute) { this->minute = newminute; } |
---|
| 288 | void CDate::setSecond(int newsecond) { this->second = newsecond; } |
---|
[219] | 289 | |
---|
[532] | 290 | void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec) |
---|
| 291 | { |
---|
| 292 | this->year = yr; |
---|
| 293 | this->month = mth; |
---|
| 294 | this->day = d; |
---|
| 295 | this->hour = hr; |
---|
| 296 | this->minute = min; |
---|
| 297 | this->second = sec; |
---|
| 298 | } |
---|
| 299 | |
---|
[219] | 300 | //---------------------------------------------------------------- |
---|
| 301 | |
---|
[549] | 302 | void CDate::addMonth(int value) |
---|
[219] | 303 | {// Value doit être égale à 1 ou -1. |
---|
[549] | 304 | this->month += value; |
---|
| 305 | if (this->month == 13) { year++; this->month = 1; } |
---|
| 306 | if (this->month == 0) { year--; this->month = 12; } |
---|
[219] | 307 | } |
---|
| 308 | |
---|
| 309 | //---------------------------------------------------------------- |
---|
| 310 | |
---|
[532] | 311 | bool CDate::setRelCalendar(const CCalendar& relCalendar) |
---|
| 312 | { |
---|
| 313 | this->relCalendar = &relCalendar; |
---|
| 314 | return this->checkDate(); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | //---------------------------------------------------------------- |
---|
| 318 | |
---|
[549] | 319 | CDate CDate::FromString(const StdString& str, const CCalendar& calendar) |
---|
[219] | 320 | { |
---|
[549] | 321 | CDate dt(calendar); |
---|
| 322 | StdIStringStream iss(str); |
---|
| 323 | iss >> dt; |
---|
| 324 | return dt; |
---|
[219] | 325 | } |
---|
[549] | 326 | |
---|
[219] | 327 | //---------------------------------------------------------------- |
---|
[549] | 328 | |
---|
[321] | 329 | StdString CDate::getStryyyymmdd(void) const |
---|
[549] | 330 | { |
---|
| 331 | std::streamsize s; |
---|
| 332 | char c; |
---|
[321] | 333 | |
---|
[549] | 334 | ostringstream oss; |
---|
[321] | 335 | |
---|
[549] | 336 | s = oss.width(4); c = oss.fill('0'); oss << year; |
---|
| 337 | s = oss.width(2); c = oss.fill('0'); oss << month; |
---|
| 338 | s = oss.width(2); c = oss.fill('0'); oss << day; |
---|
[321] | 339 | |
---|
[549] | 340 | return oss.str(); |
---|
[321] | 341 | } |
---|
[431] | 342 | |
---|
| 343 | string CDate::getStr(const string& str) const |
---|
| 344 | { |
---|
[549] | 345 | ostringstream oss; |
---|
[431] | 346 | int level; |
---|
[549] | 347 | |
---|
| 348 | level=0; |
---|
[431] | 349 | for(string::const_iterator it=str.begin();it!=str.end();++it) |
---|
| 350 | { |
---|
| 351 | if (level==0) |
---|
| 352 | { |
---|
[549] | 353 | if (*it=='%') level++; |
---|
| 354 | else oss<<*it; |
---|
[431] | 355 | } |
---|
| 356 | else if (level==1) |
---|
| 357 | { |
---|
| 358 | switch (*it) |
---|
| 359 | { |
---|
| 360 | case 'y' : |
---|
[549] | 361 | oss.width(4); oss.fill('0'); oss << year; |
---|
| 362 | level=0; |
---|
| 363 | break; |
---|
[431] | 364 | case 'm' : // month or minute |
---|
[549] | 365 | level++; |
---|
| 366 | break; |
---|
[431] | 367 | case 'd' : |
---|
[549] | 368 | oss.width(2); oss.fill('0'); oss << day; |
---|
[431] | 369 | level=0; |
---|
[549] | 370 | break; |
---|
[431] | 371 | case 'h' : |
---|
[549] | 372 | oss.width(2); oss.fill('0'); oss << hour; |
---|
[431] | 373 | level=0; |
---|
[549] | 374 | break; |
---|
[431] | 375 | case 's' : |
---|
[549] | 376 | oss.width(2); oss.fill('0'); oss << second; |
---|
| 377 | level=0; |
---|
[431] | 378 | break; |
---|
[549] | 379 | case 'S' : // seconds since time origin |
---|
| 380 | oss.width(0); oss << Time(*this); |
---|
| 381 | level=0; |
---|
| 382 | break; |
---|
| 383 | case 'D' : // days since time origin |
---|
| 384 | oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds(); |
---|
| 385 | level=0; |
---|
| 386 | break; |
---|
[431] | 387 | default : |
---|
[549] | 388 | oss<<'%'<<*it; |
---|
| 389 | level=0; |
---|
[431] | 390 | } |
---|
| 391 | } |
---|
| 392 | else if (level==2) |
---|
| 393 | { |
---|
| 394 | switch (*it) |
---|
| 395 | { |
---|
| 396 | case 'o' : // month |
---|
[549] | 397 | oss.width(2); oss.fill('0'); oss << month; |
---|
| 398 | level=0; |
---|
| 399 | break; |
---|
[431] | 400 | case 'i' : //minute |
---|
[549] | 401 | oss.width(2); oss.fill('0'); oss << minute; |
---|
| 402 | level=0; |
---|
| 403 | break; |
---|
[431] | 404 | default : |
---|
[549] | 405 | oss<<"%m"<<*it; |
---|
| 406 | level=0; |
---|
[431] | 407 | } |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | return oss.str(); |
---|
| 411 | } |
---|
[549] | 412 | |
---|
[431] | 413 | StdString CDate::toString(void) const |
---|
[549] | 414 | { |
---|
| 415 | StdOStringStream oss; |
---|
| 416 | oss << *this; |
---|
| 417 | return oss.str(); |
---|
[219] | 418 | } |
---|
| 419 | |
---|
| 420 | ///--------------------------------------------------------------- |
---|
| 421 | |
---|
[335] | 422 | } // namespace xios |
---|