sources:
C2_1.cpp (2.2k)
C2_1.h (1.4k)
C2_1Dlg.cpp (6.8k)
C2_1Dlg.h (1.7k)
Moment.cpp (5.7k)
Moment.h (2.7k)
PolymorphicSet.h (9.8k)
StdAfx.cpp (208 bytes)
StdAfx.h (1.1k)
resource.h (774 bytes)


binaries:
Release/C2_1.exe (28.0k)


website:
more info here
studies/bauelemente/Softwarebauelemente-CodeC2-1/Moment.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente II, C2.1
  3 //
  4 // author: Stephan Brumme
  5 // last changes: August 2, 2001
  6
  7
  8 #include "stdafx.h"
  9 #include "Moment.h"
 10 // we are using OS-specific date/time operations
 11 #include <time.h>
 12
 13 #ifdef _DEBUG
 14 #undef THIS_FILE
 15 static char THIS_FILE[]=__FILE__;
 16 #define new DEBUG_NEW
 17 #endif
 18
 19
 20 // serializable
 21 IMPLEMENT_SERIAL(CMoment, CObject, 1)
 22
 23
 24
 25 // constructor, default value is current date and time
 26 CMoment::CMoment()
 27 {
 28     GetCurrent(m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond);
 29 }
 30
 31
 32 // copy constructor
 33 CMoment::CMoment(const CMoment& moment)
 34 {
 35     operator=(moment);
 36 }
 37
 38
 39 // set user defined date/time at construction time
 40 CMoment::CMoment(unsigned int nDay,  unsigned int nMonth,  unsigned int nYear,
 41                  unsigned int nHour, unsigned int nMinute, unsigned int nSecond)

 42 {
 43     // store date
 44     m_nDay    = nDay;
 45     m_nMonth  = nMonth;
 46     m_nYear   = nYear;
 47
 48     // store time
 49     m_nHour   = nHour;
 50     m_nMinute = nMinute;
 51     m_nSecond = nSecond;
 52 }
 53
 54
 55 // destructor, nothing left to do ...
 56 CMoment::~CMoment()
 57 {
 58 }
 59
 60
 61 // serialization
 62 void CMoment::Serialize(CArchive &ar)
 63 {
 64     // do not forget to serialize CObject's data members
 65     CObject::Serialize(ar);
 66
 67     if (ar.IsStoring())
 68         ar << m_nYear << m_nMonth << m_nDay << m_nHour << m_nMinute << m_nSecond;
 69     else         ar >> m_nYear >> m_nMonth >> m_nDay >> m_nHour >> m_nMinute >> m_nSecond;
 70 }
 71
 72
 73 // validate a moment
 74 void CMoment::AssertValid() const {
 75     // validate month
 76     ASSERT(m_nMonth >= JANUARY && m_nMonth <= DECEMBER);
 77
 78     // days per month, february may vary, note that array starts with 0 (JANUARY=1 !)
 79     unsigned int nDaysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 80
 81     // adjust days of february
 82     if (IsLeapYear(m_nYear))
 83         nDaysPerMonth[FEBRUARY]++;
 84
 85     // validate day
 86     ASSERT(m_nDay > 0 && m_nDay <= nDaysPerMonth[m_nMonth]);
 87     // day and month are valid
 88
 89     // now check the time
 90     ASSERT(m_nHour   >= 0 && m_nHour   <= 23);
 91     ASSERT(m_nMinute >= 0 && m_nMinute <= 59);
 92     ASSERT(m_nSecond >= 0 && m_nSecond <= 59);
 93 }
 94
 95
 96 // dump
 97 void CMoment::Dump(CDumpContext& dc) const {
 98     CObject::Dump(dc);
 99
100     CString strOutput;
101     strOutput.Format("date: %02d.%02d.%04d\ntime: %02d:%02d:%02d",
102                       m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond)
;
103     dc << strOutput;
104 }
105
106
107 // return current moment
108 void CMoment::GetCurrent(unsigned int& nDay,  unsigned int& nMonth,  unsigned int& nYear,
109                          unsigned int& nHour, unsigned int& nMinute, unsigned int& nSecond)

110 {
111     // the following code is basically taken from MSDN
112
113     // use system functions to get the current date as UTC
114     time_t secondsSince1970;
115     time(&secondsSince1970);
116
117     // convert UTC to local time zone
118     struct tm *localTime;
119     localTime = localtime(&secondsSince1970);
120
121     // store retrieved date
122     nDay    = localTime->tm_mday;
123     nMonth  = localTime->tm_mon + 1;
124     nYear   = localTime->tm_year + 1900;
125     // store time
126     nHour   = localTime->tm_hour;
127     nMinute = localTime->tm_min;
128     nSecond = localTime->tm_sec;
129 }
130
131
132 // determine whether it is a leap year
133 bool CMoment::IsLeapYear(unsigned int nYear)
134 {
135     // used to speed up code, may be deleted
136     if (nYear % 4   != 0)
137         return false;
138
139     // algorithm taken from MSDN, just converted from VBA to C++
140     if (nYear % 400 == 0)
141         return true;
142     if (nYear % 100 == 0)
143         return false;
144     if (nYear % 4   == 0)
145         return true;
146
147     // this line won't be executed because of optimization (see above)
148     return false;
149 }
150
151
152 // copy
153 CMoment& CMoment::operator =(const CMoment& moment)
154 {
155     // date
156     m_nDay    = moment.m_nDay;
157     m_nMonth  = moment.m_nMonth;
158     m_nYear   = moment.m_nYear;
159     // time
160     m_nHour   = moment.m_nHour;
161     m_nMinute = moment.m_nMinute;
162     m_nSecond = moment.m_nSecond;
163
164     return *this;
165 }
166
167
168 // see operator=
169 CMoment& CMoment::Copy(const CMoment& moment)
170 {
171     return operator=(moment);
172 }
173
174
175 // accept CTime, too
176 CMoment& CMoment::operator =(const CTime& time)
177 {
178     // date
179     m_nDay    = time.GetDay();
180     m_nMonth  = time.GetMonth();
181     m_nYear   = time.GetYear();
182     // time
183     m_nHour   = time.GetHour();
184     m_nMinute = time.GetMinute();
185     m_nSecond = time.GetSecond();
186
187     return *this;
188 }
189
190
191 // compare two moments
192 bool CMoment::operator ==(const CMoment &moment) const {
193     // compare moment and time attributes
194     return (m_nDay    == moment.m_nDay    &&
195             m_nMonth  == moment.m_nMonth  &&
196             m_nYear   == moment.m_nYear   &&
197             m_nHour   == moment.m_nHour   &&
198             m_nMinute == moment.m_nMinute &&
199             m_nSecond == moment.m_nSecond)
;
200 }
201
202
203 // see operator==
204 bool CMoment::EqualValue(const CMoment &moment) const {
205     return operator==(moment);
206 }
207
208
209 // convert to MFC's CTime
210 CMoment::operator CTime() const {
211     return CTime(m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond);
212 }
213
214
215 // set attributes, returns validility of moment
216 void CMoment::SetDay(unsigned int nDay)
217 {
218     m_nDay = nDay;
219 }
220 void CMoment::SetMonth(unsigned int nMonth)
221 {
222     m_nMonth = nMonth;
223 }
224 void CMoment::SetYear(unsigned int nYear)
225 {
226     m_nYear = nYear;
227 }
228 void CMoment::SetHour(unsigned int nHour)
229 {
230     m_nHour = nHour;
231 }
232 void CMoment::SetMinute(unsigned int nMinute)
233 {
234     m_nMinute = nMinute;
235 }
236 void CMoment::SetSecond(unsigned int nSecond)
237 {
238     m_nSecond = nSecond;
239 }
240
241 // return attributes
242 unsigned int CMoment::GetDay() const {
243     return m_nDay;
244 }
245 unsigned int CMoment::GetMonth() const {
246     return m_nMonth;
247 }
248 unsigned int CMoment::GetYear() const {
249     return m_nYear;
250 }
251 unsigned int CMoment::GetHour() const {
252     return m_nHour;
253 }
254 unsigned int CMoment::GetMinute() const {
255     return m_nMinute;
256 }
257 unsigned int CMoment::GetSecond() const {
258     return m_nSecond;
259 }
260
261