[219] | 1 | #include "calendar.hpp" |
---|
| 2 | #include "duration.hpp" |
---|
| 3 | #include "date.hpp" |
---|
| 4 | #include "calendar_util.hpp" |
---|
| 5 | |
---|
| 6 | namespace xmlioserver |
---|
| 7 | { |
---|
| 8 | namespace date |
---|
| 9 | { |
---|
| 10 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 11 | CCalendar::CCalendar(void) |
---|
| 12 | : CObject() |
---|
| 13 | , initDate(*this) |
---|
| 14 | , currentDate(initDate) |
---|
| 15 | { /* Ne rien faire de plus */ } |
---|
| 16 | |
---|
| 17 | CCalendar::CCalendar(const StdString & id, |
---|
| 18 | int yr, int mth, int d , |
---|
| 19 | int hr, int min, int sec) |
---|
| 20 | : CObject(id) |
---|
| 21 | , initDate(*this, yr, mth, d, hr, min, sec) |
---|
| 22 | , currentDate(initDate) |
---|
| 23 | { /* Ne rien faire de plus */ } |
---|
| 24 | |
---|
| 25 | CCalendar::CCalendar(const StdString & id, const StdString & dateStr) |
---|
| 26 | : CObject(id) |
---|
| 27 | , initDate(CDate::FromString(dateStr, *this)) |
---|
| 28 | , currentDate(initDate) |
---|
| 29 | { /* Ne rien faire de plus */ } |
---|
| 30 | |
---|
| 31 | CCalendar::~CCalendar(void) |
---|
| 32 | { /* Ne rien faire de plus */ } |
---|
| 33 | |
---|
| 34 | ///--------------------------------------------------------------- |
---|
| 35 | |
---|
| 36 | StdString CCalendar::toString(void) const |
---|
| 37 | { |
---|
| 38 | StdOStringStream oss; |
---|
| 39 | oss << "[type: " << this->getId() |
---|
| 40 | << ", start: " << this->initDate |
---|
| 41 | << ", current: " << this->currentDate << "]"; |
---|
| 42 | return (oss.str()); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | void CCalendar::fromString(const StdString & str) |
---|
| 46 | { ERROR("CCalendar::fromString(str)", |
---|
| 47 | << "[ str = " << str << "] Not implemented yet !"); } |
---|
| 48 | |
---|
| 49 | //----------------------------------------------------------------- |
---|
| 50 | |
---|
| 51 | void CCalendar::setTimeStep(const CDuration & duration) |
---|
| 52 | { this->timestep = duration; } |
---|
| 53 | |
---|
| 54 | CDate & CCalendar::update(int step) |
---|
| 55 | { return (this->getCurrentDate() = this->getCurrentDate() + step * this->timestep); } |
---|
| 56 | |
---|
| 57 | //----------------------------------------------------------------- |
---|
| 58 | |
---|
| 59 | const CDuration & CCalendar::getTimeStep(void) const { return (this->timestep); } |
---|
| 60 | const CDate & CCalendar::getInitDate(void) const { return (this->initDate); } |
---|
| 61 | CDate & CCalendar::getCurrentDate(void) { return (this->currentDate); } |
---|
| 62 | |
---|
| 63 | //----------------------------------------------------------------- |
---|
| 64 | |
---|
| 65 | int CCalendar::getMonthLength(const CDate & date) const |
---|
| 66 | { // Retourne la durée du mois en jour. |
---|
| 67 | static const int NoLeapMonthLength[] = |
---|
| 68 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
---|
| 69 | return (NoLeapMonthLength[date.getMonth()-1]); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | StdString CCalendar::getType(void) const { return (StdString(this->getId())); } |
---|
| 73 | |
---|
| 74 | int CCalendar::getYearTotalLength(const CDate & date) const { return (365 * 86400); } |
---|
| 75 | |
---|
| 76 | int CCalendar::getYearLength (void) const { return (12); } |
---|
| 77 | int CCalendar::getDayLength (void) const { return (24); } |
---|
| 78 | int CCalendar::getHourLength (void) const { return (60); } |
---|
| 79 | int CCalendar::getMinuteLength(void) const { return (60); } |
---|
| 80 | |
---|
| 81 | int CCalendar::getNbSecond(const CDate & date) const |
---|
| 82 | { // Retourne le nombre de secondes écoulées depuis le début de l'année. |
---|
| 83 | CDate _d0(date); int nbday = 0; |
---|
| 84 | |
---|
| 85 | for(_d0.setMonth(1); _d0.getMonth() < date.getMonth(); _d0.setMonth(_d0.getMonth()+1)) |
---|
| 86 | nbday += getMonthLength(_d0); |
---|
| 87 | return ((((nbday + date.getDay()) * getDayLength() + date.getHour()) * getHourLength() |
---|
| 88 | + date.getMinute()) * getMinuteLength() + date.getSecond()); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | StdString CCalendar::getMonthName(int month_id) const |
---|
| 92 | { |
---|
| 93 | static const StdString Monthname_str[] = |
---|
| 94 | { "january", "february", "march" , "april" , "may" , "june" , |
---|
| 95 | "july" , "august" , "september", "october", "november", "december" }; |
---|
| 96 | return(Monthname_str[month_id-1]); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | const StdString CCalendar::getMonthShortName(int month_id) const |
---|
| 100 | { StdString value = this->getMonthName(month_id); value.resize(3); return (value); } |
---|
| 101 | |
---|
| 102 | ///---------------------------------------------------------------- |
---|
| 103 | |
---|
| 104 | } // namespace date |
---|
| 105 | } // namespace xmlioserver |
---|