1 | #include "date.hpp" |
---|
2 | #include "calendar.hpp" |
---|
3 | |
---|
4 | namespace xmlioserver |
---|
5 | { |
---|
6 | namespace date |
---|
7 | { |
---|
8 | /// ////////////////////// Définitions ////////////////////// /// |
---|
9 | CDate::CDate(const CCalendar& calendar, |
---|
10 | int yr, int mth, int d, |
---|
11 | int hr, int min, int sec) |
---|
12 | : relCalendar(calendar) |
---|
13 | , year(yr), month(mth) , day(d) |
---|
14 | , hour(hr), minute(min), second(sec) |
---|
15 | { |
---|
16 | if(!this->checkDate()) |
---|
17 | DEBUG(<< "La date initialisée a été modifiée " |
---|
18 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
19 | } |
---|
20 | |
---|
21 | CDate::CDate(const CDate & date) |
---|
22 | : relCalendar(date.getRelCalendar()), |
---|
23 | year(date.year), month(date.month) , day(date.day), |
---|
24 | hour(date.hour), minute(date.minute), second(date.second) |
---|
25 | { |
---|
26 | if(!this->checkDate()) |
---|
27 | DEBUG(<< "La date initialisée a été modifiée " |
---|
28 | << "car elle était incorrecte par rapport au calendrier souhaité."); |
---|
29 | } |
---|
30 | |
---|
31 | CDate::~CDate(void) |
---|
32 | { /* Ne rien faire de plus */ } |
---|
33 | |
---|
34 | ///--------------------------------------------------------------- |
---|
35 | |
---|
36 | CDate & CDate::operator=(const CDate & date) |
---|
37 | { |
---|
38 | // relCalendar = d.getRelCalendar(); << inutile si fonction bien utilisée |
---|
39 | year = date.year; month = date.month ; day = date.day; |
---|
40 | hour = date.hour; minute = date.minute; second = date.second; |
---|
41 | return (*this); |
---|
42 | } |
---|
43 | |
---|
44 | StdOStream & operator<<(StdOStream & out, const CDate & date) |
---|
45 | { |
---|
46 | std::streamsize s = out.width (2); |
---|
47 | char c = out.fill ('0'); |
---|
48 | out << date.day << '/'; |
---|
49 | s = out.width (2); c = out.fill ('0'); |
---|
50 | out << date.month << '/'; |
---|
51 | s = out.width (4); c = out.fill ('0'); |
---|
52 | out << date.year << '-'; |
---|
53 | s = out.width (2); c = out.fill ('0'); |
---|
54 | out << date.hour << ':'; |
---|
55 | s = out.width (2); c = out.fill ('0'); |
---|
56 | out << date.minute << ':'; |
---|
57 | s = out.width (2); c = out.fill ('0'); |
---|
58 | out << date.second; |
---|
59 | |
---|
60 | return (out); |
---|
61 | } |
---|
62 | |
---|
63 | StdIStream & operator>>(StdIStream & in, CDate & date) // Non testée. |
---|
64 | { |
---|
65 | char c = '/'; // Le caractÚre c est utilisé pour "recueillir" les séparateurs "/" et ":". |
---|
66 | in >> date.day >> c >> date.month >> c >> date.year >> c; |
---|
67 | in >> date.hour >> c >> date.minute >> c >> date.second; |
---|
68 | if(!date.checkDate()) |
---|
69 | DEBUG("La date initialisée (depuis une chaîne de caractÚres) " |
---|
70 | << "a été modifiée car elle était incorrecte " |
---|
71 | << "par rapport au calendrier souhaité."); |
---|
72 | return (in); |
---|
73 | } |
---|
74 | |
---|
75 | CDate::operator Time(void) // Non vérifiée, pas optimisée ... |
---|
76 | { |
---|
77 | // Todo : Tester si la date courante est supérieure à la date initiale. |
---|
78 | Time retvalue = - relCalendar.getNbSecond(relCalendar.getInitDate()) |
---|
79 | + relCalendar.getNbSecond(*this); |
---|
80 | |
---|
81 | if ((relCalendar.getId().compare("D360") == 0) || |
---|
82 | (relCalendar.getId().compare("AllLeap") == 0) || |
---|
83 | (relCalendar.getId().compare("NoLeap") == 0)) |
---|
84 | return (retvalue + (getYear() - relCalendar.getInitDate().getYear()) |
---|
85 | * relCalendar.getYearTotalLength(*this)); |
---|
86 | |
---|
87 | for(CDate _d(relCalendar.getInitDate()); |
---|
88 | _d.getYear() < getYear(); _d.setYear(_d.getYear()+1)) |
---|
89 | retvalue += relCalendar.getYearTotalLength(_d); |
---|
90 | return (retvalue); |
---|
91 | } |
---|
92 | |
---|
93 | //---------------------------------------------------------------- |
---|
94 | |
---|
95 | bool CDate::checkDate(void) |
---|
96 | { |
---|
97 | bool retValue = true; |
---|
98 | |
---|
99 | // Vérificatio de la valeur du mois. |
---|
100 | if (month < 1) { retValue = false; month = 1; } |
---|
101 | if (month > relCalendar.getYearLength()) |
---|
102 | { retValue = false; month = relCalendar.getYearLength(); } |
---|
103 | |
---|
104 | // Vérification de la valeur du jour. |
---|
105 | if (day < 1) { retValue = false; month = 1; } |
---|
106 | if (day > relCalendar.getMonthLength(*this)) |
---|
107 | { retValue = false; day = relCalendar.getMonthLength(*this); } |
---|
108 | |
---|
109 | // Vérification de la valeur de l'heure. |
---|
110 | if (hour < 0) { retValue = false; hour = 0; } |
---|
111 | if (hour >= relCalendar.getDayLength()) |
---|
112 | { retValue = false; hour = relCalendar.getDayLength()-1; } |
---|
113 | |
---|
114 | // Vérification de la valeur des minutes. |
---|
115 | if (minute < 0) { retValue = false; minute = 0; } |
---|
116 | if (minute >= relCalendar.getHourLength()) |
---|
117 | { retValue = false; minute = relCalendar.getHourLength()-1; } |
---|
118 | |
---|
119 | // Vérification de la valeur des secondes. |
---|
120 | if (second < 0) { retValue = false; month = 0; } |
---|
121 | if (second >= relCalendar.getMinuteLength()) |
---|
122 | { retValue = false; second = relCalendar.getMinuteLength()-1; } |
---|
123 | |
---|
124 | return retValue; |
---|
125 | } |
---|
126 | |
---|
127 | //---------------------------------------------------------------- |
---|
128 | |
---|
129 | int CDate::getYear (void) const { return (this->year ); } |
---|
130 | int CDate::getMonth (void) const { return (this->month ); } |
---|
131 | int CDate::getDay (void) const { return (this->day ); } |
---|
132 | int CDate::getHour (void) const { return (this->hour ); } |
---|
133 | int CDate::getMinute(void) const { return (this->minute); } |
---|
134 | int CDate::getSecond(void) const { return (this->second); } |
---|
135 | |
---|
136 | //---------------------------------------------------------------- |
---|
137 | |
---|
138 | const CCalendar & CDate::getRelCalendar(void) const |
---|
139 | { return (this->relCalendar); } |
---|
140 | |
---|
141 | //---------------------------------------------------------------- |
---|
142 | |
---|
143 | void CDate::setYear (int newyear) { this->year = newyear; } |
---|
144 | void CDate::setMonth (int newmonth) { this->month = newmonth; } |
---|
145 | |
---|
146 | //---------------------------------------------------------------- |
---|
147 | |
---|
148 | void CDate::addMonth (int value) |
---|
149 | {// Value doit être égale à 1 ou -1. |
---|
150 | this->month += value; |
---|
151 | if (this->month == 13) { year++; this->month = 1 ; } |
---|
152 | if (this->month == 0 ) { year--; this->month = 12; } |
---|
153 | } |
---|
154 | |
---|
155 | //---------------------------------------------------------------- |
---|
156 | |
---|
157 | CDate CDate::FromString(const StdString & str, const CCalendar & calendar) |
---|
158 | { |
---|
159 | CDate dt(calendar); |
---|
160 | StdIStringStream iss(str); |
---|
161 | iss >> dt; |
---|
162 | return dt; |
---|
163 | } |
---|
164 | |
---|
165 | //---------------------------------------------------------------- |
---|
166 | |
---|
167 | StdString CDate::toString(void) const |
---|
168 | { |
---|
169 | StdOStringStream oss; |
---|
170 | oss << (*this); |
---|
171 | return (oss.str()); |
---|
172 | } |
---|
173 | |
---|
174 | ///--------------------------------------------------------------- |
---|
175 | |
---|
176 | } // namespace date |
---|
177 | } // namespace xmlioserver |
---|