1 ///////////////////////////////////////////////////////////
2 // Softwarebauelemente II, Aufgabe O2.1
3 //
4 // author: Stephan Brumme
5 // last changes: February 26, 2001
6
7
8 #include "Date.h"
9 #include <iostream>
10
11 // we are using OS-specific date/time operations
12 #include <time.h>
13
14
15
16
17 // constructor, default value is 0/0/00 (invalid date !)
18 CDate::CDate(int nDay, int nMonth, int nYear)
19 {
20 // store date
21 m_nDay = nDay;
22 m_nMonth = nMonth;
23 m_nYear = nYear;
24 }
25
26
27 // attention ! this method is NOT part of CDate
28 ostream& operator<<(ostream &mystream, const CDate& date)
29 {
30 return date.OutStream(mystream);
31 }
32
33
34 // used to perform proper display in derived classes
35 ostream& CDate::OutStream(ostream& mystream, bool bDebug) const
{
36 if (!bDebug)
37 {
38 mystream << m_nDay << "." << m_nMonth << "." << m_nYear;
39 }
40 else
{
41 mystream << "DEBUG info for '" << ClassnameOf() << "'" << endl
42 << " m_nDay=" << m_nDay
<< " m_nMonth=" << m_nMonth
<< " m_nYear=" << m_nYear;
43 }
44
45 return mystream;
46 }
47
48
49 // return current date
50 CDate CDate::GetToday()
51 {
52 CDate dtReturn;
53
54 // the following code is taken from MSDN
55
56 // use system functions to get the current date as UTC
57 time_t secondsSince1970;
58 time(&secondsSince1970);
59
60 // convert UTC to local time zone
61 struct tm *localTime;
62 localTime = localtime(&secondsSince1970);
63
64 // store retrieved date
65 dtReturn.m_nDay = localTime->tm_mday;
66 dtReturn.m_nMonth = localTime->tm_mon + 1;
67 dtReturn.m_nYear = localTime->tm_year + 1900;
68
69 return dtReturn;
70 }
71
72
73 // validate a date
74 bool CDate::IsValid() const
{
75 // validate month
76 if (m_nMonth < JANUARY || m_nMonth > DECEMBER)
77 return false;
78
79 // days per month, february ma(r)y vary !!!
80 int nDaysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
81
82 // adjust days of february
83 if (IsLeapYear(m_nYear))
84 nDaysPerMonth[FEBRUARY]++;
85
86 // validate day
87 if (m_nDay < 1 || m_nDay > nDaysPerMonth[m_nMonth])
88 return false;
89
90 // day and month are valid
91 return true;
92 }
93
94
95 // determine whether it is a leap year
96 bool CDate::IsLeapYear(int nYear)
97 {
98 // used to speed up code, may be deleted
99 if (nYear % 4 != 0)
100 return false;
101
102 // algorithm taken from MSDN, just converted from VBA to C++
103 if (nYear % 400 == 0)
104 return true;
105 if (nYear % 100 == 0)
106 return false;
107 if (nYear % 4 == 0)
108 return true;
109
110 // this line won't be executed because of optimization (see above)
111 return false;
112 }
113
114
115 // copy constructor
116 CDate& CDate::operator =(const CDate &date)
117 {
118 m_nDay = date.m_nDay;
119 m_nMonth = date.m_nMonth;
120 m_nYear = date.m_nYear;
121 // m_bDebug is not copied !
122
123 return *this;
124 }
125
126
127 // virtual, see operator=
128 bool CDate::Copy(const CDate *date)
129 {
130 // invalid class
131 if (date == NULL || date == this)
132 return false;
133
134 // use non virtual reference based copy
135 // return value isn't needed
136 operator=(*date);
137
138 // we're done
139 return true;
140 }
141
142
143 // compare two dates
144 bool CDate::operator ==(const CDate &date) const
{
145 // m_bDebug is not neccessary
146 return (m_nDay == date.m_nDay &&
147 m_nMonth == date.m_nMonth &&
148 m_nYear == date.m_nYear);
149 }
150
151
152 // virtual, see operator==
153 bool CDate::EqualValue(CDate *date) const
{
154 // invalid class
155 if (date == NULL)
156 return false;
157
158 // use non virtual reference based copy
159 return operator==(*date);
160 }
161
162
163 // set attributes, returns validility of date
164 inline bool CDate::SetDay(int nDay)
165 {
166 m_nDay = nDay;
167 return IsValid();
168 }
169 inline bool CDate::SetMonth(int nMonth)
170 {
171 m_nMonth = nMonth;
172 return IsValid();
173 }
174 inline bool CDate::SetYear(int nYear)
175 {
176 m_nYear = nYear;
177 return IsValid();
178 }
179
180 // return attributes
181 inline int CDate::GetDay() const
{
182 return m_nDay;
183 }
184 inline int CDate::GetMonth() const
{
185 return m_nMonth;
186 }
187 inline int CDate::GetYear() const
{
188 return m_nYear;
189 }
190
191