[219] | 1 | #include "calendar.hpp" |
---|
| 2 | #include "duration.hpp" |
---|
| 3 | #include "date.hpp" |
---|
| 4 | #include "calendar_util.hpp" |
---|
| 5 | |
---|
[335] | 6 | namespace xios |
---|
[219] | 7 | { |
---|
| 8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 9 | CCalendar::CCalendar(void) |
---|
| 10 | : CObject() |
---|
| 11 | , initDate(*this) |
---|
[334] | 12 | , timeOrigin(*this) |
---|
| 13 | , currentDate(*this) |
---|
| 14 | { } |
---|
[219] | 15 | |
---|
[532] | 16 | CCalendar::CCalendar(const StdString& id) |
---|
[334] | 17 | : CObject(id) |
---|
| 18 | , initDate(*this) |
---|
| 19 | , timeOrigin(*this) |
---|
| 20 | , currentDate(*this) |
---|
| 21 | { } |
---|
| 22 | |
---|
[532] | 23 | CCalendar::CCalendar(const StdString& id, |
---|
[219] | 24 | int yr, int mth, int d , |
---|
| 25 | int hr, int min, int sec) |
---|
| 26 | : CObject(id) |
---|
[334] | 27 | , initDate(*this) |
---|
| 28 | , timeOrigin(*this) |
---|
| 29 | , currentDate(*this) |
---|
| 30 | { |
---|
| 31 | initializeDate(yr, mth, d, hr, min, sec) ; |
---|
| 32 | } |
---|
[219] | 33 | |
---|
[532] | 34 | CCalendar::CCalendar(const StdString& id, const CDate& startDate) |
---|
[219] | 35 | : CObject(id) |
---|
[532] | 36 | , initDate(startDate) |
---|
| 37 | , timeOrigin(startDate) |
---|
| 38 | , currentDate(startDate) |
---|
| 39 | { |
---|
| 40 | // Initialize the dates only in the derivated classes |
---|
| 41 | // since we want to use the overloaded virtual functions |
---|
[334] | 42 | } |
---|
[219] | 43 | |
---|
[532] | 44 | CCalendar::CCalendar(const StdString& id, const CDate& startDate, const CDate& timeOrigin) |
---|
[334] | 45 | : CObject(id) |
---|
[532] | 46 | , initDate(startDate) |
---|
| 47 | , timeOrigin(timeOrigin) |
---|
| 48 | , currentDate(startDate) |
---|
| 49 | { |
---|
| 50 | // Initialize the dates only in the derivated classes |
---|
| 51 | // since we want to use the overloaded virtual functions |
---|
[334] | 52 | } |
---|
| 53 | |
---|
[532] | 54 | void CCalendar::initializeDate() |
---|
| 55 | { |
---|
| 56 | if (!initDate.setRelCalendar(*this)) |
---|
| 57 | ERROR("CCalendar::initializeDate()", |
---|
| 58 | "initDate: Bad format or date not conform to the calendar"); |
---|
| 59 | if (!timeOrigin.setRelCalendar(*this)) |
---|
| 60 | ERROR("CCalendar::initializeDate()", |
---|
| 61 | "timeOrigin: Bad format or date not conform to the calendar"); |
---|
| 62 | if (!currentDate.setRelCalendar(*this)) |
---|
| 63 | ERROR("CCalendar::initializeDate()", |
---|
| 64 | "currentDate: Bad format or date not conform to the calendar"); |
---|
| 65 | } |
---|
[334] | 66 | |
---|
[532] | 67 | void CCalendar::initializeDate(int yr, int mth, int d, |
---|
| 68 | int hr, int min, int sec) |
---|
| 69 | { |
---|
[334] | 70 | initDate=CDate(*this,yr, mth, d, hr, min, sec) ; |
---|
| 71 | timeOrigin=initDate; |
---|
| 72 | currentDate=initDate ; |
---|
| 73 | } |
---|
| 74 | |
---|
[532] | 75 | void CCalendar::initializeDate(const StdString& dateStr) |
---|
[334] | 76 | { |
---|
| 77 | initDate=CDate::FromString(dateStr, *this) ; |
---|
| 78 | timeOrigin=initDate ; |
---|
| 79 | currentDate=initDate ; |
---|
| 80 | } |
---|
| 81 | |
---|
[532] | 82 | void CCalendar::initializeDate(const StdString& dateStr, const StdString& timeOriginStr) |
---|
[334] | 83 | { |
---|
| 84 | initDate=CDate::FromString(dateStr, *this) ; |
---|
| 85 | timeOrigin=CDate::FromString(timeOriginStr, *this) ; |
---|
| 86 | currentDate=initDate ; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
[219] | 90 | CCalendar::~CCalendar(void) |
---|
| 91 | { /* Ne rien faire de plus */ } |
---|
| 92 | |
---|
| 93 | ///--------------------------------------------------------------- |
---|
| 94 | |
---|
| 95 | StdString CCalendar::toString(void) const |
---|
| 96 | { |
---|
| 97 | StdOStringStream oss; |
---|
| 98 | oss << "[type: " << this->getId() |
---|
| 99 | << ", start: " << this->initDate |
---|
| 100 | << ", current: " << this->currentDate << "]"; |
---|
| 101 | return (oss.str()); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void CCalendar::fromString(const StdString & str) |
---|
| 105 | { ERROR("CCalendar::fromString(str)", |
---|
| 106 | << "[ str = " << str << "] Not implemented yet !"); } |
---|
| 107 | |
---|
| 108 | //----------------------------------------------------------------- |
---|
| 109 | |
---|
| 110 | void CCalendar::setTimeStep(const CDuration & duration) |
---|
| 111 | { this->timestep = duration; } |
---|
| 112 | |
---|
| 113 | CDate & CCalendar::update(int step) |
---|
[266] | 114 | { |
---|
[300] | 115 | info(20) << "update step : " << step <<" timestep "<<this->timestep << std::endl; |
---|
[266] | 116 | return (this->getCurrentDate() = this->getInitDate() + step * this->timestep); |
---|
| 117 | } |
---|
[219] | 118 | |
---|
| 119 | //----------------------------------------------------------------- |
---|
| 120 | |
---|
| 121 | const CDuration & CCalendar::getTimeStep(void) const { return (this->timestep); } |
---|
| 122 | const CDate & CCalendar::getInitDate(void) const { return (this->initDate); } |
---|
[334] | 123 | const CDate & CCalendar::getTimeOrigin(void) const { return (this->timeOrigin); } |
---|
[219] | 124 | CDate & CCalendar::getCurrentDate(void) { return (this->currentDate); } |
---|
| 125 | |
---|
| 126 | //----------------------------------------------------------------- |
---|
| 127 | |
---|
| 128 | int CCalendar::getMonthLength(const CDate & date) const |
---|
| 129 | { // Retourne la durée du mois en jour. |
---|
| 130 | static const int NoLeapMonthLength[] = |
---|
| 131 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
---|
| 132 | return (NoLeapMonthLength[date.getMonth()-1]); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | StdString CCalendar::getType(void) const { return (StdString(this->getId())); } |
---|
| 136 | |
---|
| 137 | int CCalendar::getYearTotalLength(const CDate & date) const { return (365 * 86400); } |
---|
| 138 | |
---|
| 139 | int CCalendar::getYearLength (void) const { return (12); } |
---|
| 140 | int CCalendar::getDayLength (void) const { return (24); } |
---|
| 141 | int CCalendar::getHourLength (void) const { return (60); } |
---|
| 142 | int CCalendar::getMinuteLength(void) const { return (60); } |
---|
| 143 | |
---|
| 144 | int CCalendar::getNbSecond(const CDate & date) const |
---|
| 145 | { // Retourne le nombre de secondes écoulées depuis le début de l'année. |
---|
| 146 | CDate _d0(date); int nbday = 0; |
---|
| 147 | |
---|
| 148 | for(_d0.setMonth(1); _d0.getMonth() < date.getMonth(); _d0.setMonth(_d0.getMonth()+1)) |
---|
| 149 | nbday += getMonthLength(_d0); |
---|
| 150 | return ((((nbday + date.getDay()) * getDayLength() + date.getHour()) * getHourLength() |
---|
| 151 | + date.getMinute()) * getMinuteLength() + date.getSecond()); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | StdString CCalendar::getMonthName(int month_id) const |
---|
| 155 | { |
---|
| 156 | static const StdString Monthname_str[] = |
---|
| 157 | { "january", "february", "march" , "april" , "may" , "june" , |
---|
| 158 | "july" , "august" , "september", "october", "november", "december" }; |
---|
| 159 | return(Monthname_str[month_id-1]); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | const StdString CCalendar::getMonthShortName(int month_id) const |
---|
| 163 | { StdString value = this->getMonthName(month_id); value.resize(3); return (value); } |
---|
| 164 | |
---|
| 165 | ///---------------------------------------------------------------- |
---|
| 166 | |
---|
[335] | 167 | } // namespace xios |
---|