studies/bauelemente/Softwarebauelemente-CodeO3-5/Date.cpp
⇒
download file
1
2
3
4
5
6
7
8 #include "Date.h"
9
10
11
12 #include <time.h>
13 #include <iomanip>
14 #include <iostream>
15 using namespace std;
16
17
18
19
20
21 CDate::CDate()
22 {
23 GetToday(m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond);
24 }
25
26
27
28 CDate::CDate(const CDate &date)
29 {
30 operator=(date);
31 }
32
33
34
35 CDate::CDate(unsigned int nDay, unsigned int nMonth, unsigned int nYear,
36 unsigned int nHour, unsigned int nMinute, unsigned int nSecond)
37 {
38
39 m_nDay = nDay;
40 m_nMonth = nMonth;
41 m_nYear = nYear;
42
43
44 m_nHour = nHour;
45 m_nMinute = nMinute;
46 m_nSecond = nSecond;
47 }
48
49
50
51 string CDate::Show() const
52 {
53
54 if (!ClassInvariant())
55 return "";
56
57 ostringstream strOutput;
58
59 strOutput << setw(2) << setfill('0') << m_nDay << "."
60 << setw(2) << setfill('0') << m_nMonth << "."
61 << m_nYear << " - "
62 << setw(2) << setfill('0') << m_nHour<< ":"
63 << setw(2) << setfill('0') << m_nMinute << ":"
64 << setw(2) << setfill('0') << m_nSecond;
65
66 return strOutput.str();
67 }
68
69
70
71 string CDate::ShowDebug() const
72 {
73 ostringstream strOutput;
74
75 strOutput << "DEBUG info for 'CDate'" << endl
76 << " m_nDay = " << m_nDay << endl
77 << " m_nMonth = " << m_nMonth << endl
78 << " m_nYear = " << m_nYear << endl
79 << " m_nHour = " << m_nHour << endl
80 << " m_nMinute = " << m_nMinute << endl
81 << " m_nSecond = " << m_nSecond << endl;
82
83 return strOutput.str();
84 }
85
86
87
88 void CDate::GetToday(unsigned int &nDay, unsigned int &nMonth, unsigned int &nYear,
89 unsigned int &nHour, unsigned int &nMinute, unsigned int &nSecond)
90 {
91
92
93
94 time_t secondsSince1970;
95 time(&secondsSince1970);
96
97
98 struct tm *localTime;
99 localTime = localtime(&secondsSince1970);
100
101
102 nDay = localTime->tm_mday;
103 nMonth = localTime->tm_mon + 1;
104 nYear = localTime->tm_year + 1900;
105
106 nHour = localTime->tm_hour;
107 nMinute = localTime->tm_min;
108 nSecond = localTime->tm_sec;
109 }
110
111
112
113 bool CDate::ClassInvariant() const
114 {
115
116 if (m_nMonth < JANUARY || m_nMonth > DECEMBER)
117 return false;
118
119
120 unsigned int nDaysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
121
122
123 if (IsLeapYear(m_nYear))
124 nDaysPerMonth[FEBRUARY]++;
125
126
127 if (m_nDay < 1 || m_nDay > nDaysPerMonth[m_nMonth])
128 return false;
129
130
131
132 if (m_nHour < 0 || m_nHour > 23)
133 return false;
134 if (m_nMinute < 0 || m_nMinute > 59)
135 return false;
136 if (m_nSecond < 0 || m_nSecond > 59)
137 return false;
138
139
140 return true;
141 }
142
143
144
145 bool CDate::IsLeapYear(unsigned int nYear)
146 {
147
148 if (nYear % 4 != 0)
149 return false;
150
151
152 if (nYear % 400 == 0)
153 return true;
154 if (nYear % 100 == 0)
155 return false;
156 if (nYear % 4 == 0)
157 return true;
158
159
160 return false;
161 }
162
163
164
165 CDate& CDate::operator =(const CDate &date)
166 {
167
168 m_nDay = date.m_nDay;
169 m_nMonth = date.m_nMonth;
170 m_nYear = date.m_nYear;
171
172 m_nHour = date.m_nHour;
173 m_nMinute = date.m_nMinute;
174 m_nSecond = date.m_nSecond;
175
176 return *this;
177 }
178
179
180
181 bool CDate::Copy(const CBasicClass *pClass)
182 {
183
184 const CDate *date = dynamic_cast<const CDate*>(pClass);
185
186
187 if (date == NULL || date == this)
188 return false;
189
190
191
192 operator=(*date);
193
194
195 return true;
196 }
197
198
199
200 bool CDate::operator ==(const CDate &date) const
201 {
202
203 return (m_nDay == date.m_nDay &&
204 m_nMonth == date.m_nMonth &&
205 m_nYear == date.m_nYear &&
206 m_nHour == date.m_nHour &&
207 m_nMinute == date.m_nMinute &&
208 m_nSecond == date.m_nSecond);
209 }
210
211
212
213 bool CDate::EqualValue(const CBasicClass *pClass) const
214 {
215
216 const CDate *date = dynamic_cast<const CDate*>(pClass);
217
218
219 if (date == NULL)
220 return false;
221
222
223 return operator==(*date);
224 }
225
226
227
228 void CDate::SetDay(unsigned int nDay)
229 {
230 m_nDay = nDay;
231 }
232 void CDate::SetMonth(unsigned int nMonth)
233 {
234 m_nMonth = nMonth;
235 }
236 void CDate::SetYear(unsigned int nYear)
237 {
238 m_nYear = nYear;
239 }
240 void CDate::SetHour(unsigned int nHour)
241 {
242 m_nHour = nHour;
243 }
244 void CDate::SetMinute(unsigned int nMinute)
245 {
246 m_nMinute = nMinute;
247 }
248 void CDate::SetSecond(unsigned int nSecond)
249 {
250 m_nSecond = nSecond;
251 }
252
253
254 unsigned int CDate::GetDay() const
255 {
256 return m_nDay;
257 }
258 unsigned int CDate::GetMonth() const
259 {
260 return m_nMonth;
261 }
262 unsigned int CDate::GetYear() const
263 {
264 return m_nYear;
265 }
266 unsigned int CDate::GetHour() const
267 {
268 return m_nHour;
269 }
270 unsigned int CDate::GetMinute() const
271 {
272 return m_nMinute;
273 }
274 unsigned int CDate::GetSecond() const
275 {
276 return m_nSecond;
277 }
278