[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) |
---|
[549] | 14 | {} |
---|
[219] | 15 | |
---|
[532] | 16 | CCalendar::CCalendar(const StdString& id) |
---|
[334] | 17 | : CObject(id) |
---|
| 18 | , initDate(*this) |
---|
| 19 | , timeOrigin(*this) |
---|
| 20 | , currentDate(*this) |
---|
[549] | 21 | {} |
---|
| 22 | |
---|
[532] | 23 | CCalendar::CCalendar(const StdString& id, |
---|
[549] | 24 | int yr, int mth, int d, |
---|
| 25 | int hr /*= 0*/, int min /*= 0*/, int sec /*= 0*/) |
---|
[219] | 26 | : CObject(id) |
---|
[334] | 27 | , initDate(*this) |
---|
| 28 | , timeOrigin(*this) |
---|
| 29 | , currentDate(*this) |
---|
[549] | 30 | { |
---|
| 31 | initializeDate(yr, mth, d, hr, min, sec); |
---|
[334] | 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, |
---|
[549] | 68 | int hr /*= 0*/, int min /*= 0*/, int sec /*= 0*/) |
---|
[532] | 69 | { |
---|
[549] | 70 | initDate = CDate(*this, yr, mth, d, hr, min, sec); |
---|
| 71 | timeOrigin = initDate; |
---|
| 72 | currentDate = initDate; |
---|
[334] | 73 | } |
---|
| 74 | |
---|
[532] | 75 | void CCalendar::initializeDate(const StdString& dateStr) |
---|
[549] | 76 | { |
---|
| 77 | initDate = CDate::FromString(dateStr, *this); |
---|
| 78 | timeOrigin = initDate; |
---|
| 79 | currentDate = initDate; |
---|
[334] | 80 | } |
---|
| 81 | |
---|
[532] | 82 | void CCalendar::initializeDate(const StdString& dateStr, const StdString& timeOriginStr) |
---|
[549] | 83 | { |
---|
| 84 | initDate = CDate::FromString(dateStr, *this); |
---|
| 85 | timeOrigin = CDate::FromString(timeOriginStr, *this); |
---|
| 86 | currentDate = initDate; |
---|
[334] | 87 | } |
---|
| 88 | |
---|
[219] | 89 | CCalendar::~CCalendar(void) |
---|
| 90 | { /* Ne rien faire de plus */ } |
---|
| 91 | |
---|
| 92 | ///--------------------------------------------------------------- |
---|
| 93 | |
---|
| 94 | StdString CCalendar::toString(void) const |
---|
| 95 | { |
---|
| 96 | StdOStringStream oss; |
---|
| 97 | oss << "[type: " << this->getId() |
---|
| 98 | << ", start: " << this->initDate |
---|
| 99 | << ", current: " << this->currentDate << "]"; |
---|
| 100 | return (oss.str()); |
---|
| 101 | } |
---|
| 102 | |
---|
[549] | 103 | void CCalendar::fromString(const StdString& str) |
---|
[219] | 104 | { ERROR("CCalendar::fromString(str)", |
---|
| 105 | << "[ str = " << str << "] Not implemented yet !"); } |
---|
| 106 | |
---|
| 107 | //----------------------------------------------------------------- |
---|
| 108 | |
---|
[549] | 109 | void CCalendar::setTimeStep(const CDuration& timestep) |
---|
| 110 | { |
---|
| 111 | if (timestep.timestep) |
---|
| 112 | ERROR("CCalendar::setTimeStep(const CDuration& timestep)", |
---|
| 113 | << "Circular definition of the timestep: the timestep cannot refer to itself."); |
---|
| 114 | this->timestep = timestep; |
---|
| 115 | } |
---|
[219] | 116 | |
---|
[549] | 117 | CDate& CCalendar::update(int step) |
---|
| 118 | { |
---|
| 119 | info(20) << "update step : " << step << " timestep " << this->timestep << std::endl; |
---|
| 120 | return (this->getCurrentDate() = this->getInitDate() + step * this->timestep); |
---|
[266] | 121 | } |
---|
[219] | 122 | |
---|
| 123 | //----------------------------------------------------------------- |
---|
| 124 | |
---|
[549] | 125 | void CCalendar::setInitDate(const CDate& initDate) |
---|
| 126 | { |
---|
| 127 | if (&initDate.getRelCalendar() != this) |
---|
| 128 | ERROR("CCalendar::setInitDate(const CDate& initDate)", |
---|
| 129 | << "The init date cannot be attached to another calendar."); |
---|
| 130 | this->initDate = initDate; |
---|
| 131 | } |
---|
[219] | 132 | |
---|
[549] | 133 | void CCalendar::setTimeOrigin(const CDate& timeOrigin) |
---|
| 134 | { |
---|
| 135 | if (&timeOrigin.getRelCalendar() != this) |
---|
| 136 | ERROR("CCalendar::setInitDate(const CDate& timeOrigin)", |
---|
| 137 | << "The time origin cannot be attached to another calendar."); |
---|
| 138 | this->timeOrigin = timeOrigin; |
---|
| 139 | } |
---|
| 140 | |
---|
[219] | 141 | //----------------------------------------------------------------- |
---|
| 142 | |
---|
[549] | 143 | const CDuration& CCalendar::getTimeStep(void) const { return this->timestep; } |
---|
| 144 | const CDate& CCalendar::getInitDate(void) const { return this->initDate; } |
---|
| 145 | const CDate& CCalendar::getTimeOrigin(void) const { return this->timeOrigin; } |
---|
| 146 | CDate& CCalendar::getCurrentDate(void) { return this->currentDate; } |
---|
| 147 | |
---|
| 148 | //----------------------------------------------------------------- |
---|
| 149 | |
---|
| 150 | int CCalendar::getMonthLength(const CDate& date) const |
---|
[219] | 151 | { // Retourne la durée du mois en jour. |
---|
[549] | 152 | static const int NoLeapMonthLength[] = |
---|
| 153 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
---|
| 154 | return NoLeapMonthLength[date.getMonth() - 1]; |
---|
[219] | 155 | } |
---|
| 156 | |
---|
[549] | 157 | StdString CCalendar::getType(void) const { return StdString(this->getId()); } |
---|
[219] | 158 | |
---|
[549] | 159 | int CCalendar::getYearTotalLength(const CDate& date) const { return (365 * 86400); } |
---|
[219] | 160 | |
---|
[549] | 161 | int CCalendar::getYearLength (void) const { return 12; } |
---|
| 162 | int CCalendar::getDayLength (void) const { return 24; } |
---|
| 163 | int CCalendar::getHourLength (void) const { return 60; } |
---|
| 164 | int CCalendar::getMinuteLength(void) const { return 60; } |
---|
| 165 | int CCalendar::getDayLengthInSeconds(void) const { return getDayLength() * getHourLength() * getMinuteLength(); } |
---|
[219] | 166 | |
---|
[549] | 167 | StdString CCalendar::getMonthName(int monthId) const |
---|
[219] | 168 | { |
---|
[549] | 169 | static const StdString MonthNames[] = |
---|
| 170 | { "january", "february", "march", "april" , "may", "june", |
---|
| 171 | "july", "august", "september", "october", "november", "december" }; |
---|
| 172 | return MonthNames[monthId - 1]; |
---|
[219] | 173 | } |
---|
| 174 | |
---|
[549] | 175 | const StdString CCalendar::getMonthShortName(int monthId) const |
---|
| 176 | { StdString value = this->getMonthName(monthId); value.resize(3); return value; } |
---|
[219] | 177 | |
---|
| 178 | ///---------------------------------------------------------------- |
---|
| 179 | |
---|
[335] | 180 | } // namespace xios |
---|