1 | #include "calendar.hpp" |
---|
2 | #include "duration.hpp" |
---|
3 | #include "date.hpp" |
---|
4 | #include "calendar_util.hpp" |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
9 | CCalendar::CCalendar(void) |
---|
10 | : CObject() |
---|
11 | , initDate(*this) |
---|
12 | , timeOrigin(*this) |
---|
13 | , currentDate(*this) |
---|
14 | {} |
---|
15 | |
---|
16 | CCalendar::CCalendar(const StdString& id) |
---|
17 | : CObject(id) |
---|
18 | , initDate(*this) |
---|
19 | , timeOrigin(*this) |
---|
20 | , currentDate(*this) |
---|
21 | {} |
---|
22 | |
---|
23 | CCalendar::CCalendar(const StdString& id, |
---|
24 | int yr, int mth, int d, |
---|
25 | int hr /*= 0*/, int min /*= 0*/, int sec /*= 0*/) |
---|
26 | : CObject(id) |
---|
27 | , initDate(*this) |
---|
28 | , timeOrigin(*this) |
---|
29 | , currentDate(*this) |
---|
30 | { |
---|
31 | initializeDate(yr, mth, d, hr, min, sec); |
---|
32 | } |
---|
33 | |
---|
34 | CCalendar::CCalendar(const StdString& id, const CDate& startDate) |
---|
35 | : CObject(id) |
---|
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 |
---|
42 | } |
---|
43 | |
---|
44 | CCalendar::CCalendar(const StdString& id, const CDate& startDate, const CDate& timeOrigin) |
---|
45 | : CObject(id) |
---|
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 |
---|
52 | } |
---|
53 | |
---|
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 | } |
---|
66 | |
---|
67 | void CCalendar::initializeDate(int yr, int mth, int d, |
---|
68 | int hr /*= 0*/, int min /*= 0*/, int sec /*= 0*/) |
---|
69 | { |
---|
70 | initDate = CDate(*this, yr, mth, d, hr, min, sec); |
---|
71 | timeOrigin = initDate; |
---|
72 | currentDate = initDate; |
---|
73 | } |
---|
74 | |
---|
75 | void CCalendar::initializeDate(const StdString& dateStr) |
---|
76 | { |
---|
77 | initDate = CDate::FromString(dateStr, *this); |
---|
78 | timeOrigin = initDate; |
---|
79 | currentDate = initDate; |
---|
80 | } |
---|
81 | |
---|
82 | void CCalendar::initializeDate(const StdString& dateStr, const StdString& timeOriginStr) |
---|
83 | { |
---|
84 | initDate = CDate::FromString(dateStr, *this); |
---|
85 | timeOrigin = CDate::FromString(timeOriginStr, *this); |
---|
86 | currentDate = initDate; |
---|
87 | } |
---|
88 | |
---|
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 | |
---|
103 | void CCalendar::fromString(const StdString& str) |
---|
104 | { ERROR("CCalendar::fromString(str)", |
---|
105 | << "[ str = " << str << "] Not implemented yet !"); } |
---|
106 | |
---|
107 | //----------------------------------------------------------------- |
---|
108 | |
---|
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 | } |
---|
116 | |
---|
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); |
---|
121 | } |
---|
122 | |
---|
123 | //----------------------------------------------------------------- |
---|
124 | |
---|
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 | } |
---|
132 | |
---|
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 | |
---|
141 | //----------------------------------------------------------------- |
---|
142 | |
---|
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 |
---|
151 | { // Retourne la durée du mois en jour. |
---|
152 | static const int NoLeapMonthLength[] = |
---|
153 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
---|
154 | return NoLeapMonthLength[date.getMonth() - 1]; |
---|
155 | } |
---|
156 | |
---|
157 | StdString CCalendar::getType(void) const { return StdString(this->getId()); } |
---|
158 | |
---|
159 | int CCalendar::getYearTotalLength(const CDate& date) const { return (365 * 86400); } |
---|
160 | |
---|
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(); } |
---|
166 | |
---|
167 | StdString CCalendar::getMonthName(int monthId) const |
---|
168 | { |
---|
169 | static const StdString MonthNames[] = |
---|
170 | { "january", "february", "march", "april" , "may", "june", |
---|
171 | "july", "august", "september", "october", "november", "december" }; |
---|
172 | return MonthNames[monthId - 1]; |
---|
173 | } |
---|
174 | |
---|
175 | const StdString CCalendar::getMonthShortName(int monthId) const |
---|
176 | { StdString value = this->getMonthName(monthId); value.resize(3); return value; } |
---|
177 | |
---|
178 | ///---------------------------------------------------------------- |
---|
179 | |
---|
180 | } // namespace xios |
---|