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