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