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, int min, int sec) |
---|
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 StdString & dateStr) |
---|
35 | : CObject(id) |
---|
36 | , initDate(CDate::FromString(dateStr, *this)) |
---|
37 | , timeOrigin(initDate) |
---|
38 | , currentDate(initDate) |
---|
39 | { |
---|
40 | initializeDate(dateStr) ; |
---|
41 | } |
---|
42 | |
---|
43 | CCalendar::CCalendar(const StdString & id, const StdString & dateStr, const StdString & timeOriginStr) |
---|
44 | : CObject(id) |
---|
45 | , initDate(*this) |
---|
46 | , timeOrigin(*this) |
---|
47 | , currentDate(*this) |
---|
48 | { |
---|
49 | initializeDate(dateStr, timeOriginStr) ; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | void CCalendar::initializeDate( int yr, int mth, int d , |
---|
54 | int hr, int min, int sec) |
---|
55 | { |
---|
56 | initDate=CDate(*this,yr, mth, d, hr, min, sec) ; |
---|
57 | timeOrigin=initDate; |
---|
58 | currentDate=initDate ; |
---|
59 | } |
---|
60 | |
---|
61 | void CCalendar::initializeDate(const StdString & dateStr) |
---|
62 | { |
---|
63 | initDate=CDate::FromString(dateStr, *this) ; |
---|
64 | timeOrigin=initDate ; |
---|
65 | currentDate=initDate ; |
---|
66 | } |
---|
67 | |
---|
68 | void CCalendar::initializeDate(const StdString & dateStr, const StdString & timeOriginStr) |
---|
69 | { |
---|
70 | initDate=CDate::FromString(dateStr, *this) ; |
---|
71 | timeOrigin=CDate::FromString(timeOriginStr, *this) ; |
---|
72 | currentDate=initDate ; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | CCalendar::~CCalendar(void) |
---|
77 | { /* Ne rien faire de plus */ } |
---|
78 | |
---|
79 | ///--------------------------------------------------------------- |
---|
80 | |
---|
81 | StdString CCalendar::toString(void) const |
---|
82 | { |
---|
83 | StdOStringStream oss; |
---|
84 | oss << "[type: " << this->getId() |
---|
85 | << ", start: " << this->initDate |
---|
86 | << ", current: " << this->currentDate << "]"; |
---|
87 | return (oss.str()); |
---|
88 | } |
---|
89 | |
---|
90 | void CCalendar::fromString(const StdString & str) |
---|
91 | { ERROR("CCalendar::fromString(str)", |
---|
92 | << "[ str = " << str << "] Not implemented yet !"); } |
---|
93 | |
---|
94 | //----------------------------------------------------------------- |
---|
95 | |
---|
96 | void CCalendar::setTimeStep(const CDuration & duration) |
---|
97 | { this->timestep = duration; } |
---|
98 | |
---|
99 | CDate & CCalendar::update(int step) |
---|
100 | { |
---|
101 | info(20) << "update step : " << step <<" timestep "<<this->timestep << std::endl; |
---|
102 | return (this->getCurrentDate() = this->getInitDate() + step * this->timestep); |
---|
103 | } |
---|
104 | |
---|
105 | //----------------------------------------------------------------- |
---|
106 | |
---|
107 | const CDuration & CCalendar::getTimeStep(void) const { return (this->timestep); } |
---|
108 | const CDate & CCalendar::getInitDate(void) const { return (this->initDate); } |
---|
109 | const CDate & CCalendar::getTimeOrigin(void) const { return (this->timeOrigin); } |
---|
110 | CDate & CCalendar::getCurrentDate(void) { return (this->currentDate); } |
---|
111 | |
---|
112 | //----------------------------------------------------------------- |
---|
113 | |
---|
114 | int CCalendar::getMonthLength(const CDate & date) const |
---|
115 | { // Retourne la durée du mois en jour. |
---|
116 | static const int NoLeapMonthLength[] = |
---|
117 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
---|
118 | return (NoLeapMonthLength[date.getMonth()-1]); |
---|
119 | } |
---|
120 | |
---|
121 | StdString CCalendar::getType(void) const { return (StdString(this->getId())); } |
---|
122 | |
---|
123 | int CCalendar::getYearTotalLength(const CDate & date) const { return (365 * 86400); } |
---|
124 | |
---|
125 | int CCalendar::getYearLength (void) const { return (12); } |
---|
126 | int CCalendar::getDayLength (void) const { return (24); } |
---|
127 | int CCalendar::getHourLength (void) const { return (60); } |
---|
128 | int CCalendar::getMinuteLength(void) const { return (60); } |
---|
129 | |
---|
130 | int CCalendar::getNbSecond(const CDate & date) const |
---|
131 | { // Retourne le nombre de secondes écoulées depuis le début de l'année. |
---|
132 | CDate _d0(date); int nbday = 0; |
---|
133 | |
---|
134 | for(_d0.setMonth(1); _d0.getMonth() < date.getMonth(); _d0.setMonth(_d0.getMonth()+1)) |
---|
135 | nbday += getMonthLength(_d0); |
---|
136 | return ((((nbday + date.getDay()) * getDayLength() + date.getHour()) * getHourLength() |
---|
137 | + date.getMinute()) * getMinuteLength() + date.getSecond()); |
---|
138 | } |
---|
139 | |
---|
140 | StdString CCalendar::getMonthName(int month_id) const |
---|
141 | { |
---|
142 | static const StdString Monthname_str[] = |
---|
143 | { "january", "february", "march" , "april" , "may" , "june" , |
---|
144 | "july" , "august" , "september", "october", "november", "december" }; |
---|
145 | return(Monthname_str[month_id-1]); |
---|
146 | } |
---|
147 | |
---|
148 | const StdString CCalendar::getMonthShortName(int month_id) const |
---|
149 | { StdString value = this->getMonthName(month_id); value.resize(3); return (value); } |
---|
150 | |
---|
151 | ///---------------------------------------------------------------- |
---|
152 | |
---|
153 | } // namespace xios |
---|