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