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
70 ar >> m_nYear >> m_nMonth >> m_nDay >> m_nHour >> m_nMinute >> m_nSecond;
71 }
72
73
74 // validate a moment
75 void CMoment::AssertValid() const
76 {
77 // validate month
78 ASSERT(m_nMonth >= JANUARY && m_nMonth <= DECEMBER);
79
80 // days per month, february may vary, note that array starts with 0 (JANUARY=1 !)
81 unsigned int nDaysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
82
83 // adjust days of february
84 if (IsLeapYear(m_nYear))
85 nDaysPerMonth[FEBRUARY]++;
86
87 // validate day
88 ASSERT(m_nDay > 0 && m_nDay <= nDaysPerMonth[m_nMonth]);
89 // day and month are valid
90
91 // now check the time
92 ASSERT(m_nHour >= 0 && m_nHour <= 23);
93 ASSERT(m_nMinute >= 0 && m_nMinute <= 59);
94 ASSERT(m_nSecond >= 0 && m_nSecond <= 59);
95 }
96
97
98 // dump
99 void CMoment::Dump(CDumpContext& dc) const
100 {
101 CObject::Dump(dc);
102
103 CString strOutput;
104 strOutput.Format("date: %02d.%02d.%04d\ntime: %02d:%02d:%02d",
105 m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond);
106 dc << strOutput;
107 }
108
109
110 // return current moment
111 void CMoment::GetCurrent(unsigned int& nDay, unsigned int& nMonth, unsigned int& nYear,
112 unsigned int& nHour, unsigned int& nMinute, unsigned int& nSecond)
113 {
114 // the following code is basically taken from MSDN
115
116 // use system functions to get the current date as UTC
117 time_t secondsSince1970;
118 time(&secondsSince1970);
119
120 // convert UTC to local time zone
121 struct tm *localTime;
122 localTime = localtime(&secondsSince1970);
123
124 // store retrieved date
125 nDay = localTime->tm_mday;
126 nMonth = localTime->tm_mon + 1;
127 nYear = localTime->tm_year + 1900;
128 // store time
129 nHour = localTime->tm_hour;
130 nMinute = localTime->tm_min;
131 nSecond = localTime->tm_sec;
132 }
133
134
135 // determine whether it is a leap year
136 bool CMoment::IsLeapYear(unsigned int nYear)
137 {
138 // used to speed up code, may be deleted
139 if (nYear % 4 != 0)
140 return false;
141
142 // algorithm taken from MSDN, just converted from VBA to C++
143 if (nYear % 400 == 0)
144 return true;
145 if (nYear % 100 == 0)
146 return false;
147 if (nYear % 4 == 0)
148 return true;
149
150 // this line won't be executed because of optimization (see above)
151 return false;
152 }
153
154
155 // copy
156 CMoment& CMoment::operator =(const CMoment& moment)
157 {
158 // date
159 m_nDay = moment.m_nDay;
160 m_nMonth = moment.m_nMonth;
161 m_nYear = moment.m_nYear;
162 // time
163 m_nHour = moment.m_nHour;
164 m_nMinute = moment.m_nMinute;
165 m_nSecond = moment.m_nSecond;
166
167 return *this;
168 }
169
170
171 // see operator=
172 CMoment& CMoment::Copy(const CMoment& moment)
173 {
174 return operator=(moment);
175 }
176
177
178 // accept CTime, too
179 CMoment& CMoment::operator =(const CTime& time)
180 {
181 // date
182 m_nDay = time.GetDay();
183 m_nMonth = time.GetMonth();
184 m_nYear = time.GetYear();
185 // time
186 m_nHour = time.GetHour();
187 m_nMinute = time.GetMinute();
188 m_nSecond = time.GetSecond();
189
190 return *this;
191 }
192
193
194 // compare two moments
195 bool CMoment::operator ==(const CMoment &moment) const
196 {
197 // compare moment and time attributes
198 return (m_nDay == moment.m_nDay &&
199 m_nMonth == moment.m_nMonth &&
200 m_nYear == moment.m_nYear &&
201 m_nHour == moment.m_nHour &&
202 m_nMinute == moment.m_nMinute &&
203 m_nSecond == moment.m_nSecond);
204 }
205
206
207 // see operator==
208 bool CMoment::EqualValue(const CMoment &moment) const
209 {
210 return operator==(moment);
211 }
212
213
214 // convert to MFC's CTime
215 CMoment::operator CTime() const
216 {
217 return CTime(m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond);
218 }
219
220
221 // set attributes, returns validility of moment
222 void CMoment::SetDay(unsigned int nDay)
223 {
224 m_nDay = nDay;
225 }
226 void CMoment::SetMonth(unsigned int nMonth)
227 {
228 m_nMonth = nMonth;
229 }
230 void CMoment::SetYear(unsigned int nYear)
231 {
232 m_nYear = nYear;
233 }
234 void CMoment::SetHour(unsigned int nHour)
235 {
236 m_nHour = nHour;
237 }
238 void CMoment::SetMinute(unsigned int nMinute)
239 {
240 m_nMinute = nMinute;
241 }
242 void CMoment::SetSecond(unsigned int nSecond)
243 {
244 m_nSecond = nSecond;
245 }
246
247 // return attributes
248 unsigned int CMoment::GetDay() const
249 {
250 return m_nDay;
251 }
252 unsigned int CMoment::GetMonth() const
253 {
254 return m_nMonth;
255 }
256 unsigned int CMoment::GetYear() const
257 {
258 return m_nYear;
259 }
260 unsigned int CMoment::GetHour() const
261 {
262 return m_nHour;
263 }
264 unsigned int CMoment::GetMinute() const
265 {
266 return m_nMinute;
267 }
268 unsigned int CMoment::GetSecond() const
269 {
270 return m_nSecond;
271 }
272