#ifndef __XMLIO_CALENDAR_UTILS__ #define __XMLIO_CALENDAR_UTILS__ namespace XMLIOSERVER { Date Date::FromString(const string& str, const AbstractCalendar& cal) { Date dt(cal); std::istringstream iss(str); iss >> dt; return dt; } bool Date::checkDate(void) { bool retValue = true; // Vérificatio de la valeur du mois. if (month < 1) { retValue = false; month = 1; } if (month > relCalendar.getYearLength()) { retValue = false; month = relCalendar.getYearLength(); } // Vérification de la valeur du jour. if (day < 1) { retValue = false; month = 1; } if (day > relCalendar.getMonthLength(*this)) { retValue = false; day = relCalendar.getMonthLength(*this); } // Vérification de la valeur de l'heure. if (hour < 0) { retValue = false; hour = 0; } if (hour >= relCalendar.getDayLength()) { retValue = false; hour = relCalendar.getDayLength()-1; } // Vérification de la valeur des minutes. if (minute < 0) { retValue = false; minute = 0; } if (minute >= relCalendar.getHourLength()) { retValue = false; minute = relCalendar.getHourLength()-1; } // Vérification de la valeur des secondes. if (second < 0) { retValue = false; month = 0; } if (second >= relCalendar.getMinuteLength()) { retValue = false; second = relCalendar.getMinuteLength()-1; } return retValue; } Duration& Duration::resolve(const AbstractCalendar& c) { // Simplification de l'écriture des minutes. second += modf(minute, &minute) * (float)c.getMinuteLength(); minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength(); // Simplification de l'écriture des heures. minute += modf(hour , &hour) * (float)c.getHourLength(); hour += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength(); // Simplification de l'écriture des jours. hour += modf(day, &day) * (float)c.getDayLength(); day += int(hour) /c.getDayLength(); hour = int(hour)%c.getDayLength(); // > Aucune équivalence jour - mois fixée par avance. // // Simplification de l'écriture des années. month += modf(year, &year) * (float)c.getYearLength(); year += int(month) /c.getYearLength(); month = int(month)%c.getYearLength(); return (*this); } Duration operator*(const Duration& ddr, const double& scal) { Duration dur(ddr); dur.year *= scal; dur.month *= scal; dur.day *= scal; dur.hour *= scal; dur.minute *= scal; dur.second *= scal; return (dur); } Duration operator*(const double& scal, const Duration& ddr) { return (ddr * scal); } Duration operator-(const Duration& ddr, const Duration& dr) { Duration dur(ddr); dur.year -= dr.year; dur.month -= dr.month ; dur.day -= dr.day; dur.hour -= dr.hour; dur.minute -= dr.minute; dur.second -= dr.second; return (dur); } Duration operator-(const Duration& ddr) { Duration dur(ddr); dur.year = -dur.year; dur.month = -dur.month ; dur.day = -dur.day; dur.hour = -dur.hour; dur.minute = -dur.minute; dur.second = -dur.second; return (dur); } Duration operator+(const Duration& ddr, const Duration& dr) { Duration dur(ddr); dur.year += dr.year; dur.month += dr.month ; dur.day += dr.day; dur.hour += dr.hour; dur.minute += dr.minute; dur.second += dr.second; return (dur); } Date::operator Time(void) // Non vérifiée, pas optimisée ... { // Todo : Tester si la date courante est supérieure à la date initiale. Time retvalue = - relCalendar.getNbSecond(relCalendar.getInitDate()) + relCalendar.getNbSecond(*this); if ((relCalendar.getId().compare("D360") == 0) || (relCalendar.getId().compare("AllLeap") == 0) || (relCalendar.getId().compare("NoLeap") == 0)) return (retvalue + (getYear() - relCalendar.getInitDate().getYear()) * relCalendar.getYearTotalLength(*this)); for(Date _d(relCalendar.getInitDate()); _d.getYear() < getYear(); _d.setYear(_d.getYear()+1)) retvalue += relCalendar.getYearTotalLength(_d); return (retvalue); } Date operator+(const Date& dt, const Duration& dr) // Non testée. { Duration drr (dr); int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; const AbstractCalendar& c = dt.getRelCalendar(); drr.resolve(dt.getRelCalendar()); // Ajustement des minutes par rapport aux secondes. second += dt.getSecond() + drr.second; if (second < 0) { minute --; second += c.getMinuteLength(); } if (second >= c.getMinuteLength()) { minute ++; second -= c.getMinuteLength(); } // Ajustement des heures en fonction des minutes. minute += dt.getMinute() + drr.minute; if (minute < 0) { hour --; minute += c.getHourLength(); } if (minute >= c.getHourLength()) { hour ++; minute -= c.getHourLength(); } // Ajustement des jours en fonction des heures. hour += dt.getHour() + drr.hour; if (hour < 0) { drr.day --; hour += c.getDayLength(); } if (hour >= c.getDayLength()) { drr.day ++; hour -= c.getDayLength(); } // Ajustement des mois en fonction des jours. int signVal = drr.day/fabs(drr.day); Date dtt(dt); dtt.addMonth (signVal); for(; c.getMonthLength(dtt) < fabs(drr.day); dtt.addMonth (signVal)) { drr.day -= signVal * c.getMonthLength(dtt); drr.month += signVal; } day += dt.getDay() + drr.day; if (day < 0) { drr.month --; day += c.getMonthLength(dtt); } if (day >= c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << Problème ici if (day == 0) day = c.getMonthLength(dtt); drr.resolve(dt.getRelCalendar()); // Ajustement des années en fonction des mois. month += dt.getMonth() + drr.month; if (month < 0) { year --; month += c.getYearLength(); } if (month >= c.getYearLength()) { year ++; month -= c.getYearLength(); } if (month == 0) month = c.getYearLength(); year += dt.getYear() + drr.year; return (Date(dt.getRelCalendar(), year, month, day, hour, minute, second)); } Date operator-(const Date& dt, const Duration& dr) { return (dt + (-dr)); } Duration operator-(const Date& dt0, const Date& dt1) { // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. Duration dur = { dt0.getYear() - dt1.getYear(), dt0.getMonth() - dt1.getMonth() , dt0.getDay() - dt1.getDay(), dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() }; return (dur.resolve(dt0.getRelCalendar())); } /// Les opérateurs de comparaison. (Non testés pour le moment) bool operator==(const Date& dt0, const Date& dt1) { // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth() == dt1.getMonth()) && (dt1.getDay() == dt0.getDay()) && (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond())); } bool operator<(const Date& dt0, const Date& dt1) { // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier. if (dt0.getYear() < dt1.getYear()) return true; if (dt0.getMonth() < dt1.getMonth()) return true; if (dt0.getDay() < dt1.getDay()) return true; if (dt0.getHour() < dt1.getHour()) return true; if (dt0.getMinute() < dt1.getMinute()) return true; if (dt0.getSecond() < dt1.getSecond()) return true; return false; } bool operator!=(const Date& dt0, const Date& dt1) { return !(dt1 == dt0); } bool operator> (const Date& dt0, const Date& dt1) { return (dt1 < dt0); } bool operator>=(const Date& dt0, const Date& dt1) { return ((dt1 > dt0) || (dt1 == dt0)); } bool operator<=(const Date& dt0, const Date& dt1) { return ((dt1 < dt0) || (dt1 == dt0)); } } // namespace XMLIOSERVER #endif //__XMLIO_CALENDAR_UTILS__