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