[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 | |
---|
[550] | 89 | StdIStream& operator>>(StdIStream& in, CDate& date) |
---|
[219] | 90 | { |
---|
[550] | 91 | if (date.relCalendar) |
---|
| 92 | date.relCalendar->parseDate(in, date); |
---|
| 93 | else |
---|
| 94 | CCalendar::parseDateDefault(in, date); |
---|
[549] | 95 | |
---|
| 96 | return in; |
---|
[219] | 97 | } |
---|
| 98 | |
---|
[334] | 99 | CDate::operator Time(void) const // Non vérifiée, pas optimisée ... |
---|
[219] | 100 | { |
---|
[549] | 101 | // This will check that a calendar was correctly associated to the date |
---|
| 102 | const CCalendar& c = getRelCalendar(); |
---|
[532] | 103 | |
---|
[549] | 104 | // Todo : Tester si la date courante est supérieure à la date initiale. |
---|
[647] | 105 | Time t = getSecondOfYear() - c.getTimeOrigin().getSecondOfYear(); |
---|
[219] | 106 | |
---|
[550] | 107 | if (c.hasLeapYear()) |
---|
| 108 | { |
---|
[647] | 109 | for (CDate d(c.getTimeOrigin()); d.getYear() < getYear(); d.setYear(d.getYear() + 1)) |
---|
| 110 | t += c.getYearTotalLength(d); |
---|
[550] | 111 | } |
---|
| 112 | else |
---|
[647] | 113 | t += Time(getYear() - c.getTimeOrigin().getYear()) * c.getYearTotalLength(*this); |
---|
[219] | 114 | |
---|
[647] | 115 | return t; |
---|
[219] | 116 | } |
---|
| 117 | |
---|
| 118 | //---------------------------------------------------------------- |
---|
| 119 | |
---|
| 120 | bool CDate::checkDate(void) |
---|
| 121 | { |
---|
[550] | 122 | // This will also check that a calendar was correctly associated to the date |
---|
| 123 | return getRelCalendar().checkDate(*this); |
---|
[219] | 124 | } |
---|
| 125 | |
---|
| 126 | //---------------------------------------------------------------- |
---|
| 127 | |
---|
| 128 | int CDate::getYear (void) const { return (this->year ); } |
---|
| 129 | int CDate::getMonth (void) const { return (this->month ); } |
---|
| 130 | int CDate::getDay (void) const { return (this->day ); } |
---|
| 131 | int CDate::getHour (void) const { return (this->hour ); } |
---|
| 132 | int CDate::getMinute(void) const { return (this->minute); } |
---|
| 133 | int CDate::getSecond(void) const { return (this->second); } |
---|
| 134 | |
---|
| 135 | //---------------------------------------------------------------- |
---|
| 136 | |
---|
[532] | 137 | const CCalendar& CDate::getRelCalendar(void) const |
---|
| 138 | { |
---|
| 139 | if (!this->relCalendar) |
---|
| 140 | ERROR("const CCalendar& CDate::getRelCalendar(void) const", |
---|
| 141 | "Invalid state: The date is not associated with any calendar."); |
---|
| 142 | return (*this->relCalendar); |
---|
| 143 | } |
---|
[219] | 144 | |
---|
[532] | 145 | bool CDate::hasRelCalendar(void) const |
---|
| 146 | { return (this->relCalendar != NULL); } |
---|
| 147 | |
---|
[219] | 148 | //---------------------------------------------------------------- |
---|
| 149 | |
---|
[549] | 150 | /*! |
---|
| 151 | Get the number of seconds since the beginning of the year. |
---|
| 152 | \return the number of seconds since the beginning of the year. |
---|
| 153 | */ |
---|
| 154 | int CDate::getSecondOfYear() const |
---|
| 155 | { |
---|
| 156 | CDate yearStart(*this); |
---|
| 157 | const CCalendar& c = getRelCalendar(); |
---|
| 158 | int nbDay = 0; |
---|
| 159 | |
---|
| 160 | for (yearStart.setMonth(1); yearStart.getMonth() < getMonth(); yearStart.setMonth(yearStart.getMonth() + 1)) |
---|
| 161 | nbDay += c.getMonthLength(yearStart); |
---|
| 162 | |
---|
[550] | 163 | // We need to use getDayLengthInSeconds instead of getDayLength since we might |
---|
| 164 | // have a non-integral number of hours per day for user defined calendars |
---|
| 165 | return ((nbDay + getDay() - 1) * c.getDayLengthInSeconds() |
---|
| 166 | + (getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond()); |
---|
[549] | 167 | } |
---|
| 168 | |
---|
| 169 | /*! |
---|
| 170 | Get the number of days (expressed as a real number) since the beginning of the year. |
---|
| 171 | \return the number of days (expressed as a real number) since the beginning of the year. |
---|
| 172 | */ |
---|
| 173 | double CDate::getDayOfYear() const |
---|
| 174 | { |
---|
| 175 | return double(getSecondOfYear()) / getRelCalendar().getDayLengthInSeconds(); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /*! |
---|
| 179 | Get the fraction of the current year as a real number between 0 and 1. |
---|
| 180 | \return the fraction of the current year. |
---|
| 181 | */ |
---|
| 182 | double CDate::getFractionOfYear() const |
---|
| 183 | { |
---|
| 184 | return double(getSecondOfYear()) / getRelCalendar().getYearTotalLength(*this); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /*! |
---|
| 188 | Get the number of seconds since the beginning of the day. |
---|
| 189 | \return the number of seconds since the beginning of the day. |
---|
| 190 | */ |
---|
| 191 | int CDate::getSecondOfDay() const |
---|
| 192 | { |
---|
| 193 | const CCalendar& c = getRelCalendar(); |
---|
| 194 | return ((getHour() * c.getHourLength() + getMinute()) * c.getMinuteLength() + getSecond()); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /*! |
---|
| 198 | Get the fraction of the current day as a real number between 0 and 1. |
---|
| 199 | \return the fraction of the current day. |
---|
| 200 | */ |
---|
| 201 | double CDate::getFractionOfDay() const |
---|
| 202 | { |
---|
| 203 | return double(getSecondOfDay()) / getRelCalendar().getDayLengthInSeconds(); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | //---------------------------------------------------------------- |
---|
| 207 | |
---|
[532] | 208 | void CDate::setYear (int newyear) { this->year = newyear; } |
---|
| 209 | void CDate::setMonth (int newmonth) { this->month = newmonth; } |
---|
| 210 | void CDate::setDay (int newday) { this->day = newday; } |
---|
| 211 | void CDate::setHour (int newhour) { this->hour = newhour; } |
---|
| 212 | void CDate::setMinute(int newminute) { this->minute = newminute; } |
---|
| 213 | void CDate::setSecond(int newsecond) { this->second = newsecond; } |
---|
[219] | 214 | |
---|
[532] | 215 | void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec) |
---|
| 216 | { |
---|
| 217 | this->year = yr; |
---|
| 218 | this->month = mth; |
---|
| 219 | this->day = d; |
---|
| 220 | this->hour = hr; |
---|
| 221 | this->minute = min; |
---|
| 222 | this->second = sec; |
---|
| 223 | } |
---|
| 224 | |
---|
[219] | 225 | //---------------------------------------------------------------- |
---|
| 226 | |
---|
[549] | 227 | void CDate::addMonth(int value) |
---|
[219] | 228 | {// Value doit être égale à 1 ou -1. |
---|
[549] | 229 | this->month += value; |
---|
| 230 | if (this->month == 13) { year++; this->month = 1; } |
---|
| 231 | if (this->month == 0) { year--; this->month = 12; } |
---|
[219] | 232 | } |
---|
| 233 | |
---|
| 234 | //---------------------------------------------------------------- |
---|
| 235 | |
---|
[532] | 236 | bool CDate::setRelCalendar(const CCalendar& relCalendar) |
---|
| 237 | { |
---|
| 238 | this->relCalendar = &relCalendar; |
---|
| 239 | return this->checkDate(); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | //---------------------------------------------------------------- |
---|
| 243 | |
---|
[549] | 244 | CDate CDate::FromString(const StdString& str, const CCalendar& calendar) |
---|
[219] | 245 | { |
---|
[549] | 246 | CDate dt(calendar); |
---|
| 247 | StdIStringStream iss(str); |
---|
| 248 | iss >> dt; |
---|
| 249 | return dt; |
---|
[219] | 250 | } |
---|
[549] | 251 | |
---|
[219] | 252 | //---------------------------------------------------------------- |
---|
[549] | 253 | |
---|
[321] | 254 | StdString CDate::getStryyyymmdd(void) const |
---|
[549] | 255 | { |
---|
| 256 | std::streamsize s; |
---|
| 257 | char c; |
---|
[321] | 258 | |
---|
[549] | 259 | ostringstream oss; |
---|
[321] | 260 | |
---|
[549] | 261 | s = oss.width(4); c = oss.fill('0'); oss << year; |
---|
| 262 | s = oss.width(2); c = oss.fill('0'); oss << month; |
---|
| 263 | s = oss.width(2); c = oss.fill('0'); oss << day; |
---|
[321] | 264 | |
---|
[549] | 265 | return oss.str(); |
---|
[321] | 266 | } |
---|
[431] | 267 | |
---|
| 268 | string CDate::getStr(const string& str) const |
---|
| 269 | { |
---|
[549] | 270 | ostringstream oss; |
---|
[431] | 271 | int level; |
---|
[549] | 272 | |
---|
| 273 | level=0; |
---|
[431] | 274 | for(string::const_iterator it=str.begin();it!=str.end();++it) |
---|
| 275 | { |
---|
| 276 | if (level==0) |
---|
| 277 | { |
---|
[549] | 278 | if (*it=='%') level++; |
---|
| 279 | else oss<<*it; |
---|
[431] | 280 | } |
---|
| 281 | else if (level==1) |
---|
| 282 | { |
---|
| 283 | switch (*it) |
---|
| 284 | { |
---|
| 285 | case 'y' : |
---|
[549] | 286 | oss.width(4); oss.fill('0'); oss << year; |
---|
| 287 | level=0; |
---|
| 288 | break; |
---|
[431] | 289 | case 'm' : // month or minute |
---|
[549] | 290 | level++; |
---|
| 291 | break; |
---|
[431] | 292 | case 'd' : |
---|
[549] | 293 | oss.width(2); oss.fill('0'); oss << day; |
---|
[431] | 294 | level=0; |
---|
[549] | 295 | break; |
---|
[431] | 296 | case 'h' : |
---|
[549] | 297 | oss.width(2); oss.fill('0'); oss << hour; |
---|
[431] | 298 | level=0; |
---|
[549] | 299 | break; |
---|
[431] | 300 | case 's' : |
---|
[549] | 301 | oss.width(2); oss.fill('0'); oss << second; |
---|
| 302 | level=0; |
---|
[431] | 303 | break; |
---|
[549] | 304 | case 'S' : // seconds since time origin |
---|
| 305 | oss.width(0); oss << Time(*this); |
---|
| 306 | level=0; |
---|
| 307 | break; |
---|
| 308 | case 'D' : // days since time origin |
---|
| 309 | oss.width(0); oss << Time(*this) / getRelCalendar().getDayLengthInSeconds(); |
---|
| 310 | level=0; |
---|
| 311 | break; |
---|
[431] | 312 | default : |
---|
[549] | 313 | oss<<'%'<<*it; |
---|
| 314 | level=0; |
---|
[431] | 315 | } |
---|
| 316 | } |
---|
| 317 | else if (level==2) |
---|
| 318 | { |
---|
| 319 | switch (*it) |
---|
| 320 | { |
---|
| 321 | case 'o' : // month |
---|
[549] | 322 | oss.width(2); oss.fill('0'); oss << month; |
---|
| 323 | level=0; |
---|
| 324 | break; |
---|
[431] | 325 | case 'i' : //minute |
---|
[549] | 326 | oss.width(2); oss.fill('0'); oss << minute; |
---|
| 327 | level=0; |
---|
| 328 | break; |
---|
[431] | 329 | default : |
---|
[549] | 330 | oss<<"%m"<<*it; |
---|
| 331 | level=0; |
---|
[431] | 332 | } |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | return oss.str(); |
---|
| 336 | } |
---|
[549] | 337 | |
---|
[431] | 338 | StdString CDate::toString(void) const |
---|
[549] | 339 | { |
---|
| 340 | StdOStringStream oss; |
---|
| 341 | oss << *this; |
---|
| 342 | return oss.str(); |
---|
[219] | 343 | } |
---|
| 344 | |
---|
| 345 | ///--------------------------------------------------------------- |
---|
| 346 | |
---|
[335] | 347 | } // namespace xios |
---|